Programs On Timers
Programs On Timers
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.
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:
Example 9-25
A switch is connected to pin PI.2. 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:
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:
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:
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.
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:
For Examples of Timer 2, see the www.MicroDigitalEd.com 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.