0% found this document useful (0 votes)
192 views12 pages

8051 Timer Programming Guide

The document discusses programming timers/counters in 8051 C. Timers 0 and 1 can each generate time delays when used as timers, or serve as event counters when external clock pulses are fed into them. The timers are accessed via the THx and TLO registers and the TMOD register sets the operation mode. Mode 0 is a 13-bit timer, mode 1 is 16-bit, and mode 2 is 8-bit. When used as timers, the crystal frequency clocks the timers, whereas external pulses increment the registers when used as counters. Examples are provided to illustrate programming the timers for various modes and applications like delays, frequencies, and counting.

Uploaded by

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

8051 Timer Programming Guide

The document discusses programming timers/counters in 8051 C. Timers 0 and 1 can each generate time delays when used as timers, or serve as event counters when external clock pulses are fed into them. The timers are accessed via the THx and TLO registers and the TMOD register sets the operation mode. Mode 0 is a 13-bit timer, mode 1 is 16-bit, and mode 2 is 8-bit. When used as timers, the crystal frequency clocks the timers, whereas external pulses increment the registers when used as counters. Examples are provided to illustrate programming the timers for various modes and applications like delays, frequencies, and counting.

Uploaded by

MusTaFa QuReShi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Accessing timer registers in C

In 8051 C we can access the timer registers TH, TL, and TMOD directly using the reg51 .h
header file. This is shown in Example 9-20. Example 9-20 also shows how to access the TR
and TF bits.

Example 9-20
Write a 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.
Solution:

Syed M Hamedoon Timer in 8051 C Microprocessor Theory& Interfacing


Calculating delay length using timers
As we mentioned in Chapter 7, the delay length depends on three factors: (a) the crystal
frequency, (b) the number of clocks per machine cycle, and (c) the C compiler. The original
8051 used 1/12 of the crystal oscillator frequency as one machine cycle. In other words,
each machine cycle is equal to 12 clocks periods of the crystal frequency connected to the
XI – X2 pins. The time it takes for the 8051 to execute an instruction is one or more
machine cycles, as shown in Appendix A. To speed up the 8051, many recent versions of
the 8051 have reduced the number of clocks per machine cycle from 12 to four, or even
one. For example, the AT89C51/52 uses 12, while the DS5000 uses 4 clocks, and the
DS89C4xO uses only one clock per machine cycle. As we mentioned earlier in this chapter,
the 8051 timers also use the crystal frequency as the clock source. The frequency for the
timer is always l/12th the frequency of the crystal attached to the 8051, regardless of the
8051 version. In other words, for the AT89C51/52, DS5000, or DS89C4xO the duration of
the time to execute an instruction varies, but they all use 1/12th of the crystal’s oscillator
frequency for the clock source to the timers. This is done in order to maintain compatibility
with the original 8051 since many designers use timers to create time delay. This is an
important point and needs to be emphasized. The C compiler is a factor in the delay size
since various 8051 C compilers generate different hex code sizes. This explains why the
timer delay duration is unknown for Example 9-20 since none of the other factors mentioned
is specified.
Delay duration for the AT89C51/52 and DS89C4xO chips
As we stated before, there is a major difference between the AT89C51 and DS89C4xO
chips in term of the time it takes to execute a single instruction. Although the DS89C4xO
executes instructions 12 times faster than the AT89C51 chip, they both still use Osc/12
clock for their timers. The faster execution time for the instructions will have an impact on
your delay length. To verify this very important point, compare parts (a) and (b) of Example
9-21 since they have been tested on these two chips with the same speed and C compiler.
Timers 0 and 1 delay using mode 1 (16-bit non auto-reload)
Examples 9-21 and 9-22 show 8051 C programming of the timers 0 and 1 in mode 1 (16-bit
non-auto reload). Examine them to get familiar with the syntax.
Timers 0 and 1 delay using mode 2 (8-bit auto-reload)
Examples 9-23 through 9-25 shows 8051 C programming of timers 0 and 1 in mode 2 (8-bit
auto-reload). Study these examples to get familiar with the syntax.

Syed M Hamedoon Timer in 8051 C Microprocessor Theory& Interfacing


Example 9-21

Write an 8051 C program to toggle only bit PI.5 continuously every 50 ms. Use Timer 0,
mode 1 (16-bit) to create the delay. Test the program (a) on the AT89C51 and (b) on the
DS89C420.
Solution:

Syed M Hamedoon Timer in 8051 C Microprocessor Theory& Interfacing


