SECTION 9.3: PROGRAMMING TIMERS 0 AND 1 IN 8051 C
In Chapter 7 we showed some examples of C programming for the 8051. In this section we study C programming for the 8051 timers. As we saw in the examples in Chapter 7, the general-purpose registers of the 8051, such as RO – R7, A, and B, are under the control of the C compiler and are not accessed directly by C statements. In the case of SFRs, the entire RAM space of 80 – FFH is accessible directly using 8051 C statements. As an example of accessing the SFRs directly, we saw how to access ports PO – P3 in Chapter 7. Next, we discuss how to access the 8051 timers directly using C statements.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
For Examples of Timer 2, see the www.MicroDigitalEd.com Web site.
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.