Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Unit-6 8051 Timer Counter and Programming Technical

The document provides an overview of the 8051 microcontroller's timer and counter functionalities, including associated registers and programming examples in Assembly language and Embedded C. It details the structure and operation of the TMOD and TCON registers, as well as the various modes of operation for the timers. Additionally, it includes review questions and programming examples to illustrate the use of timers in generating time delays and controlling operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Unit-6 8051 Timer Counter and Programming Technical

The document provides an overview of the 8051 microcontroller's timer and counter functionalities, including associated registers and programming examples in Assembly language and Embedded C. It details the structure and operation of the TMOD and TCON registers, as well as the various modes of operation for the timers. Additionally, it includes review questions and programming examples to illustrate the use of timers in generating time delays and controlling operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

9 8051 Timer /Counter

and Programming
Syllabus
Use of counter as timer,
Timer/Counters
operations, Time delay programs
and associated
in Assembly language/registers, Various modes of timer/counter
Embedded C.
Contents
9.1 Timers /Counters and
Associated Registers Winter-15,17,18, 19,
9.2 Various Modes of Timers/ Counter Summer-16, Marks 7

Operations
Winter-11, 15, 17, 18,
9.3 Programming Examples. Summer-16, Marks 7
Winter-08,10,11, 12,13,14,17,
9.4 Short Questions and Summer-08,10, 12,18,19, Marks 4
Answers

(9- 1)
Microprocessors and Microcontrollers 9-2 8051 Timer / Counter and Programming

9.1 Timers / Counters and Associated Registers


GTU : Winter-15,17,18, 19, Summer-16

8051 has two timers, timer 0 and timer 1. Basically both, timer 0 and timer 1are
16-bit registers. Since 8051 is an 8-bit microcontroller, each 16-bit register can be accessed
as low-byte register (TL) and high-byte register (TH). Fig. 9.1.1 shows the timer 0 and
in
timer 1registers. These registers can be accessed like other registers (A, B, RO, R1 etc.)
8051.
Timer 1 register Timer 0 register

THO TLO
TH1 TL1
(8 - bit) (8 - bit) (8 - bit) (8- bit)

Timer control (TCON)


reglster

Timer mode (TMOD)register


Fig. 9.1.1 Timer registers
9.1.1 Structure of TMOD Register
Timer/counter mode control (TMOD) is the special function register in 8051 having
format as shown in Fig. 9.1.2.
(MSB) (LSB)
GATE CT M1 MO GATE CT M1 MO

Timer 1 Timer 0
Fig. 9.1.2 TMOD register
The TMOD register is responsible for configuring the timers for the following
operations :
Select Timer 0 to operate as a counter or timer
Select Timer 1 to operate as a counter or timer
Select the mode in which timer should operate.
MI, MO : These bits select the timer mode. There are four different modes of timer,
mode 0, mode 1, mode 2 and mode 3. All these modes are discussed in the further
section.

M1 MO
Operating mode
8-bit Tiner/Counter "THx" with "TLX's 5-bit prescaler.

TECHNICAL PUBLICATIONS- An up thrust for knowledge


MicroprocessorS and Microcontrollers 9-3 8051 Timer / Counter and Programming

1
16-bit Timer/Counter "THY" with "TLX" are cascaded;
there is no prescaler.
1
8-bit auto-reload Timer/Counter "THX" holds a value
which is to be reloaded into "TLx" each time it
overflows.
1
(Timer 0) TLO is an 8-bit Timer/Counter controlled by
the standard Timer 0 control bits. THO is an 8-bit
timer ony controlled by Timer 1 control bits.
(Timer 1) Timer/Counter 1 stopped.
C/T: This bit is cleared (C/T =0) for selecting 'timer operation and is
set (C/T= 1)
for selecting 'counter' operation.
GATE : Gating control when set. Timer/Counter "x" is enabled only while
is high and "TRx" control bit is set. When cleared Timer "x" is "INTX" pin
enabled whenever "IRX"
control bit is set.

9.1.2 Structure of TCON Register


The Fig. 9.1.3 shows the format for the TCON register of 8051.
(MSB) (LSB)
TF1 TR1 TFO TRO IE1 IT1 IEO ITO

Symbol Position Name and Significance


TF1 TCON.7 Timer 1 Overflow Flag. Set by hardware on timer/counter overflow. Cleared
when interrupt processed.
TRI TCON.6 Timer 1 Run control bit. Set/cleared by software to turn timer/counter on/off.
IFO TCON.5
Timer 0 Overflow Flag. Set by hardware on timer/counter overflow. Cleared
when interrupt processed.
TRO TCON.4 Timer 0 Run control bit. Set/cleared by software to turn timer/counter on/off.
IE1 TCON.3 Interrupt 1 Edge Flag. Set by hardware when external interrupt edge detected.
Cleared when interrupt processed.
IT1 TCON.2 Interrupt 1Type control bit. Set/cleared by software to specify falling edge/low
level triggered external interrupts.
IEO TCON.1 Interrupt 0 Edge Flag. Set by hardware when external interrupt edge detected.
Cleared when interruptprocessed.
ITO TCON.0 Interrupt 0Type control bit. Set/cleared by software to specify faling edge/low
level triggered external interrupts.

Fig. 9.1.3TCON-timerlcounter control/status register

TECHNICAL PUBLICA TIONS-An up thrust for knowledge


MicroprOcessors and Microcontrolers 9-4 8051 Timer / Counter and Programming

The TCON register controls the following timer operations :


Start and stop timer 0 and timer 1
Provides status of timer/counter overflows
Provides status of external interrupts
Configures external interrupts as either low level triggered or falling edge
triggered.
Example 9.1.1 Indicate the effect of following 8051 instructions.
a. MOV TMOD, #00010000B
b. MOV TMOD, #00000001B
c. MOV TMOD,#04
Solution :
Timer 1 in
a. MOV TMOD, #00010000B : The effect of this instruction is to set
mode 1 and Gate = 0 for internal clocking.
b. MOV TMOD, #00000001B : The effect of this instruction is to set Timer 0 in mode
1and Gate = 0 for internal clocking.
. MOV TMOD, #04 : The effect of this instruction is to select timer 0 to run in the
counter mode.

Example 9.1.2 Perform thefollowing operations using bit addressable instructions


4. Start Timer 1

b. Stop Timer 0
Solution : a) SETB TR1 ; starts timer 1 by setting TCON.6 = 1
b) CLR TRO ; stops timer 0 by clearing TCON.4 = 0

Review Questions

1. List the timer registers.


2. Explain the timer structure of 8051 with TMOD register.
3. Explain TMOD and TCON registers with its bit pattern.
4. Give the mode word, (TMOD) and the control word, (TCON) values to perform the following
operations :
i) Timer 0 in auto reload mode i) Timer1 in mode 1.
5. Explain the working of timer in 8051 using the configuration of TCON and TMOD registers.
Explain the difference between timer operation in mode Oand mode 1.
GTU : Winter-15, Marks 7
6. Explain all the bits of TMOD register and hence find the control word for selecting timer 0 in
mode 2. GTU : Summer-16, Marks 7

TECHNICAL PUBLICATIONs - An up thrust for knowledge


MicroproDcessors and Microcontrollers 9-5 8051 Timer / Counter and Programming

7 Briefly describe the bit format of TMOD and TCON


registers. GTU : Winter-17, Marks 4
8. What is the use of gate bit in TMODregister ?
Explain it with suitable diagram.
GTU: Winter-17, Marks 4
9 Show bit structure of TMOD and TCON register.
GTU: Winter-18, Marks 3|
10. Explain TMOD in detail for 8051 microcontroller.
GTU: Winter-19, Marks 3
9.2 Various Modes of Timers / Counter
Operations
GTU: Winter-11, 15, 17, 18, Summer-16
There are four modes of timer, mode 0, mode 1, mode 2
and mode 3. All these
modes and their programming are discussed in this
section. Mode 1 and mode 2 are
widely used, so we will discuss them in detail.
Mode 0

