Using Timers to Measure Time

However, when dealing with a 13-bit or 16-bit timer, the chore is a little more complicated. Consider what happens when the low byte of the timer is read as 255, then the high byte of the timer is read as 15. In this case, what actually happens is that the timer value is 14/255 (high byte 14, low byte 255) but the readout is 15/255. The reason for this is because the low byte was read as 255. However, when the next instruction is executed, enough time passes for the timer to increment again, which rolls the value over from 14/255 to 15/0. In the process, the timer is read as being 15/255 instead of 14/255. Obviously, this is a problem.

The solution is not complicated. Read the high byte of the timer, then read the low byte, then read the high byte again. If the high byte read the second time is not the same as the high byte read the first time you repeat the cycle. In code, this would appear as:

REPEAT:

MOV A,TH0

MOV R0,TL0

CJNE A,TH0,REPEAT

...

In this case, the accumulator is loaded with the high byte of Timer 0. Then R0 is loaded with the low byte of Timer 0. Finally, the high byte we read out of Timer 0—which is now stored in the accumulator—is checked to see if it is the same as the current Timer 0 high byte. If it is not, that means it just rolled over and the timer value must be reread, which is done by going back to REPEAT. When the loop exits, the low byte of the timer is in R0 and the high byte is in the accu- mulator.

Another much simpler alternative is to simply turn off the timer run bit (i.e. CLR TR0), read the timer value, and then turn on the timer run bit (i.e. SETB TR0). In this case, the timer is not running, so no special tricks are necessary. Of course, this implies that the timer will be stopped for a few instruction cycles. Whether or not this is tolerable depends on the specific application.

8.3.6.2Detecting Timer Overflow

Often it is only necessary to know that the timer has reset to 0. That is to say, there is no particular interest in the value of the timer, but rather an interest in knowing when the timer has overflowed back to 0.

Whenever a timer overflows from its highest value back to 0, the microcontroller automatically sets the TFx bit in the TCON register. This is useful because, rather than checking the exact value of the timer, you can just check if the TFx bit is set. If the TF0 bit is set, it means that Timer 0 has overflowed; if TF1 is set, it means that Timer 1 has overflowed.

8-10

Page 84
Image 84
Texas Instruments MSC1210 manual Detecting Timer Overflow