Unit 4-Microcontroller 8051
Unit 4-Microcontroller 8051
4.1 Introduction
Microprocessors are playing an important role in various industries and
influencing our day-to-day life more strongly than one can imagine. Basically,
microprocessor is a single chip Very Large Scale Integrated (VLSI) digital
circuit. The term microprocessor comes from the merger of micro-electronics
technology. Microprocessor is defined as Arithmetic Logic Unit (ALU) and
Control Unit (CU) on a single chip. The Central Processor Unit (CPU) consists of
an Arithmetic Logic Unit (ALU) and Control Unit (CU), So microprocessor can
act as CPU. To have a minimum system configuration, microprocessor should
be connected with external memory, I/O ports etc.
Similarly, the microcontroller can be defined as a complete microprocessor
system on a single chip which includes the CPU (microprocessor), Memory
(RAM, ROM), I/O ports, Serial interface etc. Microcontrollers are developed to
replace the microprocessor in low cost products.
I/O Port
Internal ROM
Interr
upt
Circu
Cloc
kt
Program Counter
125
Fig. 4.1 shows the block diagram of a typical microcontroller, which is a true
computer on a single ship. It has all features found in a microprocessor CPU,
ALU, Program Counter, Stack Pointer and Registers. In added to the above
feature, it has internal ROM, RAM, Parallel I/O ports, Serial I/O port and timers.
Like microprocessor it is a general-purpose device, which reads the data,
perform limited calculations and logical operations and control the
environment based on the result of calculations/logical operations.
The important use of a microcontroller is to control the operation of
machines using a program stored in ROM memory and does not change its
operation over the life time of the system.
Many of the instructions are coupled with pins on the integrated circuit
package. Thus, the pins are programmable, which means pins of
microcontroller are accessible by the programmer and can be set as per the
requirement.
CPU
Microcontroller 8051 has an 8-bit CPU. It consists of Accumulator (A
Register), B register, Arithmetic Logic Unit (ALU) and Control Unit (CU). It is
used to perform basic mathematical calculations and Logical operations. 8051
CPU can process 8-bit data only.
Memory
The 8051 has internal RAM and ROM memories. 128 bytes of RAM and
4Kbytes of ROM are available with 8051. Internal RAM has 4 register banks
each having 8 registers.
2
Microprocessor and Microcontroller
I/O Ports
8051 consist of four 8-bit ports. They are port 0, port 1, port 2 and port 3.
These ports can be programmed either input or output port. These ports are
arranged in thirty-two input/output pins.
Timer/Counter
There are two timers timer 0 and timer 1 provided in 8051. Both timers are
16 bits wide. Timers are programmed to generate desired time delays.
Registers
In the CPU of 8051, registers are used to store information temporarily.
The majority of 8051 registers are 8-bit registers. Some of the widely used
general purpose registers are A (Accumulator), B, R0, R1, R2, R3, R4, R5, R6, R7,
Data Pointer and Program Counter. Only two registers Data pointer (DPTR)
register and Program Counter (PC) are 16-bit wide.
125
VCC VSS
Po
La
B
Register
PCON SCON TMODTCON
Temp 2 Temp 1
1
Microprocessor and
Po
SBUF SBUF Serial
IE Port
IP and Timer Blocks
La
Interrupt,
ALU
PSEN PSW
ALETiming
La
Instruc
Po
EA and Control
tion
RST
Osc
XTAL2 18
C1
8051
Crystal
XTAL1 19
C2
P2 P1 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2
P2
Address
Latch
Enable(ALE 8051 Timing
)
Fig. 4.2 Crystal Oscillator Circuit
Interrupts
There are five interrupts available in 8051, in which three are internal
interrupts and two are external interrupts. Three internal interrupts are timer
0(TF0), timer 1(TF1) and serial I/O port (RI or TI) interrupts. Two external
interrupts are INT0 and INT1.
128
Microprocessor and Microcontroller
I/O PORT
AD0 - AD7
Internal Bus
I/O PORT
I/O PORT
Alternate
function
Out of 32 I/O lines of four ports, 8 lines of port 1 are used as single I/O
function only. The remaining 24 pins of port 0, port 2 and port 3 are used for
dual functions. Port 0 pins used for I/O function and lower address and data
multiplexed port for external memory. Port 2 pins used for I/O function and
higher address for external memory. Port 3 pins used for I/O functions and
Alternative uses.
Vcc
Pin 40 provides supply to the chip. The voltage source is +5V.
GND
Pin 20 is Ground.
130
Microprocessor and Microcontroller
RST
Pin 9 is RESET pin. It is an active high input pin and normally low. Upon
applying a high pulse to this pin, 8051 terminate all activities and go to reset
condition.
EA
Pin 31 is External enable pin (EA). When this pin is connected to Vcc, the
CPU access internal memory. When EA pin connected to ground, the CPU access
the program/data from external memory.
PSEN
This is an output pin. PSEN stands for Program Store Enable. An active low
pulse in this pin enables the external ROM. This pin is connected to OE of
external ROM chip.
ALE
This is an active high output pin. ALE stands for Address Latch Enable. ALE
used to demultiplex the lower byte address of external memory and data from
port 0. When ALE is high, port 0 is connected to external memory address latch.
When ALE is low, port 0 carries the data.
131
Dr. N. Karuppiah & Dr. S. Ravivarman
Accumulator (A register)
The Accumulator (A register) is used for many operations like Addition,
Subtraction, Multiplication, Division and Boolean bit logical operations. It is also
used for data transfer between 8051 and external memories, I/O devices.
B Register
The B register is used to do arithmetic operations with A register. B register
may be used to store the data and it has no other functions.
Flags register, Program Status Word (PSW)
Flags are single bit register and used to store the result of certain function
after executing instruction. Flags are grouped inside PSW and PCON registers.
PSW register contains math flags and PCON contains general user flags. Math
flags are grouped in PSW and they are Carry(C), Auxiliary Carry (AC), Over flow
(OV), and Parity (P). The general user flags are GF0 and GF1 which are grouped
in PCON register.
7 6 5 4 3 2 1 0
132
Microprocessor and Microcontroller
Internal RAM
8051 has 128-byte internal RAM and called data memory. RAM is a read-
write memory and processor can read data or write data to this memory space.
These 128 bytes are divided into three groups and they are Register Banks, Bir
addressable registers and General purpose. Internal organisation of RAM is
given in fig. 1.9
There are four register banks available in 8051 are Bank 0, Bank 1, Bank 2
and Bank 3. By default, or on reset Bank 0 is selected. To select other Banks RS0
and RS1 in PSW is to set accordingly. Unselected register banks are used for
general purpose. The address of four register banks occupies from 00h to 1fh.
133
Dr. N. Karuppiah & Dr. S. Ravivarman
7F
2F
7F
Genera 2E 7F 78
l 77 70
30 Purpos
e Area 6F 68
2D 67 60
Bit
2F Addressabl 5F 58
2C
e Area 2 57 50
B
Registe 4F 48
20 r Bank
1F 47 40
3 29 3F 38
18 Registe 28 37 30
r Bank
2 27 2F 28
10 Registe 26 27 20
0F
r Bank 25 1F 18
1
24 17 10
08 R7
R6 0F 08
07 23
06 R5 07 00
05 22
R4 30
04 R3
BIT ADDRESSABLE GENERAL PURPOSE
R2 RAM LOCATIONS
21 RAM LOCATIONS
R1
03
02 Register20
01 Bank 0
00
Addresses from 20h to 2Fh are bit addressable area. A bit in this area may
specified by its bit address. Addressable bits are useful when the program need
to set or reset a single function (On or OFF).
Address from 30h to FFh are used as general-purpose area. Locations in
this area can be addressed byte wise.
Internal ROM
8051 has 4Kbyte of ROM. Program memory (ROM) is read only memory.
The CPU reads program from this memory and execute it. CPU cannot write
data inside this memory. Address of internal ROM is from 000h to FFFh. Above
these address the CPU will access external memory.
134
Microprocessor and Microcontroller
135
Dr. N. Karuppiah & Dr. S. Ravivarman
Store Data SP = OA SP = OA
SP = OA Get Data
SP = 09
Store Data SP = O9
Get Data
SP = O9
SP = O8
Store Data
Get Data
SP = O8 SP = O8
SP = 07
SP = O7 SP = O7
Storing Data on the
Stack (Increment Internal Getting Data from the
then store) RAM Stack (Get then
decrement)
136
Microprocessor and Microcontroller
137
Dr. N. Karuppiah & Dr. S. Ravivarman
has D-type o/p latch for each pin. The Special Function Register (SFR) for each
port is made up of these eight latches, which can be address for that port. The
port latches should not be confused with the port pins. The data in the latches
does not have to be the same as that on port pins. The data in latch buffer and
the data of ports pins can be read independently. To make any port as input or
output it must be programmed. To make a port as input port, the port latch
should be written with all 1s. By default, the port is defined as output port.
I/O operation of ports (P0, P1, P2, and P3)
I/O operation of all ports i.e. port 0, port1, port 2 and port 3 are same.
Except port 1 all other ports are having alternate function. Each port figures
shows the operation of both I/O operation and alternate function also. Common
figure of all port I/O function are shown in fig. 1.9
Rea
d
Latc
h Contr VCC
ol Extern
Signal al FET
s Pullu
p
Pin 0
Tri State
B Buffer
U Pin 7
S
Contr
Tri SFR ol
State ‘D’
Logic
Buffe Latc
r h
Rea
d
Pin
Fig. 4.9 I/O operation of 8051 Ports
Output Operation
Except port 0 other ports have pull up resistors. Pull up resistor are
connected to Vcc to make the pin high. To make port 0 as output port, external
pull up resistors of 10K are to be connected to port pins. Each port has eight D
138
Microprocessor and Microcontroller
type output latch for each pin. These eight latches are called as port SFR latch.
The data to be sent out is loaded into port SFR latch buffer. Where ever 0s are
loaded in latch, the high output from Q drives the concerned FET drivers
139
Dr. N. Karuppiah & Dr. S. Ravivarman
through control circuit and makes the corresponding pins low. Where ever 1s
are loaded in latch, output of Q is low and the control circuit cut off the FET
driver. The port pins are made 1 by external pull up resistors. Thus, the 0s and
1s written in port latch are transferred to port pins.
Note: It may be noted that the data in latch buffer and port pins may not be
the same always. There are two sets of tri-state buffers used to read the latch or
pin. When upper set of tri-state buffer is enabled by read latch signal, the latch
data are transferred to internal bus. When the lower set of tri-state buffer is
enabled by read pin data signal, the data of pin are transferred to internal bus.
Input Operation
When a port to be used as an input port, all 8-bits of port latch are to be
programmed with 1s. If a pin is to be set as input pin then corresponding latch
bit is to be set to1. When latch is set to 1, the output of Q is low and FET drivers
are in cut-off state. The pin is in float condition. The data in port pins are
connected to input buffers. When control signal read pin data goes high, the
contents of port are transferred to internal bus.
Port 0
Control Signal Address / Data
Read latch bit
VCC
BUS
D Q
Control Logic Pin
0.x
CLKQ
Write to Latch
PORT SFR Latch
Port 0 has dual function as I/O port and Address/Data bus for external
memory. When external memory is not used, port 0 is used as simple I/O port.
When external memory is used, port 0 is used as address bus and data bus.
Lower byte address and data are multiplexed in this port. ALE signal is used to
demultiplex the address from this port. ALE goes high, when this port contains
140
Microprocessor and Microcontroller
address. After addressing ALE goes low and the port become data bus. To read
the data from external memory logic 1 is written to all port SFR latches.
Port 1
Port 1 is not dual function port as port 0. It is used only for simple I/O
operation of data. So, control circuit is not used and the output of SFR latch is
directly connected to FET drivers. The Pin configuration diagram of port 1 is
shown in fig. 1.11
It has internal pull up resistors to drive the output device and no need to
connect external pull up resistors like port 0. The data to be send out is loaded
to the SFR latch of port1.
Read latch bit VCC
BUS
Pin 1.x
D Q
CLK Q
Write to Latch
PORT SFR Latch
Where ever thedata are 0 the Q output of latch is high and drives the FET
driver. The output pin goes low. Where ever data are 1 the Q output of latch is
low and the FET driver is in cut-off condition. The pull up resistor make the
output pin high.
To program the port 1 as input port, the SFR latch buffer should be set to 1.
The low output from the Q of latch isolates the FET drivers and pull up
resistors. The pins of port 1 are connected to input buffer. When read pin signal
enabled, data available in port pins are transferred to internal bus.
141
Dr. N. Karuppiah & Dr. S. Ravivarman
Port 2
Control Signal Address
Read latch bit
BUS VCC
Internal FET
Pullup
D Q Pin 2.x
Control Logic
CLKQ
Write to Latch
PORT SFR Latch
Port 3
Alternate Output
VCC
Read latch bit
Internal FET
BUS Pullup
D Q Pin
3.x
CLKQ
Write to Latch
142
Microprocessor and Microcontroller
143
Dr. N. Karuppiah & Dr. S. Ravivarman
Simple Example
i) MOV A, #35h; Load the 8-bit hexadecimal number 35
; intoAccumulator (A Register)
ii) MOV R4, #25h; Load the 8-bit hexadecimal number 25
; into register R4
Note
In the above instruction, 25h is loaded into register R4. We do have four
register banks in the internal RAM memory of 8051, starting from Bank 0 to
Bank 3. Each bank consists of 8 register namely R0 - R7. But the default register
bank is bank 0. Hence the 8-bit hex number 25h is loaded into register R4 of
Bank 0.
iii) MOV B, #40h; Load the 8-bit hex. number 40h into
; B register
iv) MOV DPTR, #4521h; Load the 16-bit hex. number
; 4521 into Data Pointer Register (DPTR)
Although the DPTR register is a 16-bit register, it can also be accessed as
two 8-bit registers namely DPh and DPl.
DPL is the low byte register and DPH is the high byte register.
Hence 4521h is loaded into DPTR register as DPL = 21h
and DPh = 45h
45H 21H
144
Microprocessor and Microcontroller
Simple Example
MOV A, R0 ; Copy the contents of register R0
; into Accumulator.
R7
R6
R5
R4
R3
R2
R1
R0 Accumulator
Register Bank 0
R7
R6
R5
R4
R3
Accumulator R2
R1
R0
Register Bank 0
145
Dr. N. Karuppiah & Dr. S. Ravivarman
7F
7E
7D
Accumulator
55
54
53
36
35
34
33
32
31
30
General Purpose RAM
Example
MOV DPTR, A is invalid. Because DPTR is a16 bit register and
Accumulator is an8-bit register. Hence the source and destination registers
must match in size. But this instruction,
MOV DPTR, #3542h
MOV R7, DPL
MOV R6, DPH are valid.
In register addressing mode, data can only be moved between accumulator
and register Rn (where n = 0 to 7). But the movement of data between Rn
register is not allowed.
For Example
MOV R4, R7 is invalid.
In the immediate addressing mode and register addressing mode, the
operands are either tagged along with the instruction itself or inside one of the
registers.
146
Microprocessor and Microcontroller
Simple Example
Accessing the contents of Register Bank
The registers R0 to R7 of register bank 0 can be accessed in direct
addressing mode as,
MOV A, 04h ; Copy the contents of RAM
; location 04h (R4 of register
bank ; 0) in to
accumulator
MOV A, 02h ; Copy the contents of RAM
; location 02h (R2 of register
bank ; 0) into
accumulator
147
Dr. N. Karuppiah & Dr. S. Ravivarman
Simple Examples
MOV A, @R0 ; Copy the contents of RAM
Limitations
The registers R0 and R1 which are used in this mode are 8 bits wide,
therefore their use is limited to access any information in internal RAM (Scratch
pad memory 30h to 7Fh or special function register). The data in external RAM
or in code space of on-chip ROM cannot be accessed.
(i.e. external RAM or ROM). Normally we use a MOV instruction to access data
from internal RAM. The external data moves (i.e. from external RAM or from
external ROM) uses MOVX and MOVC instructions.
149
Dr. N. Karuppiah & Dr. S. Ravivarman
Data Bus
ReadWriteReadWrite
A Register Data
External RAM
R0 or R1PC + A Internal and External ROM
PC + A
DPTR
DPTR + A
PC + A
PC + A
PC + A
8051
Simple Example
MOVX A, @R1; Copy the contents of data from external
; RAM location whose 8-bit address is
held ; in register R1 to
A Register.
R1 Register
8 bit address of external RAM location 3AH
External RAM
3AHData
Accumulator
Read data move
8051
150
Microprocessor and Microcontroller
DPTR Register
16 bit address
External RAM
of external Rn
AM locatio
4025H
Data
4025H
Accumulator
Read data move
8051
Both the above instructions are referred to as read Data moves i.e., data
from external RAM is moved to accumulator of 8051.
DPTR Register
16 bit address of external RAM location 4025H
External RAM
4025H
Data in Accumulator
8051
151
Dr. N. Karuppiah & Dr. S. Ravivarman
R0 Register
8 bit address of external RAM location 3AH
External RAM
3AH
Data in Accumulator
8051
Both the above instructions are referred to as write data moves. i.e., the
data from accumulator of 8051 is moved to external RAM.
152
Microprocessor and Microcontroller
Example
MOV DPTR, #2234h ; DPTR = 2234h
MOV A, #66h ; A = 66h
MOVC A, @A + DPTR ; A = 2234h + 66h
: = 229Ah
The contents of ROM location 229Ah is moved to Accumulator.
Use of PC (Program Counter) in read only data move.
Instruction
MOVC A, @A + PC
PC - Program counter holds a 16-bit address. The program counter is
incremented by 1 before it is added to Accumulator's content.
A - Holds an8-bit data
@ A + PC - Addition of 16-bit address in the program counter incremented
by 1- and 8-bit data in Accumulator results in another 16-bit address. This
newly formed 16-bit address is the address of the external ROM location from
where the data is to be read.
Example
MOV A, #60h ; A = 60h
MOVC A, @A + PC
IF the program counter holds an address 4000H then first it is incremented
by 1 before it is added with the 8-bit data in accumulator.
i.e., PC = 4000h + 1 = 4001h
153
Dr. N. Karuppiah & Dr. S. Ravivarman
storing of a CPU register in the stack is called a PUSH. The retrieving of the
contents of the stack back into a CPU register is called POP.
In 8051, RAM locations 08H to 1FH can be used for stack. The locations 20H
- 2FH of RAM are reserved for bit addressable registers and must not be used
by the stack. That means the stack in 8051 has only 24 bytes, but by using a
special instruction,
MOV SP, #xx (xx Ranges from 30h - 7Fh)
i.e., the RAM locations from 30h to 7Fh can be also used as stack.
Example
MOV R6, #32h
MOV R1, #25h
MOV R4, #0A2h
PUSH 6
PUSH 1
PUSH 4
When 8051 is powered up the stack pointer (SP) points to the location 07h
(SP = 07h).
PUSH 6 ; Push the contents of register R6 to stack. Whenever 8051
executes a PUSH instruction the first step involved is the stack pointer (SP)
incremented by 1. Therefore, stack pointer becomes 08H. The 8-bit data 32H is
stored in memory location 08H.
Before PUSH operation
After PUSH 6 operation
154
Microprocessor and Microcontroller
155
Dr. N. Karuppiah & Dr. S. Ravivarman
The next instruction also PUSH 00h. When this instruction executed, the
stack pointer incremented by one and become 32h. The Contents of register R0
i.e. ACh is move to stack location 32h.
156
Microprocessor and Microcontroller
POP 01h ; Whenever 8051 executes POP instruction, the contents from
the stack location where the Stack Pointer points (Stack Pointer points to the
top of the stack) is moved. i.e., Data ACh in stack location 32h is moved to
R1(01h is address of register R1).
R1 = ACh. After moving the data, the stack pointer decremented by 1 and
becomes 31h.
After POP 01h
157
Dr. N. Karuppiah & Dr. S. Ravivarman
Instruction
XCH A, Source Byte ; The source byte can be any
; register or internal RAM
location.
158
Microprocessor and Microcontroller
Examples
XCH A, R3 ; Exchanges the contents of R3 register with ; Accumulator
XCH A, 40h ; Exchanges the data in memory location 40H ; with
Accumulator
XCH A, @R0 ; Exchanges the contents of a memory location
Illustrative Example
MOV A, #65h ; A = 65h
MOV R2, #97h ; R2 = 97h
XCH A, R2 ; Now A= 97h and R2 = 65h
Instruction
XCHD A, @Rn
The XCHD instruction exchanges only the lower nibble of A with the lower
nibble of the data present in RAM location pointed by Rn while leaving the
upper nibble in both places intact.
A nibble consists of 4 bits. Normally a Byte is represented by 8 bits or 2
nibbles as lower nibble and higher nibble.
MOV 40H, #97h; RAM location 40h = 1001 0111(97h)
MOV A, #12h; Accumulator = 0001 0010(12h)
MOV R1, #40h; R1 = 40h (RAM Location)
XCHD A, @R1
R1 points the memory location 40H where the data is stored. The lower
nibble in memory location 40H is exchanged with the lower nibble of
Accumulator. i.e., before the program executed
40H = (97h) 1001 0111
Accumulator = (12h) 0001 0010
After the execution of program,
40H = (92h) 1001 0010
Accumulator = (17h) 0001 0111
159
Dr. N. Karuppiah & Dr. S. Ravivarman
memory location called interrupt vector. There are five interrupt flags and their
corresponding interrupt vector addresses are as follows;
Interrupt Flag Addresses of interrupt
Flags Name subroutine vectors.
IE0 External interrupt 0 0003h
TF0 Timer 0 overflow flag 000Bh
IE1 External interrupt 1 0013h
TF1 Timer 1 overflow flag 001Bh
R1 or T1 Serial Interrupt flag 0023h
The interrupt subroutine is placed at these memory locations. Whenever an
interrupt flag is set to 1, the Program Counter (PC) suspends the main program
and jumps automatically to the interrupt subroutine vector address without
separate subroutine call. The last instruction of an interrupt subroutine is RETI.
When the interrupt subroutine is executed the RETI instruction clears the
interrupt flag to 0 and resumes the main program where the interrupt occurs.
Interrupt enabled subroutine has the following events:
i) Internal generated or external hardware generated interrupt signal
set the interrupt flags to 1.
ii) When interrupt flag set to 1 the processor suspends the main
program and stores the next address of main program to stack.
iii) The program Counter jumps to interrupt subroutine vector and
activates the interrupt subroutine.
iv) After execution of interrupt subroutine, the RETI instruction reset
the interrupt flag to 0, which was set by the interrupt signal.
v) The processor resumes the main program where the interrupt
suspends the program. The address is taken from the stack.
Sample Program
SJMP OVER
.ORG 0003h ; IE 0 interrupt vector
LJMP XINT ; Jump to label of XINT
.ORG 000Bh ; TF0 interrupt vector
LJMP TINT ; Jump to label TINT
OVER: MOV SP, #40h ; Staring address of stack is 40h
-------
-------
XINT: ------- ; IE 0 Interrupt Subroutine
-------
160
Microprocessor and Microcontroller
RETI
TINT: ------- ; TF 0 Interrupt Subroutine
-------
RETI
END
Assembler directives of 8051
Some widely used 8051 directives are as followers:
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. Some
assemblers use '' .ORG '' instead of ''ORG'' for the origin directive.
EQU(Equate)
This is used to define a constant without occupying a memory location. The
EQU directive does not set aside storage for a data item but associates a
constant with a data label so that when the label appears in the program, its
constant value will be substituted for the label.
For example
count EQU 25
-------
MOV R3, # count
whenever the label count appears in the program segment it is replaced by
a constant value 25.
End
This indicates 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 the end directive is ignored by the assembler. Some assembler used ''
END'' instead of '' END''.
161
Dr. N. Karuppiah & Dr. S. Ravivarman
Timer 0 divided as
TH0 TL 0
HIGHER 8 BIT LOWER 8 BIT
Timer 1 divided as
TH1 TL 1
HIGHER 8 BIT LOWER 8 BIT
Fig 4.14 Timer Registers
The timer registers TL0, TL1, TH0 and TH1 can be addressed by its location
like any other registers A, B, Rn etc. The addresses of timer registers TL0, TH0,
TL1, TH1 and Special Function Register related to timer TMOD and TCON are
given in fig. 1.15.
TH1 8DH
TH0 8CH
TL1 8BH
TL0 8AH
TMO 89H
D 88H
TCO FUNCTION
TIMER SPECIAL
REGISTER AND ADDRESSES
Fig. 4.15 Timer SFRS and Addresses
162
Microprocessor and Microcontroller
Internal Clock
12
Timer Enable
C/T = 0
Timer 0 or 1Timer Flag
External Clock
T0 or T1
C/T = 1
TR0 or TR1
in TCON
Gate in TMOD
INT0/1
Fig. 4.16 Logic diagram of Timer
Timer can be addressed by its location also. The above instruction can be
written as follows,
MOV 8AH, #20H ;8AH is address of TL0
MOV 8CH, #38H ;8CH is address of TH0
When timer 0 enabled it will start counting from 2038h to FFFFh. Both
timers are controlled by two special function registers TMOD and TCON
registers. The TMOD register is a byte addressable and only used to set the
modes of both timers. TCON register has control bits and over flow flags for
both timers.
163
Dr. N. Karuppiah & Dr. S. Ravivarman
The lower nibble of TMOD register is used to set the mode of Timer0 and
the higher nibble is used to set the mode of Timer1.
Gate: (Timer enable bit internal/external)
By referring fig. 4.3 the Gate bit in TMOD register is set to enable timer.
When Gate is set to 0 by the programmer, the timer is start and stop by control
bit TR1/0 in TCON register. Gate set to 0, the timer is start and stop by external
control signal INT0/1.
C/T: (Counter/ Timer Bit)
This bit is used to select the clock pulse to timer registers. C/T cleared by 0.
When C/T is set to 1, the timer is receiving clock frequency from external pins
T0/1(P3.5&P3.4) and act as counter.
M0 & M1:(Mode selection Bits)
These bits are used to set the operation modes of timers. Lower nibble M0
and M1 are for timer0 and Higher nibble M0 and M1 for timer1.
M1 M0 Mode
0 0 Mode 0 (13-bit timer mode)
0 1 Mode 1(16-bit timer mode)
1 0 Mode 2 (8bit - auto-reload)
1 1 Mode 3 (8-bit Split timer)
To set the timer mode all 8 bits are to be set by an instruction. With a single
instruction, single timer or both timers can be set.
Example
MOV TMOD, #01H ;0000 0001 - mode 1 of timer 0 is set
MOV TMOD, #20H ;0010 0000 - mode 2 of timer 1 is set
MOV TMOD, #21h ;0010 0001 - mode 1 of timer 0 and
; mode 2 of timer1 are set.
164
Microprocessor and Microcontroller
The lower nibble bits IT1, IE0, IT1 and IE1 are control bits of interrupts.
Higher nibble bits TR0, TF0, TR1 and TF1 are control bits and flags of timers.
TCON.0 (IT0)
External interrupt 0 signal type control bit. When set to 1, external
interrupt flag IE0 raised by low active INT0 (Level triggering). When it is
cleared by 0, external interrupt flag IE0 raised by high to low level INT0(Edge
transient triggering).
TCON.1 (IE0)
External interrupt 0 edge flags. When external interrupt INT0 received on
port P3.2, this flag is raised to 1. It is cleared when Interrupt Service Routine
(ISR) of external interrupt 0 is executed.
TCON.2 (IT1)
External interrupt 1 signal type control bit. When set to 1, external
interrupt flag IE1 raised by low active INT1 (Level triggering). When it is
cleared by 0, external interrupt flag IE1 raised by high to low level INT1(Edge
transient triggering).
TCON.3 (IE1)
External interrupt 1 edge flags. When external interrupt INT1 received on
port P3.3, this flag is raised to 1. It is cleared when Interrupt Service Routine
(ISR) of external interrupt 1 is executed.
TCON.4 (TR0)
Timer 0 start/stop bit. When set to 1, the timer 0 is start counting. On clear
by 0 the timer stops. The content of timer 0 does not cleared by this bit.
165
Dr. N. Karuppiah & Dr. S. Ravivarman
TCON.5 (TF0)
Timer 0 over flow flag. When timer 0 is overflow with FFFFh and goes to
0000H with next pulse, this flag TF0 is raised by the timer 0. These flags are
cleared when Interrupt Service Routine of timer 0 is executed.
TCON.6 (TR1)
Timer 1 start/stop bit. When set to 1, the timer1 is start counting. On clear
by 0 the timer 1 stops. The content of timer 1 does not cleared by this bit.
TCON.7 (TF1)
Timer 1over flow flag. When timer 1 is overflow with FFFFH and goes to
0000H with next pulse, this flag TF1 is raised by the timer 1. These flags are
cleared when Interrupt Service Routine of timer 1 is executed.
7 6 5 4 32 1 04 32 1 0
8 bits of TH x 5 bits TLx
Fig. 4.19 Mode 0 of timer
166
Microprocessor and Microcontroller
Crystal Frequency
12
Flag
12
8 bits of TH x
7 65 43 21 0
Flag
Au to Reload
8 bits of TL x
7 6 5 43 2 1 0
Fig. 4.21 Mode 2 of Timer (Auto Reload)
Flag
TF0 7 6 5 4 3 2 1 0
8 bits of TL 0
Fig. 4.22 Mode 3 of timer
167
Dr. N. Karuppiah & Dr. S. Ravivarman
Crystal Time
Frequen C/T = 0 r
Enabl
e
Timer
T0/1
External Clk
Flag
TR0/1
Start/Stop
Fig 4.23 Timer logical circuit
The logical circuit of timer is shown in fig. 4.23. Timers are used to get
desired time delays. Both timer0 and timer1 can be programmed at the same
time to get two different time delays. Before programming, the preset value to
be loaded in timer to be calculated for the delay required.
For example, if 0000h is loaded in timer and started with internal clock, it
requires 65535 clock-pulses to reach FFFFh (Decimal equivalent to FFFFh is
65535). When time rolls over from FFFFh to 0000h the timer flag set to 1. So,
the maximum delay is the time period of 65535 pulses. Time period for one
pulse is 1micro-sec when 12MHz crystal is used.
Total time delay is 65535 x 1 = 65535 micro-sec
Example
Find Maximum delay achieved when 6MHz crystal is used.
Maximum delay achieved when a 6 MHz crystal used can be calculated as
follows,
Time period for one clock T = 1/ F
F = Crystal frequency / 12
= 6 / 12
= 500 KHz
168
Microprocessor and Microcontroller
Example Program
A program to get largest delay using internal clock. Output should be
available at port1 pin 3. A 12MHz crystal is used. Timer 0 to be used.
Repeat:
JNB TF0, REPEAT; Keep track timer flag TF0
CLR TR0 ; When TF0 set stop timer
CLR P1.3 ; Port 1 pin 3 to low
CLR TF0 ; Clear timer flag TF0
When the above program is executed, port 1 pin 3 high for 65535 secs and
goes to low.
The first instruction makes the output pin port1.3 to low. TMOD register is
loaded with 01H. It is 0000 0001b. M0 of Lower nibble is set to 1, so the timer 0
in mode 1. Gate bit is set to 0 to start/stop the timer by TR0 in TCON register.
Since C/T of timer 0 is set to 0, the internal clock is routed to timer. Next
instruction loads 0000H in both timer registers TL0 and TH0.
SETB P1.3 set the port 1 pin 3 to high and SETB TR0 starts the timer to
count from the loaded value 0000H. When timer is counting the flag TF0 is
under track by the instruction JNB TF0, REPEAT. This instruction is repeated
till the flag TF0 set to 1. When the timer 0 rolls over from FFFFH to 0000H the
flag TF0 set to 1 and the processor jump to next instruction. Then CLR TR0
stops the timer, and the flag TF0 and pin p1.3 are cleared by CLR P1.3.
Port1 pin 3 goes to high and remains in high for a duration, when timer
counts from 0000H to FFFFH. There are 65535 clock pulses used to count up to
FFFFH. Since 12Mhz crystal is used, time duration of one pulse is 1 sec. Total
time duration for 65535 is 65535 sec or 65.535mSec.So, the port 1 pin 3 was
high for 65.535msec.
169
Dr. N. Karuppiah & Dr. S. Ravivarman
Crystal Timer
Frequen Enable
Count
C/T = 1 er
T0/1 COUNTER
External Clk
TR0/1
Start/Stop
Flag
Counter Programming
MOV TMOD, #05H ; Timer 0 selected, C/T is set to 1
MOV TL0, #00H ;
MOV TH0, #00H ; 0000h is loaded in timer registers
SETB P3.4 ; P3.4 set as I/p for ext. clock.
SETB TR0 ; Timer 0 started
Repeat
MOV A, TL0 ; Contents of TL0 moved to A reg.
MOV P2, A ; Contents of A reg. moved to P2
JNBTF0, REPEAT ; Keep load TL0 to P2 till TF0 = 1
CLR TR0
CLR TF0
Fig.4.25
170
Microprocessor and Microcontroller
The fig. 4.25 shows the counter action when the program is executed. 8
LEDs are connected to port 2 to visualise the counting of TL0. When one clock
pulse is available at pin P3.4, TL0 counts up 01h. The contents of TL0 is moved
to P2. Thus, the LEDs connected to P2 shows the status of TL0. Let us analyse
the program,
TMOD register is set to timer 0, mode 1 and C/T = 1, so that the timer will
run on external clock from pin P3.4. Timer register is loaded with 0000h to
start from beginning. The timer is started by the instruction SETB TR0.
When the first clock pulse is available at pin P3.4, the TL0 counts up one to
01H. Now 01H is moved to accumulator and then to port 2 by MOV A, TL0 and
MOV P2, A instructions respectively. The corresponding LED glows when 01h
available at port 2. This action is repeated till timer flag TF0 is set to 1.
Solution
Calculation of initial loading number for timer.
Timer Clock is 12 MHz / 12
= 1 MHz
Program
MOV TMOD, #10h ; mode 1 of timer is set
MAIN: MOV TH1, #FFh; Initial value FF is set
171
Dr. N. Karuppiah & Dr. S. Ravivarman
RET
Repeat:
JNB TF1 REPEAT 110 110
CLR TR0 01 01
CLR TF1 01 01
RET 01 01
Total delay is 123
172
Microprocessor and Microcontroller
From the above calculation, the actual delay is 123 Sec. The program is
made to for 110 Sec delay. 13 Sec more than the desired delay is due to overhead
instruction in loop.
4.18 Interrupt
When 8051 executes a program, a signal which suspend the program is
called interrupt. When an interrupt occurs, the Program Counter (PC) loads
with interrupt service routine (ISR) vector. Then the 8051 processor starts to
execute Interrupt Service Routine. On completion of ISR the processor come
back to main program where the break occurred. Interrupt may be generated
from inside 8051 or may come from outside of 8051. Interrupt generated inside
8051 is called internal interrupt and interrupt from outside is called external
interrupt.
There are five interrupts provided in 8051. Three interrupts are internal
interrupts and two are external interrupts. Internal interrupts are Timer0over
flow flag (TF0), Timer1 over flow flag (TF1) and serial port interrupt(R1orT1).
Two external interrupts are INT0 (port 3.2) and INT1(port 3.3).
Each interrupt source causes the program to jump to Interrupt Service
Routine in one of the absolute address of program memory. When an interrupt
occurs, a call is then done to appropriate memory location called ISR vectors.
The list of ISR vectors are as follows,
Interrupt Name ISR Vector Address (in Hex.)
External Interrupt 0(IE0) 0003H
Timer Overflow Flag 0(TF0) 000BH
External Interrupt 1(IE1) 0013H
Timer Overflow Flag 1(TF1) 001BH
Serial Interrupt (T1 or R1) 002BH
Three special function registers IE (Interrupt Enable), IP (Interrupt
Priority) and TCON (Timer Control) are used in 8051 to have a complete
interrupt function.
IE (Interrupt Enable) Special Function Register
7 6 5 4 3 2 1 0
EA ---- ---- ES ET1 EX1 ET0 Ex0
173
Dr. N. Karuppiah & Dr. S. Ravivarman
the Interrupt Service routine even any of the interrupt occurs. The description
of each bit of this register is as follows,
IE.0 EX0 (Enable external interrupt 0). Programming this bit to 1,
external interrupt INT0 raises the external edge flag IE0 in TCON register.
External Interrupt edge flag IE0 causes the CPU to execute Interrupt service
routine.
IE.1 ET0 (Enable timer0 over flow flag). When this bit is set to 1 by
program, it enables the timer over flow flag TF0.
IE.2 EX1 (Enable external interrupt 1). Programming this bit to 1,
external interrupt INT1 raises the external edge flag IE1 in TCON register.
External Interrupt edge flag
IE1 causes the CPU to execute Interrupt service routine.
IE.3 ET1 (Enable timer1 over flow flag). When this bit is set to 1 by
program, it enables the timer over flow flag TF1.
IE.4 ES (Enable serial port interrupt). When this bit is set to 1 by
program, serial reception flag (R1) or serial transmission flag(T1) in SCON
register are enabled.
IE.5, IE.6 They are kept for future use.
IE.7 EA (Enable interrupt bits). Set to 0 by program disables all
interrupts. Should be set to 1 to enable one or all interrupts. IP (Interrupt
Priority) Special Function Register
7 6 5 4 3 2 1 0
---- ---- ---- PS PT1 PX1 PT0 PX0
174
Microprocessor and Microcontroller
INT0/1
IE0/1
IT0 = 1 TCON TCON 000B
ISR
Interrupt Flag
EDGE TRIGGER
Fig 4.28 External Interrupt logical Diagram
Low level signal in INT0/1 set the IE0/1 interrupt flag to 1 when IT0/1 bit
in TCON register is set to 0. If the IT0/1 bit in TCON register is set to 1, a high to
low transition of INT 0/1 set the IE0/1 flag to 1. When the interrupt flag IE0/1
is set, CPU executes interrupt service routine. On completion of ISR the
interrupt flags are cleared and the main program resumed.
Timer flag interrupt TF0 and TF1
Interrupt Flag
EA
000BH
TF0
ISR
ET0
Interrupt Flag
001BH
TF1
ISR
ET1
175
Dr. N. Karuppiah & Dr. S. Ravivarman
EA ES
Serial Interrupt
TI
RI ISR
Fig. 4.30 Serial Interrupt logical diagram
Reset
Reset is also considered as interrupt because whenever active high is
available at (RST)pin 9 of 8051, the CPU stops executing the program and goes
to reset condition. The program counter jumps to 0000H. On reset of CPU, the
special function registers have their reset value. Unlike another interrupt the
program counter cannot resume the main program but the program restarted.
So, it is called non maskable interrupt.
Including reset, there are six interrupts available in 8051.
Program using interrupts
Program to get continuously 8-bit data from port 0 and send to port2, while
creating square wave of 200 micro-sec on port1 pin 1. Crystal frequency is
12Mhz.
ORG 0000H
LJMP HERE
.ORG 000BH ; timer interrupt vector
CPL P1.1
RETI
.ORG 0030H; Main program started from this address
176
Microprocessor and Microcontroller
Back:
MOV A, P0 ; port 0 data load to A register
MOV P2, A ; contents of A reg. moved to P1
SJMP Back
From the above program, the main program stored in ROM location 0030
onwards. Timer interrupt vector is 000BH. Timer 0 is set to mode 2(Auto
reload). MOV P0, #0FFH makes Port P0 as input port by writing all 1s to Port 0
latch buffers.
Since 12MHz crystal is used, one count required 1micro-sec.To get a 200
micro-secdelay 200 counts required by timer. The initial value to load the timer
is FFH - C8H (C8H is equivalent to 200) = 37. One extra clock pulse to be added
for roll over to FFH to 00H.
i.e., 37 + 1 = 38.
Interrupt enable register IE is loaded with 82H to enable EA bit and ET0 bit.
Master interrupt enable bit and timer interrupt enable bits are set.
SETB TR0 starts the timer and the subroutine BACK keep transferring data
from P0 to P2 via Accumulator.
Whenever the timer 0 rolls over, the timer flag TF0 set to 1 and enable
timer interrupt. The CPU suspend the BACK subroutine and execute timer ISR
loaded at 000BH. The CPL P1.1 toggles the previous state of P1.1. RETI clears
the timer flag and the program is resumed.
Subroutine HERE keep generating 200 Sec square pulses and subroutine
BACK keep transferring data from P0 to P2 till reset is pressed.
177
Dr. N. Karuppiah & Dr. S. Ravivarman
Baud rate
Baud rate means number of bits per second. The common baud rates used
by digital serial communication are 1200, 2400, 4800, 9600 and 19200. To
achieve these baud rate 11.0592MHz crystal is used.
Mode 0
Timer
Mode 1
Baud Rate
SBUF
Since we required different baud rate depend upon the circuit connected to
8051, system clock frequency alone cannot be used. To achieve different baud
rate timers are used. 8051 employs UART (Universal Asynchronous Receive
and Transmit) to determine the baud rate. The clock frequency in 8051 is 1/12
of crystal frequency. i.e. 11.0592/12 = 921.58Khz. The UART divides this
178
Microprocessor and Microcontroller
SCON.0 (RI)Receive interrupt flag. This interrupt flag is set to 1 when SBUF
receives the stop bit. RI flag should be cleared by the programmer.
SCON.1 TI (Transmit interrupt flag). This flag is raised to 1 when SBUF
transmits stop bit. This flag also not cleared by ISR and to be cleared by the
programmer.
SCON.2 RB8(Received bit 8). This bit is not used in mode0. in mode 1 the
stop bit is set this bit and in mode 2 & 3 bit 8.
SCON.3 TB8(Transmit bit 8). This bit is not used in mode 0&1. In mode 2&3
this bit is set/cleared by the programmer when the bit 8 is transmitted.
179
Dr. N. Karuppiah & Dr. S. Ravivarman
SCON.4 REN (Receive Enable). Set to 1 this bit enables the serial reception.
To disable reception, set to 0.
SCON.5 SM2 (Multi processor communication bit): This bit is used for multi-
processor communications in mode 2 & 3.
SCON.6 & SCON.7 SM0 & SM1(Serial mode bits): Set/Cleared to select serial
mode. The selection of modes are as follows,
PCON Special function register is not bit addressable. The functions of each
bit in PCON register is as follows,
PCON.0 - (IDL)Idle mode bit.
PCON.1- (PD)Power down bit. This bit is used as handshaking signal to
other processors connected to 8051. Set to 1 by program to enter power down
configuration for CHMOS processors.
PCON.2 - (GFO) General purpose user flag 0. As per requirement of the
programmer, this bit can be either set or cleared in program.
PCON.3 - (GF1) General purpose user flag 1. As per requirement of the
programmer, this bit can be either set or cleared in program.
PCON.4 - 6 Not used
PCON.7 - (SMOD) Serial mode baud rate bit. Set to 0 by program to use the
baud rate set by timer 1. Set to 1 by program the timer 1 baud rate is doubled
for mode 1,2 and 3.
180
Microprocessor and Microcontroller
Sto D7 D0 Star
0 1 0 0 0 0 0 1 Mar
SPACE p
Bit LSB t k
Bit
Fig. 4.34 Mode 0 timing
Mode 0 uses SBUF as an 8-bit shift register that transmits and receives data
on port 3.0, while using port 3.1 to output the shift clock. Fig. 5. shows the
timing for the rate transmission and reception of data of a character. When data
is written into SBUF, the LSB is transmitted first. Data is transmitted with the
speed of baud rate.
The receiver is enabled when bit 5 in SCON(REN) is enabled. At the end of
reception interrupt flat RI will set. Mode 0 is the only mode that controls when
reception can take place. Enabling reception also enables the clock pulses that
shift the received data into the receiver.
MODE 2 and 3
Idle State Idle State
Start Bit Stop Bit
1 23456789
181
Dr. N. Karuppiah & Dr. S. Ravivarman
These modes are identical except for the baud rate. Mode 2 uses a baud rate
of f/32 if SMOD is cleared or f/64 if SMOD is set. Baud rate for mode 3 are
programmable using the overflows of timer1 exactly as for data mode1.
Data transmission using mode 2 and 3 features 11 bits per character as
shown fig. 4. A data begins with a start bit, and the 10th bit of data is
programmable bit followed by stop bit. The 10th bit is programmed in SCON.3.
182