Module3 timers notes
Module3 timers notes
MODULE3
Timers/Counters & Serial port programming:
Basics of Timers & Counters, Data types & Time delay in the 8051 using C, Programming 8051 Timers,
Mode 1 & Mode 2 Programming, Counter Programming (Assembly Language only). (Text book 2- 3.4,
Text book 1- 7.1, 9.1,9.2)
Basics of Serial Communication, 8051 Connection to RS232, Programming the 8051 to transfer data
serially & to receive data serially using C. (Text book 2- 3.5, Text book 1- 10.1,10.2,10.3 except assembly
language programs, 10.5)
TEXT BOOKS
1. The “8051 Microcontroller and Embedded Systems – Using Assembly and C”, Muhammad Ali Mazidi and
Janice Gillespie Mazidi and Rollind. Mckinlay; Phi, 2006 / Pearson, 2006.
2. “The 8051 Microcontroller”, Kenneth j. Ayala, 3rd edition, Thomson/Cengage Learning.
3. “Programming And Customizing The 8051 Microcontroller”.,Myke Predko Tata Mc Graw-Hill Edition
1999 (reprint 2003).
̅=0
C/T
̅=1
C/T
1|P a ge
If the oscillator clock pulses to reach the timer, the C/T ̅ bit in the TMOD register must be set to 0(Timer
operation). Bit TRX in the TCON register must be set to 1(Timer run), & the gate bit in the TMOD register
must be 0, or external pin ̅̅̅̅̅̅̅
INTX must be a 1.
Timer modes of operation:
In Intel 8051, there are two 16-bit timer registers. These registers are known as Timer0 and Timer1. The timer
registers can be used in two modes. These modes are Timer mode and the Counter mode. The only difference
between these two modes is the source for incrementing the timer registers.
Timer Mode
In the timer mode, the internal machine cycles are counted. So this register is incremented in each machine
cycle. So when the clock frequency is 12MHz, then the timer register is incremented in each millisecond. In
this mode it ignores the external timer input pin.
Counter Mode
In the counter mode, the external events are counted. In this mode, the timer register is incremented for each
1 to 0 transition of the external input pin (P3.4 & P3.5). This type of transitions is treated as events. The
external input pins are sampled once in each machine cycle, and to determine the 1or 0 transitions, another
machine cycle will be needed. So in this mode, at least two machine cycles are needed. When the frequency
is 12MHz, then the maximum count frequency will be 12MHz / (12d x 2m/c cycles) = 500KHz. So for event
counting the time duration is 2 µs.
There are four different modes of the Timer or Counter. The Mode 0 to Mode 2 are for both of the
Timer/Counter. Mode 3 has a different meaning for each timer register. There is a register called TMOD. This
register can be programmed to configure these timers or counters.
The Serial port is used for serial communication in mode 1 and 3. Timer1 is used for generating the baud rate.
So only Timer0 is available for timer or counter operations.
TMOD Register (8-bit register)
TMOD (Timer Mode) is an SFR. The address of this register is 89H. This is not bit-addressable.
In the following table, we will see the bit details and their different operations for high or low value.
Example of programming:
To configure the Timer0 as 16-bit event counter and Timer1 as 8-bit auto reload counter, we can use the bit
pattern 0 0 1 0 0 1 0 1. It is equivalent to 25H. If we want to program the TMOD register with this bit pattern,
we can use this instruction:
MOV TMOD, #25H
The above instruction is executed, then the timer/counter will be controlled by the software.
To configure the system as hardware controlled mode, then the gate bits will be 1. So the bit patterns will be
1 0 1 0 1 1 0 1 = ADH
we can use this instruction:
MOV TMOD, #0ADH
2|P a ge
TCON Register:
3|P a ge
Mode 0 of Timer/Counter
The Mode 0 operation is the 8-bit timer or counter with a 5-bit pre-scaler. So it is a 13-bit timer/counter. It
uses 5 bits of TL0 or TL1 and all of the 8-bits of TH0 or TH1.
Here Timer0 is shown in mode 0.
Timer1 can also be used in the
same manner.
In this example the Timer1is selected, in this case, every 32(2^5) event for counter operations or 32 machine
cycles for timer operation, the TH1 register will be incremented by 1. When the TH1overflows from FFH to
00H, then the TF1 of TCON register will be high, and it stops the timer/counter. So for an example, we can
say that if the TH1 is holding F0H, and it is in timer mode, then TF1will be high after 10H * 32 = 512 machine
cycles.
MOV TMOD, #00H
MOV TH1, #0F0H
MOV IE, #88H
SETB TR1
In the above program, the Timer1 is configured as timer mode 0. In this case Gate = 0. Then the TH1 will be
loaded with F0H, then enable the Timer1 interrupt. At last set the TR1 of TCON register, and start the timer.
Mode 1 of Timer/Counter
The Mode 1 operation is the 16-bit timer or counter. In the following diagram, we are using Mode 1 for
Timer0.
Here Timer0 is shown in mode 1.
Timer1 can also be used in the same
manner.
In this case every event for counter operations or machine cycles for timer operation, the TH0– TL0 register-
pair will be incremented by 1. When the register pair overflows from FFFFH to 0000H, then the TF0 of TCON
register will be high, and it stops the timer/counter. So for an example, we can say that if the TH0 – TL0
register pair is holding FFF0H, and it is in timer mode, then TF0 will be high after 10H = 16 machine cycles.
When the clock frequency is 12MHz, then the following instructions generate an interrupt 16 µs after Timer0
starts running.
MOV TMOD, #01H 12MHz/12 = 1MHz.
MOV TL0, #0F0H 1MHz 1μseconds
MOV TH0, #0FFH 16 counts * 1μseconds = 16 μseconds
MOV IE, #82H
SETB TR0
In the above program, the Timer0 is configured as timer mode 1. In this case Gate = 0. Then the TL0 will be
loaded with F0H and TH0 is loaded with FFH, then enable the Timer0 interrupt. At last set the TR0 of TCON
register, and start the timer.
Mode 2 of Timer/Counter
The Mode 2 operation is the 8-bit auto reload timer or counter. In the following diagram, we are using Mode
2 for Timer1.
In this case every event for counter operations or machine cycles for timer operation, the TL1register will be
incremented by 1. When the register pair overflows from FFH to 00H, then the TF1 of TCON register will be
high, also theTL1 will be reloaded with the content of TH1 and starts the operation again.
4|P a ge
So for an example, we can say that if the TH1 and TL1 register both are holding F0H and it is in timer mode,
then TF1 will be high after 10H= 16 machine cycles. When the clock frequency is 12MHz this happens after
16 µs, then the following instructions generate an interrupt once every 16 µs after Timer1 starts running.
MOV TMOD, #20H
MOV TL1, #0F0H
MOV TH1, #0F0H
MOV IE, #88H
SETB TR1
In the above program, the Timer1 is configured as timer mode 2. In this case Gate = 0. Then the TL1 and TH1
are loaded with F0H. then enable the Timer1 interrupt. At last set the TR1 of TCON register, and start the
timer.
Timer1 in mode 2 generates the desired baud rate when the serial port is working on Mode 1 or 3.
Mode 3 of Timer/Counter
Mode 3 is different for Timer0 and Timer1. When the Timer0 is working in mode 3, the TL0 will be used as
an 8-bit timer/counter. It will be controlled by the standard Timer0 control bits, T0 and INT0 inputs. The TH0
is used as an 8-bit timer but not the counter. This is controlled by Timer1 Control bit TR1. When the TH0
overflows from FFH to 00H, then TF1 is set to 1. In the following diagram, we can Timer0 in Mode 3.
When the Timer1 is working in Mode 3, it simply holds the count but does not run. When Timer0 is in mode
3, the Timer1 is configured in one of the mode 0, 1 and 2. In this case, the Timer1 cannot interrupt the
microcontroller. When the TF1 is used by TH0 timer, the Timer1 is used as Baud Rate Generator.
The meaning of gate bit in Timer0 and Timer1 for mode 3 is as follows
It controls the running of 8-bit timer/counter TL0 as like Mode 0, 1, or 2. The running of TH0 is controlled
by TR1 bit only. So the gate bit in this mode for Timer0 has no specific role.
The mode 3 is present for applications requiring an extra 8-bit timer/counter. In Mode 3 of Timer0, the 8051
has three timers. One 8-bit timer by TH0, another8-bit timer/counter by TL0, and one 16-bit timer/counter by
Timer1.
If the Timer0 is in mode3, and Timer1 is working on either 0, 1 or 2, then the gun control of the Timer1 is
activated when the gate bit is low or INT1 is high. The run control is deactivated when the gate is high
and INT1 is low.
Timer/Counter Programming (Assembly language only)
(Programming 8051 Timers, Counter programming):
First be perfect in TMOD & TCON registers. They are SFRs.
TMOD Register – SFR with address 89h
5|P a ge
TCON Register – SFR with address 88h
Problem statement:
1) Write an assembly language program for 8051 (ALP from now onwards) to generate a square wave
with 50% duty cycle on P1.5 bit. XTAL frequency is 11.0592MHz.
Steps of programming:
1) Load the TMOD with a bit pattern to select which timer we are using, which mode & other bit
settings. Here we have selected Timer0 with mode1.
2) Load TH0 & TL0 with a value based on the calculation.
3) Change the bit P1.5 from 1 to 0 or 0 to1.
4) Call a delay program to generate a delay depending on the ON time or OFF time of the square wave
to be generated
Delay:
a) Start the Timer with setting the respective TR bit. SETB TR0.
b) Keep monitoring the respective TF flag to check the completion of the count to check overflow.
c) If no overflow (TF = 0), then repeat (b)
d) If overflow (TF = 1), stop the timer (TR = 0) & clear TF flag
e) Go back to step (3).
Calculation:
XTAL frequency = 11.0592 MHz
8051 clock frequency = 11.0592MHz ÷ 12 = 921.6KHz.
Clock period = 1 ÷ 921.6KHz = 1.085μsecs.
Count = FFFFh – FFF2h + 1 = 14.
Delay generated = counts * clock period = 14 * 1.085μsecs = 15.19μsecs.
Square wave generated will be with ON period 15.19μsecs and OFF period 15.19μsecs.
Total period of the square wave = (ON + OFF) periods = 30.38μsecs.
6|P a ge
2) For the assembly language program for 8051 given above, to generate a square wave with 50% duty
cycle on P1.5 bit. XTAL frequency is 11.0592MHz. Here consider also the overheads involved in
the execution of the instructions in the loop & make the calculations for T generated.
ALP Clock
cycles
2
2
1
2
2
1
14
1
1
2
28
9|P a ge
Mode2 programming: -
10 | P a g e
Counter Programming.
Recall what is counter mode of Timer/Counter of 8051.
When C/T ̅ = 1, then the Timer 1/0 will be selected for counter operation, during which the Timer/counter
will count the external clock pulses from pin 14(p3.4) & 15(p3.5) by the T1 & T0 respectively.
p3.4 & p3.5 have to be set (to configure them as input pins to allow the clock pulses to be counted).
Bcos when 805 is powered-up, all ports will be set as output ports.
All operations of the counter/s is same as timers except the clock pulses which are counted.
11 | P a g e
Sample pgm to illustrate programming Timer/Counter in counter0 mode2 to count the external
events(external clock pulses) & display the count status in TL0 in port2.
MOV TMOD, 06H ; timer0 in counter mode1(8-bit auto-reload operation),
MOV TH0, #00H
SETB P3.4 ; make p3.4 as input pin to allow in the external clock pulses.
REPEAT; SETB TR0 ; start counting
BACK; MOV A, TL0 ;
MOV P2, A ; Display the count value in TL0 on port2
JNB TF0, BACK ; continue counting if TLO is not overflown
CLR TR0
CLR TF0
SJMP REPEAT
Pgm to count the pulses of an input signal using Timer0. Also calculate the number of pulses received
in 1 second (Frequency of the received pulse waveform. Use Timer1 to count 1 second.(XTAL frequency
is 22MHz).
RPT: MOV TMOD, #00010101B ; T1 in mode1 to count 1 second & T0 in mode1 to count
external pulses on p3.4
SETB P3.4 ; make p3.4 pin as input pin
MOV TL0, #00H
MOV TH0, #00H
SETB TR0
MOV R0, #1CH ; 1CH = 28d repetition count to generate 1 second delay as
AGAIN: MOV TL1, #00H illustrated in the previous sections.
MOV TH1, #00H
SETB TR1 to generate a delay of 1 second, to count the number
BACK: JNB TF1, BACK of clock pulses in 1 second to get frequency of the
CLR TF1 pulse waveform. Timer1 is used here.
CLR TR1
DJNZ R0, AGAIN
MOV A, TL0 at the end of 1 second, the values in TH0-TL0 combination gives
MOV P2, A the number of pulses that were received in 1 second. This also gives the
MOV A, TH0 frequency of the signal, i.e., pulses in 1 second.
MOV P1, A
SJMP RPT
END
12 | P a g e