Both Timers in Mode 0 are 8-bit Counters with a


timer is MCS-48 compatible. Fig. 9.2.1 shows the divide-by-32 prescaler. This 13-bit
Mode 0 operation as it applies to
Timer 1. In this mode, the Timer register is
configured as a 13-bit register. As the count
rolls over from all 1s to all Os, it sets the Timer
interrupt flag TF1.
enabled to the Timer when TR1 = 1 and either GATE = 0 The counted input is
or INT1 = 1. (Setting
GATE = 1 allows the Timer to be controlled by
external input INT1, to facilitate pulse
width measurements.) TR1 is a control bit in the Special Function
is in TMOD. Register TCON GATE

OSC +12

CIT-0 TL1 TH1


(5 Bits) (8 Bits) Interrupt
CIT
T1 PIN.

TR1
GATE Control
INT1 PIN

Timer/ counter control logic


Fig. 9.2.1 Timerlcounter 1 mode 0 : 13-bit counter
The
3 bits of13-bit register consists of all 8bits
of TH1 and the lower 5 bits of TL1. The upper
TL1 are indeterminate and should be ignored. Setting the run flag (TR1) does
hot clear the
registers.
TECHNICAL PUBLICATIONS- An up thrust for knowledge
Microprocessors end Microcontrollers 9-6 8051 Timer / Counter and
Programming
Mode 0 operation is the same for Timner 0 as for Timer 1. Substitute TRO, TFO and
INTOfor the corresponding Timer 1signals in Fig. 9.2.1. There are two different GATE
bits, one for Timer 1(TMOD.7) and one for Timer O(TMOD.3).
Mode 1

Both Timers in Mode 1 are 16-bit Counters As the count rolls over from all 1s to all
Os, it sets the Timer interrupt flag TF. The counted input is enabled to the Timer when
TR = 1and either GATE = 0 or INTÍ = 1. (Setting GATE = 1allows the Timer to be
controlled by extermal input NTI, to facilitate pulse width measurements.)
Timer 0 Mode 1 Programming
The Fig. 9.2.2 shows the timer control logic for timer 0 in mode 1.

THO TLO
OSC 12 (8 Bits) (8 Bits) TFO Interrupt
cT=0

TRO
GATE(TMOD.3) Control

INTO PIN

Timer0 control logic


Fig. 9.2.2 Timer 0 in mode 1

A time delay can be generated using mode 1 of the timer 0 using following steps :
1. Load TMOD register indicating timer 0 is used and mode 1 is selected.
7 6 5 4 3 2
TMOD X X X X = 01

2. Load TLOand THO registers with count values.


3. Start the timer by setting TRO bit = 1.
When it
4. Monitor the timer flag (TFO) with the JNB TFO, target address instruction. W
is raised, get out of the loop.
5. Stop the timer by clearing TR0 bit =0 with CLR TRO instruction.
6. Clear TPO flag with CLR TFO instruction.
needed
When start and stop of timer is done using software, no external hardware 0s
for the same. This is illustrated in the Fig. 9.2.3
TECHNICAL PUBLICA TIONS An up thrust for knowledge
Microprocessors and Microcontrollers 9-7 8051 Timer / Counter and Programming

OSC
2 cT=0
THO
(8 Bits)
TLO
(8 Bits) TFO Interrupt

TRO
Timer0 control logic when GATE = 0 and INTO =1

Fig. 9.2.3 Timer 0 in mode 1, no external hardware is used to start and stop timer
Timer 1Mode 1Programming
The Fig. 9.2.4 shows the timer control logic for timer 1 in mode 1.

TH1 TLÍ TE1 Interrupt


OSC 12 (8 Bits) (8 Bits)
CIT-0

TR1
GATE(TMOD.7) Control

NTTPIN
Timer1 control logic
Fig. 9.2.4 Timer 1 in mode 1
timer 1 using following steps :
A time delay can be generated using mode 1 of the
TMOD register indicating timer 1 is used and mode 1 is selected.
I. Load
3 1
6 5 4
7
= 10H
X
TMOD

count values.
2. Load TL1 and THIregisters with
3. Start the timer by setting TR1 bit= 1. target address instruction. When it
the JNB TF1,
flag (TF1) with
* Monitor the timer
is raised, get out of the loop.
0 with CLR TR1l instruction.
bit =
Stop the timer by clearing TR1
instruction.
6.
Clear TF1 flag with CLR TF1 external hardware is needed
software, no
of timer is done using
When start and stop
for same. It is illustrated in the Fig. 9.2.5.
the for knowledge
Microprocessors and Microcontrollers 9-8 8051 Timer / Counter and
Programming

OSC +12 TH1 TL1


TF1
CT=0 (8 Bits) (8 Bits) Interrupt

TR1
Timer1 control logic when GATE 0 and INT1 =1

Fig. 9.2.5 Timer 1in mode 1, no external hardware is used to start and stop timer
Mode 2
Mode 2 configures the Timer register as an 8-bit Counter (TL) with automatic reload.
as shown in Fig. 9.2.6. Overflow from TL only sets TF, but also reloads TL with the
contents of TH, which is preset by software. The reload leaves TH unchanged.
Advantages and disadvantages of Mode 2
The disadvantage of mode 2 operation of 8051 timer is that it has only &-bit timer.
However, it has an auto-reloading capacity. It is an advantage of mode 2
8051 timer. This mode has many applications such as operation of
setting the baud rate in serial communication and so on.
generating periodic waveforms,

Timer 0 Mode 2 Programming


The Fig. 9.2.6 shows the timer control logic for timer 0
in mode 2.

OSC TLO
(8 Bits) TFO Interrupt
CT=0

TRO
GATE(TMOD.3) THO
Control (8 Bits)
INTO PIN

Timer0 control logic


Fig. 9.2.6 Timer 0in mode 2
Atimedelay can be generated using mode 2 of the
1 Load TMOD register timer 0 using following steps :
indicating timer Ois used and mode 2 is selected.
7 6 5 4
3 2 1
TMOD X X
= 02H

TECHNICAL PUBLICATIONS - An up
thrust for knowledge
Microprocessors and Microcontrollers 9-9 8051 Timer / Counter and Programming
2. Load THO register with count value.
3. Start the timer by setting TRO bit =
1.
4. Monitor the timer flag (TFO) with the
JNB TFO. target address instruction. When it
is raised, get out of the loop.
5. Clear the TFO0 flag, with
CLR TFO instruction.
6. Go back to step 4. There is no
need to load THO register again since
auto-reload. Mode 2 1s
When start and stop of timer is done using
for the same. It is illustrated in the Fig. 9.2.7. software, no external hardware is needed

OSC
2 CT=0
TLO
(8 Bits) TFO Interrupt

TRO
Timer0 control logic when GATE = 0 and INT0 =1
THO
(8 Bits)
Fig. 9.2.7 Timer 0 in mode 2, no
external hardware is used to start and stop timer
Timer 1Mode 2 Programming
The Fig. 9.2.8 shows the timer control logic for
timer 1in mode 2.

osc +12 TL1


(8 Bits) TEI Interrupt

TR1
GATE(TMOD.7) Control TH1
(8 Bits)
INTT PIN

Timer1 control logic


Fig. 9.2.8 Timer 1 in mode 2
tne delay can be generated using mode 2 of the timer 1using following steps:
Load TMOD register indicating timer 1 is used and mode 2 is selected.
7 6 5 4 3 2 1

TMOD X X X X = 20H

TECHNICAL PUBLICATIONS - An up thrust for knowledge


Microprocessors and Microcontrollers 9- 10 8051 Timer / Counter and Programming

2. Load THI register with count value.


3. Start the timer by setting TRI bit = 1.
4. Monitor the timer flag (TF1) with the JNB TF1, target address instruction. When it
is raised, get out of the loop.
5. Clear the TF1 flag, with CLR TF1 instruction.
6.Go back to step 4. There is no need to load TH1 register again since Mode 2 is
auto-reload.

