0% found this document useful (0 votes)
240 views

Module 4 MC 18EC46 Timer Counter

Timers in 8051 microcontrollers can be used to generate pulses and time delays. The 8051 has two timers - Timer 0 and Timer 1. These timers operate in different modes like mode 0, 1, 2 to function as a counter, timer or baud rate generator. The TMOD and TCON registers are used to configure the timer modes and controls. Programming timers involves calculating initial count values based on required delay, selecting the timer mode, loading TH and TL registers, starting/stopping the timer using TR bits and monitoring TF flag. A sample program is provided to generate a 2 kHz square wave on P1.5 pin using Timer 1 with a 12 MHz crystal oscillator.

Uploaded by

kirthi bharadwaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
240 views

Module 4 MC 18EC46 Timer Counter

Timers in 8051 microcontrollers can be used to generate pulses and time delays. The 8051 has two timers - Timer 0 and Timer 1. These timers operate in different modes like mode 0, 1, 2 to function as a counter, timer or baud rate generator. The TMOD and TCON registers are used to configure the timer modes and controls. Programming timers involves calculating initial count values based on required delay, selecting the timer mode, loading TH and TL registers, starting/stopping the timer using TR bits and monitoring TF flag. A sample program is provided to generate a 2 kHz square wave on P1.5 pin using Timer 1 with a 12 MHz crystal oscillator.

Uploaded by

kirthi bharadwaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

B.N.M.

Institute of Technology
Approved by AICTE, Affiliated to VTU, Accredited as Grade A Institution by NAAC.
All UG branches – CSE, ECE, EEE, ISE &Mech.E Accredited by NBA for academic years 2018-19 to 2020-21 & valid upto 30.06.2021
Post box no. 7087, 27th cross, 12th Main, Banashankari 2nd Stage, Bengaluru- 560070, INDIA
Ph: 91-80- 26711780/81/82 Email: [email protected], www. bnmit.org

Department: Electronics and Communication Engineering


Course Name: Microcontroller Course Code: 18EC46
Sem: IV

Module 4 - 8051 Timers and Serial Port


Syllabus
8051 Timers and Counters – Operation and Assembly language programming to generate a pulse
using
Mode-1 and a square wave using Mode-2 on a port pin.

Q1. What are timers/Counters in 8051 Microcontroller and its applications.


Timers/Counters are used generally for
 Time reference
 Creating delay
 Wave form properties measurement
 Periodic interrupt generation
 Waveform generation
8051 has two timers, Timer 0 and Timer 1.

Timer in 8051 is used as timer, counter and baud rate generator. Timer always
counts up irrespective of whether it is used as timer, counter, or baud rate generator:
Timer is always incremented by the microcontroller. The time taken to count one
digit up is based on master clock frequency.
If 8051 CLK=11.0592 MHz,
Timer Clock frequency = CLK/12 = 0.9216 MHz = 921.6 KHz
Timer Clock Period = 1/921.6KHz = 1.085 µS
This indicates that one increment in count will take 1.085 micro second.
The two timers in 8051 share two SFRs (TMOD and TCON) which control the
timers, and each timer also has two SFRs dedicated solely to itself (TH0/TL0 and
TH1/TL1).
The following are timer related SFRs in 8051.

SFR Name Description SFR


Address
TH0 Timer 0 High Byte 8Ch
TL0 Timer 0 Low Byte 8Ah
TH1 Timer 1 High Byte 8Dh
TL1 Timer 1 Low Byte 8Bh
TCON Timer Control 88h
TMOD Timer Mode 89h

Q2. Draw and explain the bit patterns of TMOD register of 8051 Microcontroller.
TMOD (timer mode) register:
Both timers 0 and 1 use the same register, called TMOD, to set the various timer operation modes.
TMOD is an 8-bit register in which the lower 4 bits are set aside for Timer 0 and the upper 4 bits
for Timer 1. In each case, the lower 2 bits are used to set the timer mode and the upper 2 bits to
specify the operation. These options are discussed next.
M0 and Ml select the timer mode. As shown in Figure .There are three modes: 0, 1, and 2. Mode 0
is a 13-bit timer, mode 1 is a 16-bit timer, and mode 2 is an 8-bit timer. We will concentrate on
modes 1 and 2 since they are the ones used most widely. We will soon describe the characteristics
of these modes, after describing the rest of the 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 a timer for time delay generation. The clock source for the
time delay is the crystal frequency of the 8051. This section is concerned with this choice. The
timer’s use as an event counter is discussed in the next section.

GATE
The other bit of the TMOD register is the GATE bit. Notice in the TMOD register of Figure 9-3
that both Timers 0 and 1 have the GATE bit. What is its purpose? Every timer has a means of
starting and stopping. Some timers do this by software, some by hardware, and some have both
software and hardware controls. The timers in the 8051 have both. The start and stop of the timer
are controlled by way of software by the TR (timer start) bits TRO and TR1. This is achieved by
the instructions “SETB TR1″ and “CLR TR1″ for Timer 1, and “SETB TRO” and “CLR TRO” for
Timer 0. The SETB instruction starts it, and it is stopped by the CLR instruction.

These instructions start and stop the timers as long as GATE = 0 in the TMOD register. The
hardware way of starting and stopping the timer by an external source is achieved by making
GATE = 1 in the TMOD register. However, to avoid further confusion for now, we will make
GATE = 0, meaning that no external hardware is needed to start and stop the timers. In using
software to start and stop the timer where GATE = 0. All we need are the instructions “SETB
TRx” and “CLR TRx”.

Q3 . Draw and explain the bit patterns of TCON register of 8051 Microcontroller.
Q4. Draw and explain the Timer/ Counter Control Logic of 8051 Microcontroller.

Q5. Explain different timer modes in 8051 Microcontroller?

Timers can operate in four different modes. They are as follows


Timer Mode-0: In this mode, the timer is used as a 13-bit UP counter as follows.

Fig. Operation of Timer on 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 1's to
all 0'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.

Fig: Operation of Timer in Mode 1


Timer Mode-2: (Auto-Reload 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.

Fig: Operation of Timer in Mode 2


Timer Mode-3: 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: 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).

Q6. Explain the Steps required to program timers of 8051 in different modes.
PROGRAMMING 8051 TIMERS IN ASSEMBLY
In order to program 8051 timers, it is important to know the calculation of
initial count value to be stored in the timer register. The calculations are as
follows.
In any mode, Timer Clock period = 1/Timer Clock Frequency.
= 1/(Master Clock Frequency/12)

a. Mode 1 (16 bit timer/counter)


Value to be loaded in decimal = 65536 – (Delay required/Timer
clock period)
Convert the answer into hexadecimal and load onto THx and TLx
register.
(65536D = FFFFH+1)
b. Mode 0 (13 bit timer/counter)
Value to be loaded in decimal = 8192 – (Delay Required/Timer
clock period) Convert the answer into hexadecimal and load
onto THx and TLx register. (8192D = 1FFFH+1)
c. Mode 2 (8 bit auto reload)
Value to be loaded in decimal = 256 – (Delay Required/Timer
clock period) Convert the answer into hexadecimal and load onto
THx register.
Upon starting the timer this value from THx will be reloaded to
TLx register.
(256D = FFH+1)
Steps for programming timers in 8051

Mode 1:
 Load the TMOD value register indicating which timer (0 or 1) is to be used
and which timer mode is selected.
 Load registers TL and TH with initial count values.
 Start the timer by the instruction “SETB TR0” for timer 0 and “SETB TR1” for timer
1.
 Keep monitoring the timer flag (TF) with the “JNB TFx,target”
instruction to see if it is raised. Get out of the loop when TF becomes
high.
 Stop the timer with the instructions “CLR TR0” or “CLR TR1”, for
timer 0 and timer 1, respectively.
 Clear the TF flag for the next round with the instruction “CLR TF0”
or “CLR TF1”, for timer 0 and timer 1, respectively.
 Go back to step 2 to load TH and TL again.
Mode 0:
The programming techniques mentioned here are also applicable to
counter/timer mode 0. The only difference is in the number of bits of the
initialization value.
Mode 2:
 Load the TMOD value register indicating which timer (0 or 1) is to
be used; select timer mode 2.
 Load TH register with the initial count value. As it is an 8-bit timer,
the valid range is from 00 to FFH.
 Start the timer.
 Keep monitoring the timer flag (TFx) with the “JNB TFx,target”
instruction to see if it is raised. Get out of the loop when TFx goes high.
 Clear the TFx flag.
 Go back to step 4, since mode 2 is auto-reload.

Q7 . Write a program to continuously generate a square wave of 2 kHz frequency on


pin P1.5 using timer 1. Assume the crystal oscillator frequency to be 12 MHz.

To calculate the values to be loaded into the TL and TH registers, look at the following
example

Assume XTAL = 12 MHz, we can use the following steps for finding the TH, TL registers’
values

1. Divide the desired time delay by 1.085 us


