Unit 1 Ma
Unit 1 Ma
Introduction to 8051
The 8051 microcontroller is a very popular 8-bit microcontroller introduced by Intel in the
year 1981 and it has become almost the academic standard now a days. The 8051 is based on
an 8-bit CISC core with Harvard architecture. Its 8-bit architecture is optimized for control
applications with extensive Boolean processing. It is available as a 40-pin DIP chip and works
at +5 Volts DC.
Architecture Diagram
The architecture of the 8051 microcontroller can be understood from the block diagram. It
has Harward architecture with RISC (Reduced Instruction Set Computer) concept.
The block diagram of 8051 microcontroller is shown. It consists of:
an 8-bit ALU
one 8-bit PSW (Program Status Register)
A and B registers
one 16-bit Program counter
one 16-bit Data Pointer Register (DPTR)
128 bytes of RAM and 4kB of ROM and four parallel I/O ports each of 8-bit width
Architecture Explained
Arithmetic Logic Unit (ALU)
8-bit ALU
can perform all the 8-bit arithmetic and logical operations in one machine cycle
The ALU is associated with two registers A & B which are special function registers which
hold the results of many arithmetic and logical operations.
A Register
It is also called the Accumulator and as it’s name suggests, it is used as a general register to
accumulate the results of a large number of instructions.
By default it is used for all mathematical operations and also data transfer operations between
CPU and any external memory.
B Register
It is mainly used for multiplication (MUL AB) and division ( DIV AB) operations along with A
register.
It has no other function other than as a location where data may be stored.
The R registers
The "R" registers are a set of eight registers that are named R0, R1 up to and R7.
These registers are used as auxillary registers in many operations.
These registers are also used to temporarily store values.
Program Counter (PC)
16-bit program counter
It always points to the address of the next instruction to be executed. After execution of one
instruction the program counter is incremented to point to the address of the next instruction
to be executed.
Contents of PC are placed on address bus to find and fetch the desired instruction.
Since the PC is 16-bit width, 8051 can access program addresses from 0000H to FFFFH, a total
of 6kB of code.
Stack Pointer Register (SP)
8-bit register which stores the address of stack top. i.e the Stack Pointer is used to indicate
where the next value to be removed from the stack should be taken from.
When a value is pushed onto the stack, the 8051 first increments the value of SP and then
stores the value at the resulting memory location.
Similarly when a value is popped off the stack, the 8051 returns the value from the memory
location indicated by SP, and then decrements the value of SP.
Since the SP is only 8-bit wide it is incremented or decremented by two.
SP is modified directly by six instructions: PUSH, POP, ACALL, LCALL, RET, and RETI.
It is also used intrinsically whenever an interrupt is triggered.
Stack
The CPU needs this storage area as there are only limited number of registers.
It is a part of RAM used by the CPU to store information temporarily. This information may
be either data or address.
The register used to access the stack is called the Stack Pointer which is an 8-bit register.
So,it can take values of 00 to FF H.
When the 8051 is powered up, the SP register contains the value 07. It means the RAM
location value 08 is the first location being used for the stack by the 8051 controller.
There are two important instructions to handle this stack.
1. PUSH: The loading of data from CPU registers to the stack is done by PUSH
2. POP: The loading of contents of the stack back into aCPU register is done by POP
In the above instructions the contents of the Registers R6 and R1 are moved to stack
and they occupy the 08 and 09 locations of the stack. Now the contents of the SP are
incremented by two and it is 0A.
Similarly POP 3 instruction pops the contents of stack into R3 register. Now the
contents of the SP is decremented by 1.
In 8051 the RAM locations 08 to 1F (24 bytes) can be used for the Stack.
In any program if we need more than 24 bytes of stack, we can change the SP point to RAM
locations 30 - 7F H. This can be done with the instruction MOV SP , # XX.
Data Pointer Register (DPTR)
It is a 16-bit register which is the only user-accessible.
As the name suggests, DPTR is used to point to data.
It is used by a number of commands which allow the 8051 to access external memory. When
the 8051 accesses external memory it will access external memory at the address indicated by
DPTR.
DPTR can also be used as two 8-registers DPH and DPL.
Program Status Register (PSW)
The 8051 has a 8-bit PSW register which is also known as Flag register. In the 8-bit
register only 6-bits are used by 8051. Two unused bits are user definable bits.
In the 6-bits four of them are conditional flags. They are Carry - CY, Auxiliary Carry - AC,
Parity - P and Overflow - OV. These flag bits indicate some conditions that resulted after an
instruction was executed.
The meaning of various bits of PSW register is shown below:
0 0 0 00H-07H
0 1 1 08H-0FH
1 0 2 10H-17H
1 1 3 18H-1FH
Special Function Registers (SFRs)
Certain registers which use RAM addresses from 80h to FF H and they are meant for
certain specific operations. These registers are called Special Function Registers (SFRs).
Some of these registers are bit addressable.
Some of them are related to I/O ports (P0, P1, P2 and P3).
Some of them are meant for control operations (TCON, SCON, PCON)
Remaining are the auxillary SFRs, in the sense that they don't directly configure the 8051.
The list of SFRs and their functional names are given below.
Sr. No. Symbol Name of SFR Address (Hex)
1 ACC* Accumulator 0E0
2 B* B-Register 0F0
3 PSW* Program Status word register 0DO
4 SP Stack Pointer Register 81
5 DPL Data Pointer - low byte 82
DPTR DPH Data Pointer - high byte 83
6 P0* Port 0 80
7 P1* Port 1 90
8 P2* Port 2 0A
9 P3* Port 3 0B
10 IP* Interrupt Priority control 0B8
11 IE* Interrupt Enable control 0A8
12 TMOD Timer Mode Register 89
13 TCON* Timer Control Register 88
14 TH0 Timer 0 - Higher byte 8C
15 TL0 Timer 0 - Lower byte 8A
16 TH1 Timer 1 - Higher byte 8D
17 TL1 Timer 1 - Lower byte 8B
18 SCON* Serial Control Register 98
19 SBUF Serial Buffer Register 99
20 PCON Power Control Register 87
The * indicates the bit addressable SFRs.
Ports of 8051
There are four ports P0, P1, P2, and P3.
Each port uses 8 pins.
All I/O pins are bi-directional.
The four I/O ports:
o Port 0 (Pins 32-39): P0(P0.0~P0.7)
o Port 1 (Pins 1-8): P1(P1.0~P1.7)
o Port 2 (Pins 21-28): P2(P2.0~P2.7)
o Port 3 (pins 10-17): P3(P3.0~P3.7)
Each port has 8 pins.
Named P0.X, P1.X, P2.X, P3.X; where (X=0,1,...,7)
o Ex: P0.0 is the bit 0 (LSB) of P0
o Ex: P0.7 is the bit 7 (MSB) of P0
These 8 bits form a byte.
Each port can be used as input or output (bi-direction)
Port-0
Port-0 can be used as a normal bidirectional I/O port or it can be used for
address/data interfacing for accessing external memory.
Q=1
Q=0 which turns 'off' the lower FET while due to '0' control signal upper FET
also turns off
Hence the output pin have floats hence "HIGH" data written on pin is directly read by
read pin.
Case II: Reading "LOW" on Pin P0.X (Control Pin=0)
Internal CPU Bus(D Latch input)= 1
Output of D latch
Q=0
Q=1 which turns 'ON' the lower FET while due to '0' control signal upper FET is
turned off.
Hence the output pin have floats hence "LOW" data written on pin is directly read by
read pin.
PORT-0 as Normal Output Port
Case I: Writing "High" on Pin P0.X (Control Pin=0)
Internal CPU Bus(D Latch input)= 1
Output of D latch
Q=1
Q=0 which turns 'off' the lower FET while due to '0' control signal upper FET
also turns off.
Here we want logic '1' on pin but we getting floating value so to convert that floating
value into logic '1' we need to connect the pull up resistor parallel to upper FET. This is
the reason why we needed to connect pull up resistor to port 0 when we want to
initialize port 0 as an output port .
Case II: Writing "LOW" on Pin P0.X (Control Pin=0)
Internal CPU Bus(D Latch input)= 0
Output of D latch
Q=0
Q=1 which turns 'ON' the lower FET while due to '0' control signal upper FET is
turn off
The pin is pulled down by the lower FET. Hence the output becomes zero.
PORT-0 as Address or Data Bus
(When the control pin=1, address/data bus controls the output driver FETs.)
Case I: Writing "LOW" on Pin P0.X
If the address/data bus (internal) is '0',
Q=1
Q=0 which turns 'off' the FET
Hence the output pin have floats hence "HIGH" data written on pin is directly read by
read pin.
Case II: Reading "LOW" on Pin P1.X
Internal CPU Bus(D Latch input)= 1
Output of D latch
Q=0
Q=1 which turns 'off' the FET.
Hence the output pins have floats and "LOW" data written on pin is directly read by
read pin.
PORT-1 as Normal Output Port
Case I: Writing "High" on Pin P1.X
Internal CPU Bus(D Latch input)= 1
Output of D latch
Q=1
Q=0 which turns 'off' the lower FET
Hence at P1.X=VCC or logic '1' on pin .
Case II: Writing "LOW" on Pin P0.X (Control Pin=0)
Internal CPU Bus(D Latch input)= 0
Output of D latch
Q=0
Q=1 which turns 'ON' the lower FET
The pin is pulled down by the lower FET.
Hence at P1.X = Ground or logic '0' on pin.
Hence the output becomes zero.
PORT-2
The structure of a port-2 pin is shown in fig. below. It has 8-pins (P2.0-P2.7)
Port-2 we use for higher external address byte or a normal input/output port. The I/O
operation is similar to Port-1.
Port-2 latch remains stable when Port-2 pin are used for external memory access. Here
again due to internal pull-up there is limited current driving capability.
PORT-3
Port-3 (P3.0-P3.7) has alternate functions to each pin. The internal structure of a port-3
pin is shown in figure below.
Alternate Functions of Port 3:
Bits P3.0 and P3.1 are used for the RxD (Receive Data) and TxD (Transmit Data)
serial communications signals.
Bits P3.2 and P3.3 are meant for external interrupts.
Bits P3.4 and P3.5 are used for Timers 0 and 1.
Bits P3.6 and P3.7 are used to provide the write and read signals of external
memories connected in 8031 based systems
Sr. No. Port 3 bit Pin No Function
1 P3.0 10 RxD
2 P3.1 11 TxD
3 P3.2 12 INT0
4 P3.3 13 INT1
5 P3.4 14 T0
6 P3.5 15 T1
7 P3.6 16 WR
8 P3.7 17 RD
1. Four register banks (Bank0,Bank1, Bank2 and Bank3) each of 8-bits (total 32
bytes). The default bank register is Bank0. The remaining Banks are selected
with the help of RS0 and RS1 bits of PSW Register.
2. 16 bytes of bit addressable area and
3. 80 bytes of general purpose area (Scratch pad memory) as shown in the diagram
below. This area is also utilized by the microcontroller as a storage area for the
operating stack.
The 32 bytes of RAM from address 00 H to 1FH are used as working registers organized
as four banks of eight registers each.The registers are named as R0-R7 .Each register
can be addressed by its name or by its RAM address.
For EX : MOV A, R7 or MOV R7,#05H
Register bank to be selected using RS1,RS0 bits from Program
status word register
RS1 RS0 Register Bank RAM Address
0 0 Register Bank 0 (Slected as by default) 00H-07H
0 1 Register Bank 1 (Stack memory) 08H-0FH
1 0 Register Bank 2 10H-17H
1 1 Register Bank 3 18H-1FH
Internal ROM:
Internal ROM occupies the code address space from 0000H to 0FFFH (Size = 4K
byte)
Program addresses higher than 0FFFH will automatically fetch code bytes from
external program memory
Code bytes can also be fetched exclusively from an external memory by
connecting the external access pin (EA) to ground
External program memory interfacing with 8051
EA pin is active low input pin so if EA=0 or connected to ground the code bytes
will be fetched from External Program memory
ALE(Address Latch Enable)= indicates the Adress Latch is enabled
Reset 0000H
It is the program associated with the perticular interrupt whose starting memory
location is pointed by IVT table address.
The following table shows interrupt vector table:
Interrupt Enable (IE) register
All interrupt are disabled after reset. We can enable and disable them by Interrupt
Enable (IE) register.
IE.7 IE.6 IE.5 IE.4 IE.3 IE.2 IE.1 IE.0
EA - ET2 ES ET1 EX1 ET0 EX0
Functional name Bit number Function
EA IE.7 Disables all interrupts
- IE.6 No implemented,Reserve for future
ET2 IE.5 Enables or disables Timer 2 overflow flag interrupt
ES IE.4 Enables or disables Serial communication interrupt
ET1 IE.3 Enables or disables Timer 1 overflow flag interrupt
EX1 IE.2 Enables or disables Timer 2 overflow flag interrupt
ET0 IE.1 Enables or disables Timer 2 overflow flag interrupt
EX0 IE.0 Enables or disables Timer 2 overflow flag interrupt
Enabling and disabling an interrupt
By bit operation; recommended in the middle of program.
ORG 0
LJMP MAIN
ORG 000BH
LJMP T0ISR
ORG 001BH
LJMP T1ISR
ORG 0030H
MAIN: MOV TMOD,#12H
MOV TH0,#-71
SETB TR0
SETB TF1
MOV IE,#8AH
MOV IE,#8AH
SJMP $
T0ISR: CPL P1.7
RETI
T1ISR: CLR TR1
MOV TH1,#HIGH(-1000)
MOV TL1,#LOW(-1000)
SETB TR1
CPL P1.6
RETI
END
Timer ISR
There is no need for a “CLR TFx” instruction in timer ISR.
8051 clears the TF internally upon jumping to ISR.
We must reload timer in mode 1.
There is no need on mode 2 (timer auto reload)
Interrupt Priorities
What if two interrupt sources interrupt at the same time?
Timers/Counters Specifications
Two 16 bit timers/counters, which can be programmed independently as -
timer or event counter.
Four-SFRs connected with TIMER/COUNTER operation
1. TMOD - Timer Mode Register
2. TCON - Timer Control Register
3. TH0, TL0 - Timer/Counter - 0
4. TH1, TL1 - Timer/Counter - 1
Two pins of 8051 connected with Timer/counter.
1. T0 - Timer 0 external input - P3.4
2. T1 - Timer 1 external input - P3.5
INT0 and INT1 are also used for controlling the timers/counters.
Timer Operation
Timer Register (TH0, TL0 or TH1, TL1) incremented every m/c cycle. Thus
working at increment frequency of 1/12 of oscillator frequency (for 12 oscillator
machine cycle).
Any preset value i.e. initial count can be loaded to TH0, TL0 or TH1, TL1.
Example:
Clock frequency = 11.0592 MHz, Clock period = 1/12 µ sec, Machine cycle time =
1.08 µ sec
Thus timer register will be incremented every microsecond.
If timer is initialized to 0000H;
max. count = FFFFH and
max. time measured = 65536 * 1.08 µ sec= 70.77 milliseconds
Counter Operation
Counts pulses occurring at T0 pin (Timer/Counter 0) and/or T1 pin (Timer/Counter 1).
May correspond to event like
1 1 Timer in mode3 Split Timer 0 into two 8-bit counters or to stop Timer 1
TR1: Timer 1 run control bit. Set/cleared by software to turn the timer/counter on/off.
TF0: Timer 0 overflow flag. Set by hardware when the timer/counter overflows.
Cleared by hardware when the processor vectors to the interrupt routine.
TR0: Timer 0 run control bit. Set/cleared by software to turn the timer/counter on/off.
As value in Timer register rolls from all ones (i.e. FFFFH) to all zero’s (i.e. 0000H)
interrupt flag (TF0 or TF1) will be set.
TF0 (for Timer 0) and TF1 (for Timer 1) are bits of TCON SFR.
IF Timer 0 or Timer 1 interrupt is enabled then program control will branch to
interrupt servicing routine as shown in figure below
Modes are set by M1 M0 bits of TMOD register.
Mode 0: 13 bit Timer/Counter operation
TH0, TL0 (for Timer 0) or TH1, TL1 (for Timer 1) used as 13 bit counter.
o All 8 bits of TH0 or TH1
o 5 lower bits of TL0 or TL1 are used, for counting.
When count rolls over from all 1’s to all 0’s, - interrupt flag TF0 or TF1 is set.
In above figure when C/T = 0 - timer operation count incremented every m/c
cycle.
o Case I: TR0 (TCON. 4) or TR1 (TCON. 6) = 1 and Gate (TMOD. 3) or
(TMOD. 7) = 0
o Case II: TR0 or TR1 =1 and Gate = 1 and INT0 or INT1 = 1
Thus by sending Logic High signal on INT0 (or INT1) pins.Timer 0 or Timer 1 can
be started.
Example: This can be used for finding pulse width in the following way.
When pulse goes high: timer starts counting at the rate 1/12 clock frequency.
When pulse goes low: Timer stops.
INT0 or INT1 = Low: causes interrupt.
Operation same as mode 0 except that all bits of TH0, TL0 or TH1, TL1 are used.
When count rolls over from all 1’s to all 0’s – TF0 or TF1 interrupt flag is set.
Causes interrupt if enabled.
Mode 2: 8 bit auto-reload Timer/Counter
It acts as two 8 bit counters, i.e. TL0 and TH0 become two separate counter.
TL0: 8 bit operation in mode 0 or mode 1 (Timer or Counter); controlled by C/T, TR0,
Gate, INT0.
Sets TF0 when count rolls to all 0’s from all 1’s.
TH0: Timer function only.
Since count will roll from FFFF to 0000 additional m/c cycle will be required to
set TF0 or TF1.
Thus initial count must be 15536 = 3CB0H
By putting initial preset count of 3CB0H (or 15536 decimal), the register will
reach FFFF in 49999 m/c cycle and roll over to 0000 in 50,000th m/c cycle
accounting for 50 millisecond
a. Configure Timer 1 using TMOD register
G C/T M1 M0 G C/T M1 M0
0 0 0 1 0 0 0 0
Gate = 0, C/T = 0, Mode = 01 (16 bit timer operation)
MOV TMOD , # 10H
Example 2:
Write an ALP to generate delay of 3 ms using timer 0. Clock frequency = 16 MHz.Also
find out the value to be loaded in TH0,TL0 register.
Solution:
The TxD and Rx D pins are connected to the TI in and RI in pins of the MAX 232 IC and
the TI out and RI in pins of the MAX IC are connected to the RxD and TxD pins of the
DB9 connector as shown in the interface diagram
Baud Rate:-The rate at which the number of bits are transmitted
PC and microcontroller supports various types of Baud rates eg.
19200,9600,4800,2400,1200 etc
where
TH1=Value to be loaded in TH1 register
k=1 when SMOD=0 from PCON register
k=2 when SMOD=1 from PCON register
o XTAL freq=11.0592 MHz
Description
SMO,SM1 bits
Description