When start and stop of timer is done using software, no external hardware is needed
for the same. It is illustrated in the Fig. 9.2.9.

Osc 12 TLI
(8 Bits) Interrupt

TR
Timer1 control logic when GATE =0 and INT1 =1 THI
8 Bits)

Fig. 9.2.9 Timer 1 in mode 2, no external hardware is used to start and stop timer
Mode 3

Timer 1 in Mode 3 simply holds its count. The effect is the same as setting TRI = 0.
Timer 0 in Mode 3 establishes TLO and THO as two separate counters. The logic for
Mode 3 on Timer 0 is shown in Fig. 9.2.10. TLO uses the Timer 0 control bits : C/I,
GATE, TRO, INTO, and TFO. THO is locked into a timer mode (counting machine cycles)
and takes over the use of TRI and TF1 from Timer 1. Thus, THO now controls
the: Timer 1 interrupt.
The Table 9.2.1 summarizes the modes of timers.

Mode Brief Description


Mode 0 13-bit tiner (TL-5-bits and TH-&-bits).
Counter overflow is indicated by time interrupt lag
Mode 1 16-bit timner (TL-&-bits and TH-8-bits)
Rest is same as mode 0.
Mode 2 Automatic reload mode. 8-bit counter (TL-&-bit) overflow from
TL not only sets TT, but also reloads TL with the contents of TH.
Mode 3 Establishes TL and TH as two seperate counters.
Table 9.2.1 Summary of timer modes
TECHNICAL PUBLICATIONS An up thrust for knowledge
MicroproCesSors end Microcontrollers 9-11 8051 Timer /Counter and Programming

OSC 12
1/12f osc

TLO
TF O Interrupt
cr+=1 (8 Bits)
TOPIN

TRO
GATE Control
INTO PIN

THO
1/12f osc (8 Bits) TF 1 Interrupt

Control
TR1

Fig. 9.2.10 Timerlcounter 0mode 3: Two 8-bit counters


9.2.1 Counter Programming
When the timer/counter is used as a counter, the TMOD, TH and TL registers are
used, functioning the same as for the timer studied in the last section.
cT Bit in TMOD Register
As seen earlier, the C/T bit in the TMOD register decides the timer/counter
functioning as a counter or a timer. When C/T bit in the TMOD register is 0, the timer
mode is selected. When timer/counter is used as a timer, the 8051's crystal is used as a
source of the frequency. When C/T bit in the TMOD register is 1, the counter mode is
selected. When timer/counter is used as a counter, it gets its pulses from outside the
8051. The pin P 3.4 (pin number 14) and pin 3.5 (pin number 15) of 8051 are used for
applying pulses counter 0 and counter 1 respectively. These two pins belong to port 3.
The counter counts up for each clock pulse applied at this pin. These pins are called To
(timer 0 clock input) and T1(timer 1 clock input).
Counter 0 in Mode 1
Ine Fig. 9.2.11 (a) shows the block diagram of counter 0 in mode 1 and andthe
Fig 9.2.11 (b) shows the block diagram of counter 0in mode 1 when GATE = 0
INTO =1. Here, counter 0 counts up when the logic signal on pin TO goes from high
level to low level.

up thrust for knowledge


Microprocessors and Microcontrollers 9-12 8051 Timer / Counter and Programming

THO TLO
TFO
TOP3.4) (8 Bits) (8 Bits) Interrupt
cT=1

TRO
Control
GATE(TMOD.3)

INTO PIN
Counter0 control logic
Fig. 9.2.11 (a)

THO TLO
TOP3.4) (8 Bits) TFO Interrupt
CT=t (8 Bits)

TRO

Counter 0control logic when GATE = 0 and INTO =1


Fig. 9.2.11 (b)
To operate counter 0in mode 1 we have to perform following steps :
1. C/T bit in TMOD register (Bit 2) is set to 1 to allow counter mode operation.
2. M1 : MObits (bits 1 : 0) in TMOD register are set to 01 to select mode 1.
3. When Gate bit (Bit 3) in TMOD register is cleared to 0, TRO bit (bit 4 of TCON) is
set to 1 to start the counter.

4. When Gate bit (Bit 3) in TMOD register is set to 1, counter will run only if TRO is
set to 1 and the logic signal on external interrupt pin INTO is high.

7 5 4 3 2 1

TMOD
1 =05H

7 6 5 4 3 2 1

TCON = 10H

Fig. 9.2.12 Counter 0 control register settings for mode 1 operation

TECHNICAL PUBLICATIONS An up thrust for knowledge


Microprocessors and Microcontrollers 9- 13 8051 Timner /Counter and Programming
Counter 1 in Mode 1
The Fig. 9.2.13 (a) shows the
Fig. 9.2.13 (b) shows the block block diagram of counter 1 in mode 1 and the
INTO = 1.
diagram of counter 1in mode 1 when GATE = 0arnd
The operation of counter 1 in mode 1 is same as
counter 0 operation. The ondy
difference is that here registers for counter 1 are
programmed instead of counter 0.
TI(P3.5) TH1 TL1
CT1 (8 Bits) (8 Bits) TF1 Interrupt

TR1
GATE(TMOD.7)
Control
NTI PIN

Counter1 control logic


Fig. 9.2.13 (a)

TP3.5) THI TL1


CIT=1 (8 Bits) (8 Bits) TE1 Interrupt

TR1
Counter1 control logic when GATE=0and INT1 =1
Fig. 9.2.13 (b)

7 6 5 3 2 1

TMOD X =50H

7 5 4 3 2

TCON
1 = 40H

Fig. 9.2.13 (c) Counter 1 control register settings for mode 1 operation
ounter in Mode 2
this mnode, counter is used in auto-reload mode instead of 16-bit counter. Rest of
Peraion is exactly same as that of Mode 1. The Fig. 9.2.14 (a) shows the block
agram of counter 0in Mode 2 and the Fig. 9.2.14 (b) shows the block diagram of
Dunter O0 in Mode 2 when GATE = 0and INTO =1
TECHNICAL PUBLICATIONS - An up thrust for knowledge
8051 Timer / Counter and Programming
9-14
MicroprOcessors and Microcontrollers

TLO TFO
(8Bits) Interrupt
TO(P3.4)
cT=0

TRO THO
Control (8 Bits)
GATE(TMOD.3)
NTOPIN
CounterOcontrol logic
Fig. 9.2.14 (a)

TLO TFO Interrupt


TOP3.4) (8 Bits)
CITHO

TRO

Counter0 control logic when GATE =0 and INTO =1 THO


{8 Bits)

Fig. 9.2.14 (b)


and display the
Example 9.2.1 Wite a program for counter 1 in mode 2 to count the pulses
35)
state of TL1 count on port 2 Assume that clock input is connected to T1 in P
***********

Solution:
MOVTMOD, #01100000B Initialize counter 1 in
Mode 2, O/T=1
MOV TH1, #0 Clear TH1
SETB P3.5 Make T1 input
START : SETB TR1 Start the counter
BACK : MOV A, TL1 Get the count from TL1
MOVP2, A Sent it to port 2
JNB TF1, BACK If TF1 =0repeat
CLR TR1 Otherwise stop counter 1
CLR TF1 Make TF1=0
SJMP START Repeat

Note When 8051 is powered up ports are configured as input ports. To make them
as
work as output port we have to send high output on it. Therefore, to behave T1
input P 3.5 is set.

TECHNICAL PUBLICATIONS - An up thrust for


knowledge
Micropocessos and Microcortrolers 9- 15 8031 Tier /Counter and Programming