2. Perform 65536 – n, where n is the decimal value we got in Step1
3. Convert the result of Step2 to hex, where yyxx is the initial hex value to be loaded into the
timer’s register
4. Set TL = xx and TH = yy

The period of the square wave is T = 1/(2 kHz) = 500 µs.


Each half pulse = 250 µs.
Step 1: The value n for 250 µs is: 250 µs /1 µs = 250
Step 2: 65536 - 250 = (65286) d
Step 3: (65286)d = FF06H.
Step 4: TL = 06H and TH = 0FFH.

MOV TMOD,#10 ;Timer 1, mode 1


AGAIN: MOV TL1,#06H ;TL0 = 06H
MOV TH1,#0FFH ;TH0 = FFH
SETB TR1 ;Start timer 1
BACK: JNB TF1,BACK ;Stay until timer rolls over
CLR TR1 ;Stop timer 1
CPL P1.5 ;Complement P1.5 to
get Hi, Lo
CLR TF1 ;Clear timer flag 1
SJMP AGAIN ;Reload timer

Q8. Write a program segment that uses timer 1 in mode 2 to toggle P1.0 once
whenever the counter reaches a count of 100. Assume the timer clock is taken
from external source P3.5
(T1).
The TMOD value is 60H
The initialization value to be loaded into TH1 is 256 - 100 = 156 = 9CH
MOV TMOD,#60h ;Counter1, mode 2, C/T’= 1
MOV TH1,#9Ch ;Counting 100 pulses
SETB P3.5 ;Make T1 input
SETB TR1 ;Start timer 1
BACK: JNB TF1,BACK ;Keep doing it if TF = 0
CPL P1.0 ;Toggle port bit
CLR TF1 ;Clear timer overflow flag
SJMP BACK ;Keep doing it
Q9. Write an ALP to generate a square wave of 50% duty cycle (with equal
portions high and low) on the PI.5 bit. Timer 0 is used to generate the time
delay. Analyze the program.

Program Analysis:
In the above program notice the following steps.
1. TMOD is loaded for T0 in Mode-1
2. FFF2H is loaded into THO – TLO.
3. P1.5 is toggled for the high and low portions of the pulse.
4. The DELAY subroutine using the timer is called.
5. In the DELAY subroutine, Timer 0 is started by the “SETB TRO” instruction.
1. Timer 0 counts up with the passing of each clock, which is provided by the crystal
oscillator. As the timer counts up, it goes through the states of FFF3, FFF4, FFF5,
FFF6, FFF7, FFF8, FFF9, FFFA, FFFB, and so on until it reaches FFFFH. One more
clock rolls it to 0, raising the timer flag (TFO = 1). At that point, the JNB instruction
falls through.
2. Timer 0 is stopped by the instruction “CLR TRO”. The DELAY subroutine ends,
and the process is repeated.
Notice that to repeat the process, we must reload the TL and TH registers and start the timer
again.
Q10. Find the delay generated by Timer 0 in the following code, using both of the methods of
Do not include the overhead due to instructions.

1. (FFFF-B83E + 1) = 47C2 H= 18370 in decimal and 18370 x 1.085µS= 19.93145


ms.
2. Since TH – TL = B83EH = 47166 (in decimal) we have 65536 – 47166 = 18370.
This means that the timer counts from B83EH to FFFFH.. This plus rolling over to
0 goes through a total of 18370 clock cycles, where each clock is 1.085 µS in dura
tion. Therefore, we have 18370 x 1.085 µS= 19.93145 ms as the width of the pulse.

Q11. Write an ALP to get the largest time delay possible using timer in Mode-1. Find the
delay in ms. In your calculation, exclude the overhead due to the instructions in the loop.
Solution:
To get the largest delay we make TL and TH both 0. This will count up from 0000 to FFFFH and
then roll over to zero.
Making TH and TL both zero means that the timer will count from 0000 to FFFFH, and then roll
over to raise the TF flag. As a result, it goes through a total of 65536 states. Therefore, we have
delay = (65536 – 0) x 1.085 us = 71.1065 ms.

Q12. Assume that XTAL = 11.0592 MHz. What value do we need to load into the timer’s
registers if we want to have a time delay of 5 ms (milliseconds)? Show the program for Timer 0
to create a pulse width of 5 ms on P2.3.
Solution:
Since XTAL = 11.0592 MHz, the counter counts up every 1.085 s. This means that out of many
1.085 us intervals we must make a 5 ms pulse. To get that, we divide one by the other. We need 5
ms /1.085 µs = 4608 clocks. To achieve that we need to load into TL and TH the value 65536 –
4608 = 60928 = (EE00)H. Therefore, we have TH0 = EE and TL0 = 00H.

