COUNTER PROGRAMMING

SECTION 9.2: COUNTER PROGRAMMING
In the last section we used the timer/counter of the 8051 to generate time delays. These timers can also be used as counters counting events happening outside the 8051. The use of the timer/counter as an event counter is covered in this section. As far as the use of a timer as an event counter is concerned, everything that we have talked about in programming the timer in the last section also applies to programming it as a counter, except the source of the frequency. 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. In counter mode, notice that the TMOD and TH, TL registers are the same as for the timer discussed in the last section; they even have the same names. The timer’s modes are the same as well.
C/T bit in TMOD register
Recall from the last section that the C/T bit in the TMOD register decides the source of the clock for the timer. If C/T = 0, the timer gets pulses from the crystal. In contrast, when C/T = 1, the timer is used as a counter and gets its pulses from outside the 8051. Therefore, when C/T = 1, the counter counts up as pulses are fed from pins 14 and 15. These pins are called TO (Timer 0 input) and Tl (Timer 1 input). Notice that these two pins belong to port 3. In the case of Timer 0, when C/T = ], pin P3.4 provides the clock pulse and the counter counts up for each clock pulse coming from that pin. Similarly, for Timer 1, when C/T = 1 each clock pulse coming in from pin P3.5 makes the counter count up.
Table 9-1: Port 3 Pins Used For Timers 0 and 1







Example 9-18
Assuming that clock pulses are fed into pin Tl, write a program for counter 1 in mode 2 to count the pulses and display the state of the TL1
count on P2.




In Example 9-18, we use Timer 1 as an event counter where it counts up as clock pulses are fed into pin 3.5. These clock pulses could represent the number of people passing through an entrance, or the number of wheel rotations, or any other event that can be converted to pulses.
In Example 9-18, the TL data was displayed in binary. In Example 9-19, the TL registers are converted to ASCII to be displayed on an LCD.








Figure 9-5. (a) Timer 0 with External Input (Mode 1)
(b) Tinier 1 with External Input (Mode 1)






Example 9-19
Assume that a 1-Hz frequency pulse is connected to input pin 3.4. Write a program to display counter 0 on an LCD. Set the initial value of THO to -60.
Solution:
To display the TL count on an LCD, we must convert 8-bit binary data to ASCII. See Chapter 6 for data conversion.

By using 60 Hz we can generate seconds, minutes, hours.
Note that on the first round, it starts from 0, since on RESET, TLO = 0. To solve this problem, load TLO with -60 at the beginning of the program.





Figure 9-6. Timer 0 with External Input (Mode 2) Figure 9-7. Timer 1 with External Input (Mode 2)
As another example of the application of the timer with C/T = 1, we can feed an external square wave of 60 Hz frequency into the timer. The program will generate the second, the minute, and the hour out of this input frequency and display the result on an LCD. This will be a nice digital clock, but not a very accurate one.
Before we finish this chapter, we need to state two important points.
  1. You might think that the use of the instruction “JNB TFx, target” to mon
    itor the raising of the TFx flag is a waste of the microcontroller’s time. You
    are right. There is a solution to this: the use of interrupts. By using interrupts
    we can go about doing other things with the microcontroller. When the TF flag
    is raised it will inform us. This important and powerful feature of the 8051 is
    discussed in Chapter 11.
  2. You might wonder to what register TRO and TR1 belong. They belong to a
    register called TCON, which is discussed next.
Table 9-2: Equivalent Instructions for the Timer Control Register (TCON)





ICON register
In the examples so far we have seen the use of the TRO and TR1 flags to turn on or off the timers. These bits are part of a register called TCON (timer control). This register is an 8-bit register. As shown in Table 9-2, the upper four bits are used to store the TF and TR bits of both Timer 0 and Timer 1. The lower four bits are set aside for controlling the interrupt bits, which will be discussed in Chapter 11. We must notice that the TCON register is a bit-addressable register. Instead of using instructions such as “SETB TR1″ and “CLR TR1″, we could use “SETB TCON. 6″ and “CLR TCON. 6″, respectively. Table 9-2 shows replacements of some of the instructions we have seen so far.


The case of GATE = 1 in TMOD


Figure 9-8. Timer/Counter 0
Before we finish this section we need to discuss another case of the GATE bit in the TMOD register. All discussion so far has assumed that GATE = 0. When GATE = 0, the timer is started with instructions “SETB TRO” and “SETB TR1″, for Timers 0 and 1, respectively. What happens if the GATE bit in TMOD is set to 1? As can be seen in Figures 9-8 and 9-9, if GATE = 1, the start and stop of the timer are done externally through pins P3.2 and P3.3 for Timers 0 and 1, respectively. This is in spite of the fact that TRx is turned on by the “SETB TRx” instruction. This allows us to start or stop the timer externally at any time via a simple switch. This hardware way of controlling the stop and start of the timer can have many applications. For example, assume that an 8051 system is used in a product to sound an alarm every second using Timer 0, perhaps in addition to many other things. Timer 0 is turned on by the software method of using the “SETB TRO” instruction and is beyond the control of the user of that product. However, a switch connected to pin P3.2 can be used to turn on and off the timer, thereby shutting down the alarm.





Figure 9-9. Timer/Counter 1

Next post:

Previous post: