100% found this document useful (1 vote)
112 views8 pages

Programs On Timers

This chapter discusses programming the 8051's two timers/counters for various operating modes. The timers can generate time delays when used as timers, or serve as event counters when used as counters. Timer 0 is accessed via the TLO and THO registers, while Timer 1 uses TL1 and TH1. The TMOD register sets the operation mode, with the lower 4 bits for Timer 0 and upper 4 for Timer 1. Modes include 13-bit, 16-bit, and 8-bit configurations. When used as a timer, the crystal frequency serves as the time base, whereas an external pulse increments the registers when used as a counter. Examples show programming the timers in different modes to perform tasks like toggling pins at

Uploaded by

Pavan Kumar N
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
100% found this document useful (1 vote)
112 views8 pages

Programs On Timers

This chapter discusses programming the 8051's two timers/counters for various operating modes. The timers can generate time delays when used as timers, or serve as event counters when used as counters. Timer 0 is accessed via the TLO and THO registers, while Timer 1 uses TL1 and TH1. The TMOD register sets the operation mode, with the lower 4 bits for Timer 0 and upper 4 for Timer 1. Modes include 13-bit, 16-bit, and 8-bit configurations. When used as a timer, the crystal frequency serves as the time base, whereas an external pulse increments the registers when used as a counter. Examples show programming the timers in different modes to perform tasks like toggling pins at

Uploaded by

Pavan Kumar N
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

1. 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.
2. Write an 8051 C program to toggle all bits of P2 continuously every 500 ms. Use Timer 1.
mode 1 to create the delay.
NOTE THAT 8051 TIMERS USE 1/12 OF XTAL FREQUENCY, REGARDLESS OF MACHINE CYCLE
TIME.
3. 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.

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:

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:

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.

You might also like