Q13. Assuming that XTAL = 11.0592 MHz, write a program to generate a square wave of 2
kHz frequency on pin PI .5 using Timer1 in Mode1.

Solution:
This is similar to Q10, except that we must toggle the bit to generate the square wave. Look
at the following steps.
1. T = 1 / f = 1 / 2 kHz = 500 s the period of the square wave.
2. 1/2 of it for the high and low portions of the pulse is 250 s.
3. 250 s / 1.085 s = 230 and 65536 – 230 = 65306. Which in hex is (FF1A) H.
4. TL1 = (1A)H and TH1= (FF)H. all in hex.

Program:
Q14. Assuming XTAL = 11.0592 MHz, write a program to generate a square wave of 50 Hz
frequency on pin P2.3.
Solution:
Look at the following steps.
1. T = 1 / 50 Hz = 20 ms, the period of the square wave.
2. 1/2 of it for the high and low portions of the pulse = 10 ms
3. 10 ms / 1.085 us = 9216 and 65536 – 9216 = 56320 in decimal, and in hex it is (DC00)H.
4. TL = 00 and TH = DC (hex)

Generating a large time delay

As we have seen in the examples so far, the size of the time delay depends on two factors, (a) the
crystal frequency, and (b) the timer’s 16-bit register in mode 1. Both of these factors are beyond
the control of the 8051 programmer. We saw earlier that the largest time delay is achieved by
making both TH and TL zero. What if that is not enough? Then how to achieve large time delays.

Using Windows calculator to find TH and TL. The scientific calculator in Microsoft Windows is a
handy and easy-to-use tool to find the TH and TL values. Assume that we would like to find the
TH, TL values for a time delay that uses 35,000 clocks of 1.085 us. The following steps show the
calculation.
1. Bring up the scientific calculator in MS Windows and select decimal.
2. Enter 35,000.
3. Select hex. This converts 35,000 to hex, which is 88B8H.
4. Select +/- to give -35000 decimal (7748H).
5. The lowest two digits (48) of this hex value are for TL and the next two (77)
are for TH. We ignore all the Fs on the left since our number is 16-bit data.
Q15. Examine the following program and find the time delay in seconds. Exclude the over
head due to the instructions in the loop.

Solution:
TH – TL = 0108H = 264 in decimal and 65536 – 264 = 65272. Now 65272 x 1.085 s =
70.820 ms, and for 200 of them we have 200 x 70.820 ms = 14.164024 seconds.

Q16. Draw the Mode 2 stage timer programming and explain its characteristics. Give the
steps to program timers in Mode2.

The following are the characteristics and operations of mode 2.


1. It is an 8-bit timer; therefore, it allows only values of 00 to FFH to be loaded into the
timer’s register TH.
2. After TH is loaded with the 8-bit value, the 8051 gives a copy of it to TL. Then the timer
must be started. This is done by the instruction “SETB TRO” for Timer 0 and “SETB
TR1 for Timer 1. This is just like mode 1.
3. After the timer is started, it starts to count up by incrementing the TL register. It counts
up until it reaches its limit of FFH. When it rolls over from FFH to 00, it sets high the TF
(timer flag). If we are using Timer 0, TFO goes high; if we are using Timer 1, TF1 is
raised.
4. When the TL register rolls from (FF)H to (00)H and TF is set to 1, TL is reloaded
automatically with the original value kept by the TH register. To repeat the process, we
must simply clear TF and let it go without any need by the programmer to reload the
original value. This makes mode 2 an auto-reload, in contrast with mode 1 in which the
programmer has to reload TH and TL.

It must be emphasized that mode 2 is an 8-bit timer. However, it has an auto-reloading


capability. In auto-reload, TH is loaded with the initial count and a copy of it is given to
TL. This reloading leaves TH unchanged, still holding a copy of the original value. This
mode has many applications, including setting the baud rate in serial communication.
Steps to program in mode 2:
To generate a time delay using the timer’s mode 2, take the following steps.
1. Load the TMOD value register indicating which timer (Timer 0 or Timer 1) is
to be used, and select the timer mode (mode 2).
2. Load the TH registers with the initial count value.
3. Start the timer.
4. Keep monitoring the timer flag (TF) with the “JNB TFx, target” instruction to see
whether it is raised. Get out of the loop when TF goes high.
5. Clear the TF flag.
6. Go back to Step 4, since mode 2 is auto-reload.

Q17 . Assuming that XTAL = 11.0592 MHz. find (a) the frequency of the square wave
generated on pin P 1.0 in the following program, and (b) the smallest frequency
achievable in this program, and the TH value to do that.

Solution:
(a). First notice the target address of SJMP. In mode 2 we do not need to reload TH since
it is auto-reload. Now (256 – 05) x 1.085 us = 251 x 1.085 us = 272.33 s is the
high portion of the pulse. Since it is a 50% duty cycle square wave, the period T is
twice that; as a result T = 2 x 272.33 us = 544.67 us and the frequency = 1.83597
kHz.

(b). To get the smallest frequency, we need the largest T and that is achieved when TH
= 00. In that case, we have T = 2 x 256 x 1.085 us = 555.52 us and the frequency
= 1.8 kHz.
Q18. Find the frequency of a square wave generated on pin P1.0

Q29. Assuming that we are programming the timers for mode 2, find the value (in hex)
loaded into TH for each of the following cases.

You can use the Windows scientific calculator to verify the results provided by the assembler. In
Windows calculator, select decimal and enter 200. Then select hex, then +/- to get the TH value.
Remember that we only use the right two digits and ignore the rest since our data is an 8-bit data.
The following is what we get.

Assemblers and negative values

Since the timer is 8-bit in mode 2, we can let the assembler calculate the value for TH. For
example, in “MOV TH1 ,#-100″, the assembler will calculate the -100 = 9C, and makes TH1 = 9C
in hex. This makes our job easier.
Q20. Find (a) the frequency of the square wave generated in the following code, and (b) the
duty cycle of this wave.

Solution:
For the TH value in mode 2, the conversion is done by the assembler as long as we enter a negative
number. This also makes the calculation easy. Since we are using 150 clocks, we have time for the
DELAY subroutine = 150 x 1.085 s = 162 (as. The high portion of the pulse is twice that of the
low portion (66% duty cycle). Therefore, we have: T = high portion + low portion = 325.5 S +
162.25 S = 488.25 S (is and frequency = 2.048 kHz)

Notice that in many of the time delay calculations we have ignored the clocks caused by the
overhead instructions in the loop. To get a more accurate time delay, and hence frequency, you
need to include them. If you use a digital scope and you don’t get exactly the same frequency as
the one we have calculated, it is because of the overhead associated with those instructions. In this
section, we used the 8051 timer for time delay generation. However, a more powerful and creative
use of these timers is to use them as event counters.

Q21. Assuming that clock pulses are fed into pin T1, write a program for counter 1 in mode 2
to count the pulses and display the state of the TL1 count on P2, which connects to 8
LEDs.

Notice in the above program the role of the instruction SETB P3.5. Since ports are set up for
output when the 8051 is powered up, we make P3.5 an input port by making it high. In other
words, we must configure (set high) the T1 pin (pin P3.5) to allow pulses to be fed into it.

Q22. Write an 8051 C program to toggle all the bits of port P1 continuously with some delay
in between. Use Timer 0, 16-bit mode to generate the delay.

Q23. Write an 8051 C program to toggle all bits of P2 continuously every 500 ms. Use Timer 1, mode
1 to create the delay.
Q24. A switch is connected to pin P1.2. Write an 8051 C program to monitor SW and create
the following frequencies on pin P1.7: SW=0: 500Hz
SW=1: 750Hz, use Timer 0, mode 1 for both of them.

Q25. Write an 8051 C program to toggle only pin P1.5 continuously every 250 ms. Use
Timer 0, mode 2 (8-bit auto-reload) to create the delay.
Q26. Write an 8051 C program to create a frequency of 2500 Hz on pin P2.7. Use Timer 1,

mode 2 to create delay.

Q27. Assume that a 1-Hz external clock is being fed into pin T1 (P3.5). Write a C program
for counter 1 in mode 2 (8-bit auto reload) to count up and display the state of the TL1
count on P1. Start the count at 0H.
Q28. Assume that a 1-Hz external clock is being fed into pin T0 (P3.4). Write a C program for
counter 0 in mode 1 (16-bit) to count the pulses and display the state of the TH0 and TL0
registers on P2 and P1, respectively.
Solution:
#include <reg51.h> void main(void){
T0=1;
TMOD=0x05
; TL0=0
TH0=0;
while (1) { do {
TR0=1;
P1=TL0
;
P2=TH0
;
}
while (TF0==0); TR0=0;
TF0=0;

}
}

You might also like