Example 9.2.2 Wite a progtam to display counter Oon 7-segment LEDs Assume thut clock
input is connected to pin (P 3.4).
Solution :
MOV TMOD, #00000110
Initialize counter 0in
Mode 2, CT=1
MOVTHO, #00H Reset counter value
SETB P3.4
Make T0 as input
START SETB TRO Start counter 0
BACK: MOVA, TLO Get the count value
ACALL CONVBCD
MOV P2, A Send count value in BCD on port 2
JNB TFO, BACK If TFO =0repeat
CLR TRO
Otherwise stop counter 0
CLR TFO Make TFO=0
SJMP START
Repeat
CONVBCD :ADD A, #00H [Use DAA for
DA A BCD conversion]
RET Return to main program

9.2.2 Programming Timers in 8051 C


The general purpose registers of 8051, such as RO-R7, A and B are under control of
the C compiler and are not accessed directly by C statement. However, in case of SFRs
entire RAM space of 80H-FFH is directly accessible to 8051 C statements. In this section
we discuss the accessing of timers using C statements.

Accessing Timer Registers in C


In 8051 C, we can access the timer registers TH, TL and TMOD directly with the
inclusion reg51.h file in the program. We can also access TR and TF bits directly. This is
illustrated in example.
Example 9.2.3 Write an 8051 Cprogram to toggle all bits of port P0 continuoushy. Use timer
0to generate delay of 1 sec between each toggle.
Solution:
#include <reg51.b>
void DELAY (void);
void nain (void)

whlle(1) /* Repeat forever /

"/
PO=0x00; / Make PO bits all zero
/ Wait for 1 sec /
DELAY():
thrust tor knowledge
TECHNICAL PUBLICATIONS An up
Microprocessors and Microcontrollers 9- 16 8051 Timer /Counter and Programming

PO = 0xFF; /* Make PObits all one */


DELAY); /* Wait for 1 sec */

void DELAY()

unsigned char i;
for(i=0; i<20; i++)
{
TMOD = 0x01; /* Configure timer 0 in mode 1 /
TLO = Ox28; /* Load count in TLO */
THO = Ox29; * load count in THO */
TRO = 1; /* Turn on TO */
while(TFO == 0); * Wait for TFO to rollover */
TRO = 0; /* tum off TO */
TF0 = 0; /* clear TFO */

Timer Count Calculations

Assume crystal frequency = 12 MHz


12
T = = 1us
12x 10-6

Let us determine the count to get a delay of 50 ms


50 ms
We need = 50000 clocks.
1us
Count = 65536 - 50000=10536 =2928 H
To get a delay of1sec we have to repeat the delay of timer0 20 (1/50 msec) times.
Example 9.2.4 Write an 8051 C programto toggle only pin P1.0 continuouslyevery 500 ms.
Solution: Let us use mode 2 of timer0 to create the delay. Mode 2 is an 8-bit auto
reload mode.
#include <reg51.h>
void Delay(void);
sbit portbit = P1 ^0;
void main(void)
{
unsigned char i,j;
while(1)
portbit =~ portbit; /* toggle P1.0 */
for(i=0; i<500; i++)
for(j=0; j<40; j+ +)
TECHNICAL PUBLICATIONS- An up thrust for knowledge
MicroprocessorS and Microcontrollers 9- 17 8051 Timer /Counter and Programming
Delay();

void Delay(void)

TMOD=Ox02: /* Time 0, mode 2(8-bit


auto-reload) */
/* load THO (auto-reload value) */
THO=-25;
TRO=1; /* turn on TO */
while(TF0==0); /* wait for TFO to roll over */
TRO=0; /* tum off TO */
TFO=0; /* clear TFO */

Delay calculations : Assume XTAL frequency = 12 MHz.


T= 12/12 MHz = 1 us
256 - 25 = 231

25 x 1.0 us = 25 us
Total delay = 25 us x500x40 = 500 ms

Note Due to inclusion of for loop of Cin delay generation, the delay may be slightly
more than expected. To get a correct delay we can adjust delay loop count by
observing frequency of port 1.0 on oscilloscope.
Example 9.2.5 Write an 8051 C program to create a frequency of 2 kHz on pin P2.0.
Solution : Let us use mode 2 of timer1 to create the delay. Mode 2 is an &-bit auto
reload mode.
#include <reg51.b>
void Delay(void):
sbit portbit=P2^0;
void main(void)
{
while(1)

portbit=~portbit; /* toggle P2.0 */


Delay();

voÀd Delay(void)
auto-reload) */
TMOD= 0x20; * Timer 1, mode 2(8-bit
Th1(auto-reload value) */
/* load
TH1=-250;
TR1=1; /* turn on T1 */
up thrust for knowledge
TECHNICAL PUBLICA TIONS-An
Microprocessors and Microcontrollers 9- 18 8051 Timer / Counter and
Programming
whille(TF1==0); /* wait for TF1 to roll over */
TR1=0; /* turn off T1 */
TF1=0; /* clear TF1 */

Delay calculations : Assume XTAL frequency = 12 MHz


T= 12/12 MHz = 1 us
1/2 kHz =0.5 ms
0.5 ms/2 = 0.25 ms
0.25 mns/ 1us = 250

Example 9.2.6 Two switches are connected to pin P1.0 and P1.1, respectively. Write an 8051
Cprogram to monitor switches and create the following frequencies on pin P2.0 according
switch positions.
SWi SWO Frequency
100 HHz

1 200 Hz

1 0 300 Hz

1 1 400 Hz

Use timer 0, mode 1for both of them.


Solution :
#incude <reg51.h>
sbit sw0=P1^0;
sbit sw1=P1^1;
sbit portbit=P2 ^0;
void Delay(unsigned char);
void main(void)

SW0=1; /* Make P1.0an input */


SW1=1; / make P1.1 an input /
whlle(1)

portbit~portbit; /* toggle P2.0 */


if(sw1==0&sw0==0) /* check switch */
Delay(1);
if(sw1==0&sw0==1)
Delay(2);:
if(sw1=1&sw0==0)
Delay(3):
TECHNICAL PUBLICATIONS- An up thrust for knowledge
MicroprocessorsS and Microcontrollers 9- 19 8051 Timer / Counter and Programming

1f(sw1==1&sw0==1)
Delay(4);
}
}
yoid Delay(unsigned char c)
TMOD=0x01;
do

TL0=0x78;
THO=0xEC;
TRO=1;
whíle(TFO==0);
TRO=0;
TFO=0;
C=C-1;

whle(cl=0);
}
EC78H= 60536

65536 - 60536 = 5000


5000 x 1 us = 5 ms
1/(5 ms x 2) = 100 Hz

CProgramming of Timers 0and 1as Counters


Let us see how to use timers 0and 1 as event counters. A timer can be used as a
cOunter if we provide external clock instead of using the frequency of the crystal
Oscillator as the clock source. By feeding pulses to the TO (P3.4) and T1 (P3.5) pins, we
Can use timer 0 and timer 1 as counter 0 and counter 1, respectively. Following
examples show us how timers 0 and 1 are programmed as counters using the C
laExample
nguage. 9.2.7 Assume that a 1-Hz external clock is being fed into pin T1. Write a C
PrOgram for counter 1 in mode 2 (8-bit auto reload) to count up and display the state of
he TLI count on PO. Start the count from 00H.
Sol#nclude
ution: <reg51.h>
bit T1 =
P3^5;
old main(void)
T1=1; * make T1 an input */
TMOD=0x60;
knowledge
TECHNICAL PUBLICATIONS -An up thrust for
MicroprOcessors and Microcontrollers 9-20 8051 Timer / Counter and Programming

TH1=0; * set count to 0 */


while(1) /* repeat forever */

TR1=1 /* start timer */


do

P0=TL1; /* place value on port PO */

while(TF1==0); /* wait for TF1 to rollover */


TR1=0; / stop timer */
TF1=0; /* clear flag */

Example 9.2.8 Assume that a 1 Hz external clock is being fed into pin TO. Write a C
program for counter 0 in mode 1 (16 bit) to count the pulses and display the THO and
TLO registers on Pl and PO, respectively.
Solution :
#Include <reg51.h>
void main(void)

TO=1; /* make TO an input */


TMOD=0x05;
TL0=0; /* set count to 0 /
THO=0; /* set count to 0 "/
whille(1) /* repeat forever */
{ TRO=1; /* start timer */
do

P0=TLO; /* place value of TL0 on port 0 */


P1=TH0; / place value of THOon port 1 */

while(TF0==0); / wait here */


TRO=0; /* stop timer */
TFO=0; /*clear flag */

being fed into pin T1. Wite a C


Example 9.2.9 Assume that a 2 Hz external clock is
to display the count in ASCI. The
program for counter 0 in mode 2 (8-bit auto reload)
Display the ASCII digits (in binary) on
8-bit binary count must be converted to ASCII.
PO serially. Display least significant digit first.
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Microprocessors and Microcontrollers 9-21 8051 Timer /Counter and Programming

Solution: To display the TLI count we must convert 8 bit binary data to ASCI.
##include <reg51.h>
void BinToASCII(unsigned char);
void main()

unsigned char value;


T1=1;
TMOD=0x06;
TH0=0;
while(1)

do

TRO=1;
value=TL0;
BinToASCII(value);
while(TFO= =0);
TRO=0;
TF0=0;

}
void BinToASCII (unsigned char value)
unsigned char Abyte, i,Q;
unsigned char RÊ31;
Abyte = 0XFB;
i=0;
do

Q=Abyte/10; /* divide by 10 *I
R[i]=Abyte%10; /* find remainder and save it */
Abyte=Q; /* save quotient as a number */
i= i+1;

while(Ql= 0) /* Repeat until quotient =0/


for(; i>0; i--)
/* Make binary to ASCII /
PO=R[0-1]+0<30;

TECHNICAL PUBLICATIONs - An up thrust for knowledge


Microprocessors and Microcontrollers 9- 22 8051 Timer / Counter and
Programming
Example 9.2.10 Assume that a 100 Hz external clock is being fed into pin TO. Write a
Cprogram for counter 0 in mode 2 (8-bit auto-reload) to display the seconds and minutes
on PO and Pi, respectively.
Solution:
#include <reg51.h>
void Time(unsigned char);
void main(void)

unsigned char val=0;


TO=1;
TMOD=0x06; /* TO, mode 2, counter */
THO=100; * sec=100 pulses */
while(1)
{ TRO=1; /* start timer /
while(TF0==0);
Time(val):
val++;
TRO=0; /* stop timer */
TF0=0; /* clear flag */

}
void Time(unsigned char value)
{
unsigned char sec,min;
min=value/60;
sec=value % 60;
P0=sec;
P1=min;

Example 9.2.11 WNrite an 8051 Cprogram to generate a rectangula wave of 2 kHz with
60 % duty cyce in pin P1.2. Assume crystal frequency as 11.0592 MHz Use Timer 0
in mode-1 operation. Show delay calculations. GTU: Winter-11

Solution: For 2 kHz rectarngular wave,


1
T = =0.5 ms
2 kHz
0.5 msx 60
For 60 %duty cycle : TON = 100
= 0.3 ms

0.5 msx 40
TOFF = 0.2 ms
100

TECHNICAL PUBLICATIONS - An up thrust for knowledge


MicroproDceSSors and Microcontrolers 9- 23 8051 Timer /Counter and Programming

12
Time for 1 T-state = = 1.085 us
11.0592 x106
0.3 ms
Count for ToN = = 276
1.085us
Count for ToFF = 184
Count to be loaded for TON = 65536 - 276 = 65260 = FEECH
THO = FEH, TLO = ECH
Count to be loaded for TOFE = 65536 -184= 65352 = FF48H
THO = FFH, TLO = 48H
#include <reg51.h>
sbit mybit = P2^1
void TOM1D (unsigned char) ;
void main (void)

while (1)

mybit = 1 ;/*Make P2.1 High/


TOM1D (1) :/* Wait for 0.3 msec /
mybit = 0 :/*Make P2.1 Low*/
TOM1D = (0) :/*Wait for 0.2 msec*/
}
}
void TOM1D (unsigned char i)

TMOD =0x01 ;/*Timer 0, mode 1*/


if (i = = 0)
{
TLO = ECH :/Load FEECH*/
THO = FEH
}
else

TLO = 48H /Load FF48H*/


THO = FFH

TRO = 1 /Tun ON Timer 0*/


While (TFO = = 0) /*Wait for TFO to roll over /
TRO = 0 /Tum OFF Timer 0 /
TFO = 0 /Clear TF0*/

TECHNICAL PUBLICATIONS -An up thrust for knowledge


9-24 9051 Timer / Counter and Programming
Microprocessors and Microcontrollers

Review Questions

different modes of operation of timelcounter with relevant block diagrams.


1. Explain complement i#
an assenmbly language program to use timer 1 to read input from port 1,
2. Write
to be done once in 50 msec.
and to output via port 2. This transfer is ON and orr
program in hich every 2 seconds, the LED Connected to P2.7 is turned
3. Write a Po
is getting data from P1 and sending it to
four times, while at the same time the 8051
states are 50 ms in duration.
continuOusly. Make sure the ON and OFF
Find the value of THI if the timer 1 is used in timer mode 2 to generate a baud rate of 4800
4.
Assume appropriate oscillator frequency.
Explain the modes of operation of timers/counters
5. Distinguish counting and timing requirements.
of 8051 with diagram.
to overflow if initially set to OABCH with
6. What is the time it will take a timer of 8051 in mode 1
6 MHz of/ crystal frequency.
frequency 2kHz if crystal of frequency
7. Write aprogram to generate a symmetric square wave of
11.0592 MHz is used.

8. Explain mode-2 programming with neat sketch and specify the program steps.
9. In what way timer/counter mode 2 programming is different from mode and mode 1?
counter, also write the
10. Write the block diagram to show mode 2 operation of timer 1, as a
programming steps to perform the same.
0 in mode
11. Write an ALP or C program to generate a frequency of 100 Hz square wave, using timer
1. Assume crystal frequency = 11.0592 MHz.
12. What is advantages and disadvantages of mode-2 operation of 8051 timer when compared mode-1
operation ?
13. With regard to timers of 8051,
) Explain briefly the difference between the timer and counter operation modes.
iü) Indicate how to start/stop the timer if GATE Control is also used.
ii) Explain mode - 2 operation.
14 Explain the working of timer in 8051 using the configuration of TCON and TMOD registers.
Explain the difference between timer operation in mode 0 and mode 1.
GTU : Winter- 15, Marks 7
15. Explain all the bits of TMOD register and hence find the control word for selecting timer
mode 2. GTU : Summer- l6, Marks 7
16. Draw timer-1 hardware and explain mode-2 operation of timer-1. GTU : Summer-16, Marks 7
17. Explain mode 2 operation of timers in 8051 microcontroller. GTU : Winter- 17, Marks 3
18. List different modes of timers. GTU : Winter-18, Marks 3
Microprocessors and Microcontrollers 9-25 8051 Timer /Counter and Programming

9.3 Programming Examples


GTU: Winter-08, 10,11,12, 13,14, 17, Summer-08, 10,12, 18,19
Example 9.3.1 Assume that XTAL = 11.0592 MH. Write a program to generate a square
wave of 2 kHz frequency on pin P1.5. GTU : Winter-08
Solution: T=1/f =1/2 kHz = 500 1s is period of square wave. 1/2 of it for high and
low portion of the pulse is 250 1s. 250 us/1.085 us = 230 and 65536 - 230 = 65306
in hex is FF1AH.
which
TL = 1AH and TH = FFH
Program is as follows
MOV TMOD, #10H ;Timer 1, mode 1
AGAIN: MOV TL1, #1AH ;Low byte of timer
MOV TH1, #0FFH ;High byte of timer
SETB TR1 ;Start timer 1
BACK: JNB TEF1, BACK ; Stay until timer rolls over
CLR TR1
:Stop timer 1
CPL P1.5
;Complement P1.5
CLR TF1 ;Clear timer flag
SJMP AGAIN ;reload timer
Example 9.3.2 Generate a square wave of frequency 1 kHz using timer 1 in mode l, on
Pin P1.2. Explain the TMOD word used to configure the timer 1 for
this application.
Show the necessary calculations to find the value ofcount to be loaded into THI and. TLI
registers. Assume XTAL frequency = 11.0592 MH. GTU: Summer-08
Solution: T of square wave = 1/f = 1/2 kHiz = 500 us

TON = ToFF = 5002 us = 250 us

T of clock = 12
11.0592>x 106
1.085 us

Count = ToN 250 Ls = 230


1.085 us
T of clock

Count to be loaded in TH1 and TL1 =65536 230 = 65306 = FF1AH


TH1 = FF and TL1 = 1AH

Program MOV TMOD, #10H Timer 1, mode 1 (16-bit)


AGAIN : MOV TL1, #1AH ;Load lower byte of timer
MOV TH1, #0FFH ;Load higher byte of timer
SETB TR1 ;Start timer 1
BACK : JNB TF1, BACK :Wait for timer rolls over
CLR TR1 ;Stop timer 1
TECHNICAL PUBLICATIONS- An up thrust for knowledge
9-26 8051 Timer / Counter and Programming
Microprocessors end MicroOcontrollers

CPL P1.2 ;Complement P1.2


CLR TF1 Clear timer flag1
SJMP AGAIN Reload timer 1 and continue

write an 8051 program to


Example 9.3.3 Explain the steps to program timers in model and
GTU: Summer-08
generate a square wave of 50 %duty cycle on the pin P1.5.
Solution: Square wave
MOV TMOD, #01 :Timer0 mode 1
MOV TLO, #0F2H Load TLO - OF2
Here :
MOV THO, #0FFH Load THO -OFF
CPL P1.5 toggle input on P1.5
ACALIL Delay
SJMP Here
Delay : SETB TRO ;Start Timer 0
again : JNB TF0, again :Monitor Timer 0 flag until it rolls over
CLR TRO
CLR TFO
RET

Example 9.3.4 Write an,ALPto generatesquare wave on pin P1.5 of 500 Hz(approximately)
with asubroutine toprovide utime delay of30.38 us using timer 0. Assume that crystul
frequency of 8051 is11.0592 Hz. GTU : Winter-10. Summer-10
Solution :
12 12
1.085 us
Crystal frequency 11.0592x 10
Number of counts for roll over = 30.38 us = 28
1.085 us
65536 28 = 65508 = FFE4H
.:. To get a delay of 30.38 we have to load TH0 = FFH and TLO = E4H
1
For square wave T = = 2 ms
500
TON = TOFF = T/2 = 1 ms
Thus we have call delay routine (1 ms/30.38 us) = 33 = 21H times
Program
MOV TMOD, #01 :Timer0, mode1
HERE : MOV TLO, #E4H :Load TLO = E4H
MOV TH0, #FFH ;Load THO = FFH
CPL P1.5 ;toggle P1.5
MOVRO, #21H ;Load count in R0
BACK: ACALL Delay ;wait for 30.38 us x 33 = 1ms
DJNZ RO, BACK
SJMP HERE
DELAY: SETBTRO ;start timer 0
AGAIN: JNB TF0,AGAIN :Wait for TFO to roll over
TECHNICAL PUBLICATIONS- An up thrust for knowledge
Microprocessors and Microcontrollers 9-27 8051 Timer / Counter and Programming
CLR TRO istop timer 0
CLR TFO ;Clear TFO
RET ; Retum
Example 9.3.5 Firnd the delay generated by timer 0 in the following code, Calculate the delay
generated excluaing the instruction overhead. What count has to be loaded in TLO and
THO if elay has to be increased to 25 msec ?
CLR P2.3
HERE : MOV TMOD, #01
MOV TLO, #3Eh
MOV THO, #0B8h
SETB 1EO,
AGAN: JNB TF0, AGAIN
CLR TFO
GLR TRO
CLR P2,3
Solution :
Timer count = B83E

(FFFF B83E + 1) = 47C2H = (18370) 10


Assuming XTAL = 11.0592 MHz
12 = 1.085x 10-6
T =
11.0592x 106
= 1.085x 10- x18370 = 19.93 ms
Delay
For 25 msec delay
25 ms = 23041
Decimal count =
1.085x 10-6
= 5A01 H

:. TLO = 01H and THO= 5AH


program to monitor
Example 9.3.6 4 switch s connected to the pin Pl.2. Write an 8051 C
tne switch and createthe following frequencies on pin Pi.7.
1) When SW 0: 500 Hz i)When SW 1750 Fz
Use timer 0, mode 1 for both of them,
Solution:
#nclude <reg51.h>
sbit sw=P1^2;
Sbit port bit = P1^7;
thrust for knowledge
TECHNICAL PUBLICATIONS - An up
Microprocessors and Microcontrollers 9-28 8051 Timer / Counter and Programming

void Delay (void);


void main (void)

SW=1; /* Make SW an input */


while(SW==0)
portbit=~portbit; /* toggle portbit */
For (i=0; i<20; i++)
Delay():
while(SW==1)

portbit =~portbit; /* toggle portbit */


for (i=0; i<30; i++)
Delay ():

void Delay (void)

TMOD = Ox01; * Timer 0, mode 1 */


TLO = OxFE; / Load TLO */
THO = OxA5; * Load THO */
TRO = 1; /* Turn ON timero */
While (TF0==0); /* Wait for TF0 to roll over */
TRO=0; /* Turn OFF TO */
TFO=0; * Clear TF0*/

(A5FE)H=(42494),10
65536 - 42494 = 23092
23092 x 1.085 us = 25 ms
500 ms = 25 ms x 20
750 ms = 25 ms × 30

Example 9.3.7 Write a 8051C progTAm to toggle all bits of port PO


timer 0 to generate the delay of l sec between each toggle.
continuousy. Use
Solution :
#include <reg51.h>
void Delay (void)
vold main (vold)
{
whlle (1)

PO = 0x00; /* PO bits all zero */


Delay ():

TECHNICAL PUBLICATIONS - An up thrust for knowledge


Microprocessors and Microcontrollers 9-29 8051Timer/Counter and Programming
PO = 0xFF: * Make PO bits all one */
Delay ():

void Delay ()

unsigned char i;
for(i=0; i<20; i++)
TMOD = 0X01: /* Timer 0 in mode 1 */
TLO = 0X28: /* Load count in TLO */
THO = 0X29 * Load count in THO */
TRO = 1; * Tun on TRO /
while (TF0==0); /* Wait for TFO to rollover*/
TR0=0; /* Tum off TO *|
TF0=0; * Clear TFO */

}
Assume crystal frequency= 12 MHz
12
T = = 1us
12x10-6
50 ms
Count to get a delay of 50 ms = = 50000 clocks
1 us
Count = 65536 - 50000 10536 = 2928 H.
To get a delay of 1 sec we have to repeat delay of timer., 20 times (1/50 msec = 20).
Example 9.3.8 Find out Hex. number to be loaded in THO, to produce delay of 4.096 msec in
mode 0 operation. Assume clock frequency of 12 MHz.
Solution : Timer clock frequency Crystal frequency + 12 = 12 MHz + 12
1MHz

Timer clock period 1 us

Maximuum count in mode 0 is 1FFFH (8191) and we have to count for 4096 counts to
get a delay of 4.096 ms
Count to be loaded in timer is 1FFFH
(8191 in decimal) - 4096 = FFFH (4095 in decimal)
FFH in TLO.
Therefore, We have to load OFHin TH0 and
frequency of 10 kHz on
ple 9.3.9 Using autoreload mode of timer 0in 8051, generate a
pin P1.0. Write assembly language program for it.

for knowledge
TECHNICAL PUBLICATIONS- An up thrust
Microprocessors and Microcontrollers 9-30 8051 Timer / Counter and Programming

of 10 kHz, one
Solution:Assume XTAL frequency = 12 MHz. To generate a frequency
MHz we have cycle
half of the cycle is of period 0.05 ms. Since XTAL frequencyto =50.12Thus the initial value
period is lus. Therefore, we have to decrement count equal
to be loaded in THO = (256 - 50) = 206.
Program
MOV TMOD,#2H :Timer 0, mode 2 (8-bit auto reload)
MOV THO,#206 ;THO = 206
SETB TRO ;Start timer 0
BACK: JNBTF1, BACK ;Stay till timer rollsover
CPL P1.0 :Complement P1.0
CLR TFO ;Clear timer flag 0
SJMP BACK :Continue
and OFF connected
Example 9.3.10 Nrite an assembly anguage program to make LED ON
to P 1.0 continuously with ON tine 20 msec and off time 40 msec.
*******88

Solution: Assume crystal frequency 12 MHz


Time for one instruction = (1/12 MHz)* 12 = 1usec.
Count to be loaded in TH0:TLO = 20 1sec/1 usec. = 20000 (Decimal)
65536 - 20000 = 45536 = B1EOH
ORG 0000H ;Starting address
START: MOV TMOD, #01H ; Timer 0, Mode 1
MOV TH0, #0B1H :Load timer count
MOVTLO,#OEOH
SETB P1.0 ;Make P1.0 High
ACALL DELAY 20MS;Wait for 20msec
CLR P1.0 :Make P1.0 low
ACALL DELAY 20MS ; Wait for 40 msec
ACALL DELAY 20MS
SJMP START ;Repeat
DELAY20MS : SETB TRO : Start timer 0

HERE: JNB TFO, HERE :Wait for timer roll over


CLR TRO ;Tum off timer 0
CLR TFO ; Clear TFO
RET :Retum
Example 9.3.11 Write an assembly program to generate a waveform with ON time of 7nsec
and OFF time of 21 msec on PO.5. Assume XTAL of 11.0592 MHz. Use timer .
GTU : Summer-12

TECHNICAL PUBLICATIONS- An up thrust for knowledge


MicroproceSsors and Microcontrollers 9-31 8051 Timer / Counter and Programming

Solution:
12
= 1.085 m sec
11.0592x106
Count to be loaded in THO: TLO = 7msec
= 6452 (Decimal)
1.085 sec
65536 - 6452 = 59084= E6CCH
ORG 0000H ;Starting address
START: MOV TMOD, #01H : Timer 0, mode 1
MOV TH0, #OE6H:Load timer count
MOV TLO, #0CCH
SETB P0.5 ;Make P0.5 HIGH
ACALL DELAY7MS :Wait for 7 msec (ON period)
CLR PO.5 ;Make PO.5 LOW
ACALL DELAYZMS ;Wait for 21 msec (OFF period)
ACALL DELAY7MS
ACALL DELAY7MS
SJMP START ;Repeat
DELAYMS: SETB TRO ;Start Timer 0
HERE : JNB TF0, HERE ;Wait for timer roll over
CLR TRO Tum OFF Timer 0
CLR TFO ;Clear TFO
RET :Retum
Example 9.3.12 Wite an assembly nguage progrm to generate a squrewave of frequency
JkHz on pin P30 using auto reload modeoftimer 0 in 8051.
Solution : For 5 kHz T = 1/5 kHz = 200us and T/2 = 100 us
Assume clock frequency 12 MHz so time for one cycle is 1 us. So the value of THO
and TLO is 255 - 100 = 155
ORG 0000H
MOV TMOD, #02H Timer 0 in mode 2
MOV TH0,#155 THO = 155
SETB TRO Start timer 0
HERE: JNB TFO, HERE
CPL P3.0
ORG 001BH
CLR TFO
SJMP HERE
Show
Example 9.3.13 Write program to generate delay of 0.25 ms sing timer-1 în mode 2.
GTU : Winter-12, Marks 7
necessary calculation.

knowledge
TECHNICAL PUBLICATIONS An up thrust for
9-32 8051 Timerr/ Counter and
Microprocessors and Microcontrolers
Programming
Solution :
MOV TMOD,#20H
MOV TH1,#-06H
SETB TR1
HERE : JNB TF1, HERE
Delay calculations : Assume XTAL frequency = 12MFiz
T = 12/12 MHz = 1us
256 - 250 = 06

250x 1.0 us = 250 us = 0.25 ms


timer
Example 9.3.14 VWrite program to generate square wave of 1 kiHz on pot pin P1.7. use
generated delay bytimer 0 in mode 0. Consider crystalfrequency 12 MHZ.
GTU: Winter-13, Marks 7

Solution: Delay calculations : T = 12/12 MHz = 1 us


1/1 kHz = 1ms
1ms = 0.5 ms

0.5 ms/1 ms = 500


Since it is 13-bit timer maximum count is 1FFFH =8191 decimal
8191 - (500 - 1) or 8192 - 500 = 7692 = 1E0CH
MOV TMOD, #0OH ;Timer 0, mode 0(13-bit mode)
HERE: MOV TLO, #0CH TLO = 0CH, the low byte
MOV THO, # 1EH ;THO = 1EH, the high byte
CPL P1,7 :Toggle P1.7
ACALL DELAY ;Wait for 0.5 ms
SJMP HERE
DELAY: SETB TRO ;Start the timer 0
AGAIN: JNB TFO, AGAIN :Check timer flag 0 until it rolls over
CLR TRO Stop timer 0
CLR TFO Clear timer 0flag
RET

Example 9.3.15 Write program to count external pulses and save t in register R6 and RI.
User timer 1, 16-bit mode, GTU : Winter-13, Marks 7

Solution:
MOV TMOD, #01010000B ;Counter 1, mode 1; C/T =1 extemal pulses
MOV TH1, #0 ; Clear TH1
MØV TL1, #0 :Clear TL1
SETB P3.5 ;Make T1 input
AGAIN: SETB TR1 : Start the counter 1

TECHNICAL PUBLICATIONS - An up thrust for knowledge


Microprocessors and Microcontrollers 9-33 8051 Timer / Counter and Programming

BACK : MOV A, TL1 ;Get copy of TL1


MOV R6, A ;Save it in R6
MOVA, TH1 ; Get copy of TH1
MOV R7, A Save it in R7
JNB TF1, BACK Keep doing, until TF1 = 0
CLR TR1
Otherwise stop counter 1
CLR TF1 : Make TF1 = 0
SJMP AGAIN ;Repeat
Example 9.3.16 Prepare ALP to generate 250 ms delay subroutine using
timer 1 mode 2with
11.0592 MHz quartz clock. GTU : Winter-14, Marks 7
Solution: With clock 11.0592 MHz we have T = 1.085 us
Required delay = 250 ms .. Total count = 250 ms = 230414
1.085 us
Since we are using mode 2 we are allowed to load only 8-bit
up to 256. Thus we load - 230 in THI count. This limits count
to get 230 counts and put external loop
count = 1000 to get the desired delay.
DELAY : MOV TMOD, #20H ; Timer 1, 8-bit (auto-reload)
MOV TH1, # -E6H :TH1 =-230
MOV RO, # 04H ; Loop count 250x 4= 1000
MOV R1, #FAH ; Decimal
SETB TR1 :Start timer 1
BACK: JNB TF1, BACK :Wait for timer rolls over
CLR TE1 ;Clear Timer 1 flag
DJNZ R1, BACK ;Check if loop count =0
DJNZ RO, BACK ; Otherwise repeat
RET ;End subroutine
Example 9.3.17 Write an assembly language program to generate a
time of 4 ms and an square wave with an ON
OFF time of 10 ms on all pins of port 0. Assume an XTAL of
22 MHz.
GTU :Winter-11, Marks 7
Solution: Let us derive the count for 2 ms delay. So that such delay is
for ON time and5 times for OFF called for 2times
time.
1
T= = 45.45 ns
22x 106
2 ms
= 44000 and 65536 - 44000 = 21536 = 5420 H
45.45 ns
TL1 = 20H and TH1 = 54H