Example.9-22
Write an 8051 C program to toggle all bits of P2 continuously every 500 ms. Use Timer 1.
mode 1 to create the delay.
Solution:

NOTE THAT 8051 TIMERS USE 1/12 OF XTAL FREQUENCY, REGARDLESS OF


MACHINE CYCLE TIME.

Syed M Hamedoon Timer in 8051 C Microprocessor Theory& Interfacing


Example 9-23
Write an 8051 C program to toggle only pin PI.5 continuously every 250 ms. Use Timer 0,
mode 2 (8-bit auto-reload) to create the delay.
Solution:

256-23 = 233
23 x 1.085 us = 25 us
25 us x 250 x 40 = 250 ms by calculation.
However, the scope output does not give us this result. This is due to overhead of the for
loop in C. To correct this problem, we put 36 instead of 40.

Syed M Hamedoon Timer in 8051 C Microprocessor Theory& Interfacing


Example 9-24
Write an 8051 C program to create a frequency of 2500 Hz on pin P2.7. Use Timer 1. mode
2 to create the delay.
Solution:

Syed M Hamedoon Timer in 8051 C Microprocessor Theory& Interfacing


Example 9-25
A switch is connected to pin PI.5. Write an 8051 C program to monitor SW and create
the following frequencies on pin PI.7:
SW=0: 500 Hz
SW=1: 750 Hz;
Use Timer 0, mode 1 for both of them.
Solution:

Syed M Hamedoon Timer in 8051 C Microprocessor Theory& Interfacing


C Programming of timers 0 and 1 as counters
In Section 9.2 we showed how to use timers 0 and 1 as event counters. A timer can be
used as a counter if we provide pulses from outside the chip instead of using the frequency
of the crystal oscillator as the clock source. By feeding pulses to the TO (P3.4) and Tl (P3.5)
pins, we turn Timer 0 and Timer 1 into counter 0 and counter 1, respectively. Study the next
few examples to see how timers 0 and 1 are programmed as counters using the C
language.
Example 9-26
Assume that a 1-Hz external clock is being fed into pin Tl (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
PI. Start the count at OH.
Solution:

Syed M Hamedoon Timer in 8051 C Microprocessor Theory& Interfacing


Example 9-27
Assume that a 1-Hz external clock is being fed into pin TO (P3.4). Write a C program for
counter 0 in mode -1 (16-bit) to count the pulses and display the THO and TLO registers on
P2 and PI, respectively.
Solution:

Syed M Hamedoon Timer in 8051 C Microprocessor Theory& Interfacing


Example 9-28
Assume that a 2-Hz external clock is being fed into pin Tl (P3.5). Write a C program for
counter 0 in mode 2 (8-bit auto reload) to display the count in ASCII. The 8-bit binary count
must be converted to ASCII. Display the ASCII digits (in binary) on PO, PI, and P2 where
PO has the least significant digit. Set the initial value of THO to 0.
Solution:
To display the TL1 count we must convert 8-bit binary data to ASCII. See Chapter 7 for data
conversion. The ASCII values will be shown in binary. For example, ’9′ will show as
00111001 on ports.

Syed M Hamedoon Timer in 8051 C Microprocessor Theory& Interfacing


Example 9-29
Assume that a 60-Hz external clock is being fed into pin TO (P3.4). Write a C program for
counter 0 in mode 2 (8-bit auto-reload) to display the seconds and minutes on PI and P2,
respectively.
Solution:

Syed M Hamedoon Timer in 8051 C Microprocessor Theory& Interfacing


For Examples of Timer 2, see the [Link] Web site.

SUMMARY
The 8051 has two timers/counters. When used as timers they can generate time delays. When used as
counters they can serve as event counters. This chapter showed how to program the timers/counters
for various modes.
The two timers are accessed as two 8-bit registers: TLO and THO for Timer 0, and TL1 and TH1 for
Timer 1. Both timers use the TMOD register to set timer operation modes. The lower 4 bits of
TMOD are used for Timer 0 and the upper 4 bits are used for Timer 1.
There are different modes that can be used for each timer. Mode 0 sets the timer as a 13-bit timer,
mode 1 sets it as a 16-bit timer, and mode 2 sets it as an 8-bit timer.
When the timer/counter is used as a timer, the 8051 ‘s crystal is used as the source of the frequency;
when it is used as a counter, however, it is a pulse outside the 8051 that increments the TH, TL
registers.

Syed M Hamedoon Timer in 8051 C Microprocessor Theory& Interfacing

You might also like