1. UNIT-2
8051 Timers, Interrupts and Serial Communication
Prof. M. R. Raktate
Lecturer
E-mail : mrraktatemk@sanjivani.org.in
Contact No: 9325009155
Sanjivani Rural Education Society’s
Sanjivani K. B. P. Polytechnic, Kopargaon-423603
With NBA ACCREDIATED programs , Approved by AICTE, New Delhi,
Recognized by Govt. of Maharashtra, Affiliated to Maharashtra State Board of Technical Education, Mumbai,
ISO 9001:2015 Certified Institute
Department of Mechatronics
2. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
Contents
2.1 Timers/Counters: SFRs-TMOD, TCON, Timer/Counter Modes.
2.2 C programs for Timers/Counter.
2.3 Interrupts: Polling and Interrupts, IE and IP SFR, Simple C program on
interrupts.
2.4 Serial Communication: SFRs- SCON, SBUF, Modes of serial
communication.
2.5 8051 C programs on serial communication.
2.6 Serial Communication standard RS232, DB9 pin function, interfacing of
8051 microcontroller with MAX 232.
3. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
8051 Timers/Counters
• There are many applications which require an accurate delay to be used.
• To generate accurate delay 8051 provides Timers/Counters.
• When used as timer, this section generates delay while when used as
counter, it counts the pulses applied on pins To and T1 of port 3.
• There are total two timers in 8051 of size 16 bit. i.e. timer 0 and timer 1
which are independent of each other and may be used separately.
4. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
Timer/Counter Registers
• The timer section itself has some registers which are:
TH/TL (Timer High/ Timer Low):
• This pair forms the timer/counter. There are two such type of pairs for
timer 0 and timer 1 which are named as TH0 and TL0 for timer 0 and
similarly TH1 and TL1 for timer 1.
• Each register is 8 bits so a pair forms a 16-bit timer. While used as timer,
these registers hold the starting point of timer whereas in counter, it
contains the final count.
5. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
• Above figure shows the two 16 bits timer registers Timer 0 and
Timer 1 register respectively. These are special function registers
with addresses as follows:
• THO= 8CH
• TL0= 8AH
• TH1= 8DH
• TL1= 8BH
6. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
TMOD Register
• TMOD is timer mode register. As the name indicates, it is used to select the
mode of timer.
• It is an 8-bit register with address 89H, on reset it holds the value 00H.
• Both the timer’s timer 0 and timer 1 use the same register.
• The lower 4-bit are used for timer 0 and upper 4-bit are used for timer 1.
• In each case the lower 2 bits are used to set the timer mode and upper 2 bits
are to specify the operation.
7. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
TMOD Register
M1, M0:
M0 and M1 are the bits used to select the timer mode. There are 3
modes available in timer.
M1 M0 Mode Description
0 0 0 13-bit timer mode. (8-bit timer/counter THx with TLx as 5-bit prescaler)
0 1 1
16-bit timer mode. 1-bit timer/counters THx and TLx are cascaded, there is no
prescaler.
1 0 2
8-bit auto reload. (8-bit auto reload timer/counter, THx holds a value that is to
be reloaded into TLx each time it overflows.
1 1 3 Split timer mode.
8. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
TMOD Register
C/T (Counter/Timer):
• This bit in the TMOD register is used to decide whether the timer is used as
a delay generator or an event counter.
• If C/T = 0, it is used as timer for time delay generation. The clock source for
the time delay is the crystal frequency of the 8051. If C/T = 1, it is used as
counter operation.
9. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
TMOD Register
GATE:
• Both the timers have this GATE bit. Every timer has a means of starting and
stopping. Some timers do by software, some by hardware, and some have
both software and hardware controls.
• The timers in 8051 have both. The start and stop of the timer is controlled
by the way of software by the TR (timer start) bits TR0 and TR1. Thus, TR
bit will start and stop the timer as long as GATE=0.
• The hardware way of starting and stopping the timer by an external source
is achieved by making GATE=1 in TMOD register.
10. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
TCON Register
• TCON is an 8-bit addressable register used to control the activities of timers
like start, stop etc. The address of TCON is 88H.
TF1: Timer 1 flag (TCON.7)
• This flag indicates the overflow of timer 1, when the value in timer 1 (TH1
and TL1) rolls over from the maximum value (e. g. FFFFH in mode 1) to
0000H, this flag is set.
TR1: Timer 1 run control (TCON.6)
• This flag is used to start or stop the timer 1. When made 1, it starts the
timer whereas 0 makes the timer 1 stop.
11. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
TCON Register
TF0: Timer 0 flag (TCON.5)
• This flag indicates the overflow of timer 0, when the value in timer 0 (TH0
and TL0) rolls over from the maximum value (e. g. FFFFH in mode 1) to
0000H, this flag is set.
TR0: Timer 0 run control (TCON.4)
• This flag is used to start or stop the timer 0. When made 1, it starts the
timer whereas 0 makes the timer 0 stop.
IE1: External interrupt 1 edge flag (TCON.3)
• It is set by hardware when external interrupt edge is detected (INT1). It is
cleared by hardware when interrupt is processed.
12. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
TCON Register
IT1: Interrupt 1 type control (TCON.2)
• Is specifies the type of INT1 i. e. edge triggered or level triggered. If it is 0,
interrupt is level triggered and if it is 1, interrupt is edge triggered.
IE0: External interrupt 0 edge flag (TCON.1)
• It is set by hardware when external interrupt edge is detected (INT0). It is
cleared by hardware when interrupt is processed.
IT0: Interrupt 0 type control (TCON.0)
• Is specifies the type of INT0 i. e. edge triggered or level triggered. If it is 0,
interrupt is level triggered and if it is 1, interrupt is edge triggered.
13. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
Timer/Counter Modes
• Depending on application, the timers can be used in any of the four modes.
These modes are selected by TMOD register.
MODE 0 (13 Bit Timer):
14. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
Timer/Counter Modes
• As shown in the figure above, the clock frequency of 8051 is divided by 12 by
internal circuitry. The functions of C/T and GATE can be observed from above
figure. When working as counter, it takes the input from T0 instead of the
oscillator.
• When GATE is 1, the INT0 pin gets connected to timer to facilitate pulse width
measurement. (i. e. either GATE should be 0 or INT0 should be 1)
• TR0 bit from TCON starts or stop the counting.
• In mode 0 all 8 bits of TH and lower 5 bits of TL are used. Upper 3 bits of TL
are indeterminate and should be ignored. So, the total counts in mode 0 are
8192.
• The 13-bit mode was introduced purposely in order to make the timer
compatible with MCS 48 family. This mode works similar for timer 1. Just the
bits are changed like TR1 instead of TR0 etc.
15. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
Timer/Counter Modes
MODE 1 (16 Bit Timer):
16. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
Timer/Counter Modes
• The structure of mode 1 is similar to mode 0 except it uses all the 8- bits of TL
along with 8 bits of TH. So, the total count in mode 1is 65536.
• As shown in the figure above, the clock frequency of 8051 is divided by 12 by
internal circuitry. The functions of C/T and GATE can be observed from above
figure. When working as counter, it takes the input from T0 instead of the
oscillator.
• When GATE is 1, the INT0 pin gets connected to timer to facilitate pulse width
measurement. (i. e. either GATE should be 0 or INT0 should be 1)
• TR0 bit from TCON starts or stop the counting. The timer is started and
stopped in the same way of mode 0.
• In mode 1 all 8 bits of TH and all the 8 bits of TL are used. So, the total counts
in mode 0 are 65535. This mode works similar for timer 1. Just the bits are
changed like TR1 instead of TR0 etc.
17. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
Timer/Counter Modes
MODE 2 (8 Bit Auto Reload):
18. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
Timer/Counter Modes
• The hardware configuration of mode 2 is shown in the figure above. As the
name implies, it uses only 8 bits of TL whereas TH holds the starting point
of the timer.
• When the timer rolls over from FFH to 00H, it not only sets the timer
overflow flag but also reloads the value from TH to TL. This mode is used
for baud rate generation in serial communication. Also, it is used for
applications where delay is small and repeated, e. g. Square wave
generation.
19. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
Timer/Counter Modes
MODE 3 (Split Timer):
20. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
Timer/Counter Modes
• The mode 3 splits the timer 0 in two parts of 8 bit each whereas timer 1
simply holds its value. Here TL0 and TH0 are treated as separate 8-bit
timers.
• From hardware configuration, it can be observed that the control bits and
flags of timer 0 i. e. C/T, GATE, INT0, TR0 and TF0 are dedicated for TL0
whereas the control bits and flags of timer 1are dedicated for TH0. Thus,
TH0 now controls the timer 1interrupt.
• Mode 3 provides application requiring for an extra 8-bit timer. So, with timer
0 in mode 3, 8051 looks like having 3 timers. While timer 0 in mode 3, 8051
looks like having 3 timers.
• While timer 0 in mode 3, timer 1 may be switched on or off by switching it
out of and into its own mode 3 or can be still used for baud rate generation
or in any application not requiring interrupt.
21. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
Delay Generation
1. In any mode, the timer counts in increasing manner i. e. from starting
point upto final count. (FFH or FFFFH).
2. Programmer cannot change the end points but the starting point
depending upon the required delay.
3. The clock frequency gets divided by 12 and then given to the timer
section, i. e. 11.0592MHz/ 12=921.6KHz
4. So, the frequency of counter is f=921.6KHz.
5. Time required for counter to increment by 1 is (1/f) i. e. 1.085µsec
(microsecond).
6. So, to calculate the required delay, we need to calculate the total count
which is multiplied by above value with the required delay.
22. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
Delay Generation
7. The timer programming in either languages may be done in two ways. i. e.
without interrupt and with interrupt.
8. In either way timer need to started manually. The overflow needs to be
monitored through TF in without interrupt whereas monitoring is not
required in interrupt enabled programming.
23. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
Summary Delay Generation
1. Timer frequency (fT) = fosc / 12 = 921.6KHz
2. T= 1/ fT = 1/921.6K = 1.085 µsec.
3. Divide the required delay by 1.085 µsec to get ‘n’ (Delay/1.085 µsec).
4. Subtract the value from end point i. e. (65536- n) or (8192-n) depending on
mode.
If mode 0 is used then use 8192-n.
If mode 1 is used then use 65536-n.
If mode 2 and 3 are used then use 256-n.
5. Convert the value from decimal to hex to get yyxx H.
6. Load the xx in TL0 and yy in TH0 if timer 0 is used.
7. Start the timer and observe TF0.
24. Department of Mechatronics, Sanjivani K. B. P. Polytechnic, Kopargaon
1. Write a program to toggle all bits of port P1 continuously with some delay in between. Use
timer 0, 16-bit mode to generate delay.
2. Write a program to toggle only bit P1.5 continuously every 50ms. Use timer 0, mode 1 (16-bit)
to create the delay.
3. Write the program to toggle all the bits of P2 continuously after every 500ms. Use timer 1,
mode 1 to create the delay.
4. Write a program to toggle only pin P1.5 continuously every 250ms. Use timer 0, mode 2 (8-bit
auto reload) to create the delay.
5. Write a program to create a frequency of 2500 Hz on pin P2.7. Use timer 1, mode 2 to create
the delay.