TECHNICAL PUBLICA TIONS - An up thrust for knowledge


Microprocessors and Microcontrollers 9-34 8051 Timer / Counter and
Programming
MOV TMOD, #10H Timer 1, mode1 (16-bit)
START : MOV R0,#2
MOVR1,#5
MOV P0,#OFFH
BACK : LCALL DELAY
DJNZ RO, BACK
MOV P0,#00H
BACK1: LCALL DELAY
DJNZ R1,BACK1
SJMP START
DELAY: MOVTL1,#20H ; TL1 = 20H,l0wer byte
MOV TH1,#54H ; TH1 = 54H,higher byte
SETB TR1 ;Start timer 1
REPE : JNB TF1, REPE Stay until timer rolls over
CLR TR1 ; Stop timer 1
CLR TF1 ; Clear timer 1 lag
RET ; Retum to main
Example 9.3.18 Write an 8051 C program to toggle allprogram
bits of P2 continuously every
500 ms. Use timer 1, mode 1 to create the delay. GTU: Winter-11, Marks 7
Solution:
#include <reg51.h>
void T1M1 Delay(void);
void main(void)
{
unsigned char i;
P2=0x 55;
while(1)

P2=~P2; /*toggle all bits of P2*/


for (i=0; i<20; i++)
TIM1Dealay();

Void T1M1Delay(void)

TMOD=0x10
TL1=0X FE;
/*Timer1,mode1*/
/*Load TL1*/
TH1=0x A5; /*Load TH1*/
TR1=1; *Tum ON Timer1*/
while(TF1==0); /*wait for TF1 to roll over*/
TR1=0; /*Tun OFF Timer 1*/
TF1=0; /*Clear TF1/
TECHNICAL PUBLICATIONS -An up thrust for knowledge
Microprocessors And Microcontrollers 9- 35 8051 Timer /Counter and Programming

500ms + 20 (iteration count) = 25 ms


25 ms = 23042
1.085us
Count to be loaded = 65536 - 23042 = 42494 = ASFEH
Example 9.3.19 Write a program to generate a square wave with frequency of 10 kHz and
roith 50 % duty cycle ustng Timer 0. Assume the external crystal
frequency of 12 MHz.
GTU : Summer- 12, Marks 3
Solution:
12 12
T= = 1us
Crystal frequency 12x 106
To generate 10 kHz frequency we require time
delay of 50us in half of the square wave.
ToN ToFF
Number of count for roll over = 50 us = 50
lus -50 us50 us
65536 - 50 = 65486 = FFCEH
MOVTMOD,#01 ; Timer 0 Mode1
HERE: MOV TLO,0CEH ; Load TLO
MOV TH0,#0FFH ; Load THO
CPL P1-0 ; Toggle P1-0 to generate square wave
SETB TRO ; Start Timer 0
AGAIN : JNB TF0,AGAIN : Monitor Timer 0 lag until if rolls
over
CLR TRO ; Clear TRO
CLR TF0 ; Clear TFO
SJMP HERE ; Repeat

Note The square wave is generate on pin P:0.


Example 9.3.20 Timer 0 is used as a counter and Timer 1 is used as time base of
1 second.
Write an assembly program to find frequency of signal connected at P3.4
(counter input
jor Timer 0). Assume XTAL 22 MHz. GTU : Summer-12, Marks 7
DOlution : It is given that Timer 1 is used as time base for timing 1 second. During
1 second, this
Timer 0is run as a counter with input pulses fed into pin P3-4. At the end of
Sécond, the values in TLO adn THO give the number of pulses that were
received at
ret o during 1second. This gives the frequency of the unknown signal.
ORG 0000H
AGAIN MOVTMOD,#15H Timer 1 as Timer and Timer 0 as counter
SETBP3.4 Make port 3-4 as input port

TECHNICAL PUBLICATIONS - An up thrust for knowledge


Microprocessors and Microcontrollers 9-36 8051 Timer / Counter and Programming

MOV TL0#00 Clear TLO


MOV TH0,#00 Clear THO
SETB TRO Start counter
MOVR0,#28H RO=28, to Time 1 second
BACK: MOV TL1,#00H TL1 = 0
MOV TH1,#00H TH1 = 0
SETB TR1 Start Timer 1
BACK 1 JNB TF1,BACK1 Check Timer 1 lag
CLRTF1 Clear Timer 1 flag
CLR TR1 Stop Timer 1
DJNZ R0,BACK Repeat until RO=0
MOV A,TLO Since 1 sec is Over
Check TLO
MOV P2,A MOVTLO to port2
MOV A,THO Read THO
MOV P1,A MOVE THO to port 1
SJMP AGAIN Repeat forever
END
12
T = =5.4545x10-7
22x 106
Total count roll over = 65536

Time delay given by one roll over =65536x 5.4545x 10- = 0.0358 sec
1sec
:. Multiplier count = = 28
0.0358 sec
Example 9.3.21 What is the minimum and maximum frequency of square
wave that can be
generated at any port of 8051 microcontroller using crystal frequency of 12 MHz ?
GTU: Winter-17, Marks 3
12
Solution : T = = 1us
12x10-6
For maximum frequency : Use mode 2 and
load count = 255
256 - 255 = 1 and

Time delay will be 1 x 1.0 us = 1 us


TON =TOFF = 1 us and T =2 us
Therefore, we get square wave of frequency = 6 MHz
For minimum frequency : Use mode 1 (16-bit
timer) and load count = 0000 and
Time delay will be 65536 x 1 us = 65.536 ms

TECHNICAL PUBLICATIONS -An up thrust for


knowledge
Microprocessors and Microcontrollers 9-37 8051 Timer / Counter and Programming

TON ToEF = 65.536 ms andT = 13.1072 ms


Therefore, we get square wave of frequency = 76.29 Hz
Example 9.3.22 Find the control word to be loaded in THO and TLO to generate a
time delay
of 10 us. (Assume 12 MHz cock) GTU : Summer-18, Marks 4
Solution: Given: 12 MHz clock
12
T= = 1 us
12x106

To generate delay of 10 us
Count 10s = 10
lus
Count to be loaded = 65536 - 10 = 65526 = FFF6H
THO = FFH and TL0 = F6H
Example 9.3.23 Find control word to be
loaded in timer registers THO and TLO to generate
time delay of 25 ms Assume clock frequency as 11.0592 MHz.
GTU: Summer-19. Marks 4
Solution : Given : 11.0592 MHz clock
12
T= = 1.085 us
11.0592x106
To generate delay of 25 ms
25 ms
Count = = 23041
1.085us
Count to be loaded = 65536 - 23041 = 42495 = A5FFH
THO = A5H and TL0 = FFH

9.4 Short Questions and Answers


Fill in the Blanks
Q.1 (Ans. : 89H]
TMOD is located at
Q.2 in 8051. (Ans. :88H)
TCON is located at
(Ans. : 8CH)
Q.3 THO is located at
[Ans. : TLO]
Q.4 is located at 8AH.
[Ans.: 16]
Q.5 Both TO and T1 are bit registers.
knowledge
PUBLICA TIONS - An up thrust for
TECHNICAL
Microprocessors and Microcontrollers 9-38 8051 Timer / Counter and
Programming
Q.6 When M1 = 0, MO = 0timer is selected. [Ans. :mode 0)
Q.7 For 8-bits timer (not autoreload) bits M1 MO =
Q.8 When Ml = 1and MO = 0, mode is selected.
[Ans.:0,0)
JAns. : 8-bit auto-reload)
Multiple Choice Question
Q.1 8051 has multiple mode, timer/counters.
a 2, 16 bit bË 4, 32 bit
2, 32 bit di 4, 16 bit [Ans.: al

You might also like