Texas Instruments MSC1210 User Manual
Page 86
Using Timers as Event Counters
8-12
8.4
Using Timers as Event Counters
We have discussed how a timer can be used for the obvious purpose of keep-
ing track of time. However, the MSC1210 also allows the use of timers to count
events.
This can be useful in many applications. For example, a sensor is placed
across a road that would send a pulse every time a car passes over it. This
could be used to determine the volume of traffic on the road. The sensor is at-
tached to one of the MSC1210 I/O lines and constantly monitored, detecting
when it pulses high, and the counter incremented when it goes back to a low
state. This is not terribly difficult, but requires some code. If the sensor is
hooked to P1.0, the code to count passing cars would look something like this:
JNB P1.0,$
;If a car hasn’t raised the signal,
;keep waiting
JB P1.0,$
;The line is high, car is on the sensor
;right now
INC COUNTER
;The car has passed completely, so we count it
As shown, it is only three lines of code. However, what if other processing
needs to be done at the same time? The program cannot be stuck in the JNB
P1.0,$ loop waiting for a car to pass if it needs to be doing other things. What
if the program is doing other things when a car passes over? It is possible that
the car will raise the signal and the signal will fall low again before the program
checks the line status; this would result in the car not being counted. Of course,
there are ways to get around even this limitation, but the code quickly becomes
big, complex, and ugly.
Luckily, the MSC1210 provides a way to use the timers to count events. It is
painfully easy. Only one additional bit has to be configured.
Timer 0 can be used to count the number of cars that pass. In the bit table for
the TCON SFR, there is a bit called C/T0—it is bit 2 (TCON.2). Reviewing the
explanation of the bit, we see that if the bit is clear, Timer 0 will be incremented
every instruction cycle. This is what has already been used to measure time.
If C/T0 is set, however, Timer 0 will monitor the P3.4 line. Instead of being in-
cremented every machine cycle, Timer 0 will count events on the P3.4 line. So
in this case, simply connect the sensor to P3.4 and let the 8052 do the work.
Then, when the number of how many cars have passed is desired, just read
the value of Timer 0—the value of Timer 0 will be the number of cars that have
passed.
So what exactly is an event? What does Timer 0 actually count? Speaking at
the electrical level, the MSC1210 counts 1−0 transitions on the P3.4 line. This
means that when a car first runs over the sensor, it raises the input to a high
(1) condition. At that point, the MSC1210 does not count anything because this
is a 0−1 transition. However, when the car has passed, the sensor falls back
to a low (“0”) state. This is a 1−0 transition and at that instant the counter is
incremented by 1.