In Chapter 9 we discussed how to use Timer 0 and Timer 1 with the polling method. In this section we use interrupts to program the 8051 timers. Please review Chapter 9 before you study this section.

Figure 11-3. TF Interrupt
Roll-over timer flag and interrupt
In Chapter 9 we stated that the timer flag (TF) is raised when the timer rolls over. In that chapter, we also showed how to monitor TF with the instruction “JNB TF, target”. In polling TF, we have to wait until the TF is raised. The problem with this method is that the microcontroller is tied down while waiting for TF to be raised, and cannot do any thing else. Using interrupts solves this problem and avoids tying down the controller. If the timer interrupt in the IE register is enabled, whenever the timer rolls over, TF is raised, and the microcontroller is interrupted in whatever it is doing, and jumps to the interrupt vector table to service the ISR. In this way, the microcontroller can do other things until it is notified that the timer has rolled over. See Figure 11-3 and Example 11-2.

Notice the following points about the program in Example 11-2. 1. We must avoid using the memory space allocated to the interrupt vector table. Therefore, we place all the initialization codes in memory starting at 30H. The LJMP instruction is the first instruction that the 8051 executes when it is powered up. LJMP redirects the controller away from the interrupt vector table.

  1. The ISR for Timer 0 is located starting at memory location OOOBH since it is
    small enough to fit the address space allocated to this interrupt.
  1. We enabled the Timer 0 interrupt with “MOV IE, #10000010B” in MAIN.
    1. While the PO data is brought in and issued to P1 continuously, whenever Timer
      0 is rolled over, the TFO flag is raised, and the microcontroller gets out of the
      “BACK” loop and goes to OOOOBH to execute the ISR associated with Timer 0.
    2. In the ISR for Timer 0, notice that there is no need for a “CLR TFO” instruc
      tion before the RETI instruction. This is because the 8051 clears the TF flag
      internally upon jumping to the interrupt vector table.
Example 11-2
Write a program that continuously gets 8-bit data from PO and sends it to PI while simultaneously creating a square wave of 200 (as period on pin P2.1. Use Timer 0 to create the square wave. Assume that XTAL =11.0592 MHz.

In Example 11-2, the interrupt service routine was short enough that it could be placed in memory locations allocated to the Timer 0 interrupt. However, that is not always the case. See Example 11-3.

Example 11-3
Rewrite Example 11-2 to create a square wave that has a high portion of 1085 us and a low portion of 15 us. Assume XTAL = 11.0592 MHz. Use Timer 1.

Notice that the low portion of the pulse is created by the 14 MC (machine cycles) where each MC = 1.085 us and 14 x 1.085 us = 15.19 us.

Example 11-4

Write a program to generate a square wave of 50 Hz frequency on pin PI .2. This is similar to Example 9-12 except that it uses an interrupt for Timer 0. Assume that XTAL = 11.0592MHz.

Next post:

Previous post: