Timers Program In 8051

/ Comments off

/ File Download Area 8051 Timer 0 Mode 1 Example Program This example program shows how to configure timer/counter 0 as a 16-bit timer. An interrupt service routine (ISR) is invoked each time the timer overflows (goes from 0xFFFF to 0x0000). In the ISR, the overflowcount variable increments. To test this program. Start the debugger. Set a breakpoint on the overflowcount line in the ISR.

8051

How can the answer be improved? The 8051 has two timers: Timer 0 and Timer 1. They can be used either as timers or as event counters. In this section we first discuss the timers’ registers and then show how to program the timers to generate time delays.

Run the program. Each time the interrupt routine is invoked, the debugger stops running the program.

Position the cursor over the overflowcount variable to view its current value.

8051 Tutorial

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.

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.

Timer Programs In 8051 Embedded C

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. 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.