Features of 8051
Features of 8051
Features of 8051
3. A microcontroller does not require much additional interfacing ICs for operation and it
functions as a stand alone system. The operation of a microcontroller is multipurpose, just like
a Swiss knife.
Features of 8051
ARCHITECTURE OF 8031/8051 MICROCONTROLLER
2. Accumulator:
It is an 8-bit register.
It holds a data and receives the result of the arithmetic instructions.
3. B register:
An 8-bit general-purpose register.
The 16-bit data pointer is used for accessing external data memory.
The contents of data pointer are programmable using instructions.
It is a 16 bit register.
PC is used, as address pointer to access program instructions and it is automatically
incremented after every byte of instruction fetch.
I/O Devices:
The 8051 has four numbers of 8-bit ports namely port-0, port- 1, port-2 and port-3.
Each port has a latch and driver (or buffer).
When external memory is employed the port-0 lines will function as multiplexed low byte
address/data lines and port-2 lines will function as high byte address lines.
Also the port pins P3.7 and P3.6 are used to output read and write control signals respectively.
The port-1 is dedicated I/O port and does not have any alternate function.
The ports are also mapped as internal memory in the controller and so they can be addressed as
memory locations for 8-bit operation.
The 8051 has 8-bit ALU, which performs arithmetic and logical operations on binary data.
The A and B registers are used to hold the input data and the result of ALU operation.
The controller will fetch the instructions one by one, starting from the address stored in PC and
store in
IR, which decodes the instructions and give information to timing and control unit.
Using the information supplied by the IR unit the control signals necessary for internal and
external operations are generated by the timing and control unit.
Timer/Counter:
The 8031/8051 has two 16-bit programmable timer/counter namely timer-1 and timer 0.
In the counter mode of operation they can count the number of high to low transitions of
the signal applied to the timer pins.
In timer mode of operation they can be independently programmed to work in any one of
the four operation modes. They are called mode-0, mode-1, mode-2 and mode-3.
Serial port:
The 8031/8051 has a serial data communication circuit that uses pins RXD (P3.0) and TXD (P3.0)
for connecting serial communication device.
Special function registers related to serial port are SBUF, SCON and PCON.
DATA TYPES:
The 8051 microcontroller has only one data type. It is 8 bits, and the size of each
register is also 8 bits.
DIRECTIVES (PSEUDOCODES):
Directives or assembler directives are instructions given to the assembler. The following are directives
used in 8051
DB (Define Byte):
It is used to define the 8-bit data.
When DB is used to define data, the numbers can be in decimal, binary, hex, or ASCII
formats.
Example: DATA1 DB 24 ; 1 byte of memory is assigned for variable DATA1 and set its value with
decimal 24
DATA2 DB 34H ; 1 byte of memory is assigned for variable DATA2 and set its value with hex
value 34
EQU (Equate):
This is used to define a constant without occupying a memory location
Example:
When executing the instruction MOV R3, #COUNT, the register R3 will be loaded with the
value 25.
ORG (Origin):
The ORG directive is used to indicate the beginning of the address.
The number that comes after ORG can be either in hex or in decimal. If the number is
not followed by H, it is decimal and the assembler will convert it to hex.
END:
Another important DIRECTIVE is the END directive.
This indicates to the assembler the end of the source (asm) file.
The END directive is the last line of an 8051 program, meaning that in the source code
anything after the END directive is ignored by the assembler.
REGISTER BANKS
A total of 80 bytes from locations 30H to 7FH are used for read and write
storage, or what is normally called a scratch pad. These 80 locations of RAM are
widely used for the purpose of storing data and parameters by 8051 programmers.
Register Banks:
As mentioned earlier, a total of 32 bytes of RAM are set aside for the register banks and
stack.
These 32 bytes are divided into 4 banks of registers in which each bank has 8 registers,
RO R7.
The following shows how the 32 bytes are allocated into 4 banks:
We can select register banks by use of the PSW (program status word) register.
Bits D4 and D3 of the PSW are used to select the desired register bank as:
STACK IN 8051
The stack is a section of RAM used by the CPU to store information temporarily.
The register used to access the stack is called the SP (stack pointer) register.
The stack pointer in the 8051 is only 8 bits wide, which means that it can take values of 00 to
FFH.
When the 8051 is powered up, the SP register contains value 07.
The storing of a CPU register in the stack is called a PUSH, and pulling the contents of the stack
back into a CPU register is called a POP.
Pushing onto the stack
In the 8051 the stack pointer (SP) points to the last used location of the stack. As we
push data onto the stack, the stack pointer (SP) is incremented by one.
Popping from the stack
Popping the contents of the stack back into a given register is the opposite process of
pushing.
With every pop, the top byte of the stack is copied to the register specified by the
instruction and the stack pointer is decremented once.
INSTRUCTION SET
Data Transfer Instructions:
Arithmetic Instructions:
Logical Instructions:
ANL C, bit address ; AND carry flag and the addressed bit
CPL C ; Complement Carry flag
CPL bit address ; Complement the bit by which the address is given.
CLR bit address ; Clear the bit by which the address is given.
SETB bit address ; Set the bit by which the address is given.
JUMP INSTRUCTIONS
Bit Jump Instructions:
Timers / Counters
8051 has two 16-bit programmable UP timers/counters. They can be configured to operate either as timers
or as event counters. The names of the two counters are T0 and T1 respectively. The timer content is
available in four 8-bit special function registers, viz, TL0,TH0,TL1 and TH1 respectively.
In the "timer" function mode, the counter is incremented in every machine cycle. Thus, one can think of it
as counting machine cycles. Hence the clock rate is 1/12 th of the oscillator frequency.
In the "counter" function mode, the register is incremented in response to a 1 to 0 transition at its
corresponding external input pin (T0 or T1). It requires 2 machine cycles to detect a high to low
transition. Hence maximum count rate is 1/24 th of oscillator frequency.
The operation of the timers/counters is controlled by two special function registers, TMOD and TCON
respectively.
TMOD
Address: 89 H
Gate: This is an OR Gate enabled bit which controls the effect of on START/STOP of Timer. It is
set to one ('1') by the program to enable the interrupt to start/stop the timer. If TR1/0 in TCON is set and
signal on pin is high then the timer starts counting using either internal clock (timer mode) or
external pulses (counter mode).
TCON is bit addressable. The address of TCON is 88H. It is partly related to Timer and partly to interrupt.
TF1 : Timer1 overflow flag. It is set when timer rolls from all 1s to 0s. It is cleared when processor vectors
to execute ISR located at address 001BH.
TR1 : Timer1 run control bit. Set to 1 to start the timer / counter.
TF0 : Timer0 overflow flag. (Similar to TF1)
TR0 : Timer0 run control bit.
IE1 : Interrupt1 edge flag. Set by hardware when an external interrupt edge is detected. It is cleared when
interrupt is processed.
IE0 : Interrupt0 edge flag. (Similar to IE1)
IT1 : Interrupt1 type control bit. Set/ cleared by software to specify falling edge / low level triggered
external interrupt.
IT0 : Interrupt0 type control bit. (Similar to IT1)
As mentioned earlier, Timers can operate in four different modes. They are as follows
Timer Mode-0:
The lower 5 bits of TLX and 8 bits of THX are used for the 13 bit count.Upper 3 bits of TLX are ignored.
When the counter rolls over from all 0's to all 1's, TFX flag is set and an interrupt is generated.
The input pulse is obtained from the previous stage. If TR1/0 bit is 1 and Gate bit is 0, the counter continues
counting up. If TR1/0 bit is 1 and Gate bit is 1, then the operation of the counter is controlled by input.
This mode is useful to measure the width of a given pulse fed to input.
Timer Mode-1:
This mode is similar to mode-0 except for the fact that the Timer operates in 16-bit mode.
This is a 8 bit counter/timer operation. Counting is performed in TLX while THX stores a constant value. In
this mode when the timer overflows i.e. TLX becomes FFH, it is fed with the value stored in THX. For
example if we load THX with 50H then the timer in mode 2 will count from 50H to FFH. After that 50H is
again reloaded. This mode is useful in applications like fixed time sampling.
Timer 1 in mode-3 simply holds its count. The effect is same as setting TR1=0. Timer0 in mode-3
establishes TL0 and TH0 as two separate counters.
Fig 4.9 Operation of Timer in Mode 3
Control bits TR1 and TF1 are used by Timer-0 (higher 8 bits) (TH0) in Mode-3 while TR0 and TF0 are
available to Timer-0 lower 8 bits(TL0).
INTERRUPTS
1. ( External Interrupt 0)
3. ( External Interrupt 1)
Out of these, and are external interrupts whereas Timer and Serial port interrupts are
generated internally. The external interrupts could be negative edge triggered or low level triggered.
All these interrupt, when activated, set the corresponding interrupt flags.
Each of these interrupts can be individually enabled or disabled by 'setting' or 'clearing' the
corresponding bit in the IE (Interrupt Enable Register) SFR. IE contains a global enable bit EA which
enables/disables all interrupts at once.
EA Enable/Disable all
Each interrupt source can be programmed to have one of the two priority levels by setting (high priority)
or clearing (low priority) a bit in the IP (Interrupt Priority) Register . A low priority interrupt can itself
be interrupted by a high priority interrupt, but not by another low priority interrupt. If two interrupts of
different priority levels are received simultaneously, the request of higher priority level is served. If the
requests of the same priority level are received simultaneously, an internal polling sequence determines
which request is to be serviced. Thus, within each priority level, there is a second priority level
determined by the polling sequence, as follows.
Interrupt handling:
When an interrupt comes and the program is directed to the interrupt vector address, the Program
Counter (PC) value of the interrupted program is stored (pushed) on the stack. The required Interrupt
Service Routine (ISR) is executed. At the end of the ISR, the instruction RETI returns the value of the
PC from the stack and the originally interrupted program is resumed.
Reset is a non-maskable interrupt. A reset is accomplished by holding the RST pin high for
at least two machine cycles.
Stepper motor is one of the commonly used motors for precise angular movement.
The advantage of using a stepper motor is that the angular position of the motor shaft
can be controlled.
Similar to all the motors, stepper motors will have stator and rotor. Rotor has permanent
magnet and stator has coil.
The basic stepper motor has 4 coils with 90 degrees rotation step.
These four coils are activated in the cyclic order.
Step A B C D
0 1 1 0 0
1 0 1 1 0
2 0 0 1 1
3 1 0 0 1
Interfacing Diagram:
The ULN2003 is a current driver IC. It is used to drive the current of the stepper motor
as it requires more current. It is an array of Darlington pairs.
Stepper motor has 6 pins. In these six pins, 2 pins are connected to the supply of 12V and the
remaining pins are connected to the output of current driver.
Program: