TIMERS AND COUNTERS

If you have no knowledge about timers, you should read this chapter a few times. However, there is some repetition in the other chapters to allow each part of the book to stand as an independent resource.

Most users will find that using the timers and the counters is the hardest part of learning how to use PIC microcontrollers. With this in mind, we will proceed in a step-by-step manner and build up the programs in pieces that are easier to digest. Once you get comfortable with their setup procedures, you will find that timers and counters are not so intimidating.

We will cover timers and counters separately. Counters are essentially timers that get their clock input from an outside source. There are two counters in the 16F877A, and they are associated with Timer0 and Timer1. Timer2 cannot be used as a counter because there is no input line (internal or external) for this particular counter.

Note The clock frequency utilized by the timers is a fourth of the oscillator fre- quency. This is the frequency of the instruction clock. This means that the counters are affected by every fourth count of the main oscillator. The frequency is referred to as Fosc/4 in the data sheet. When responding to an external clock signal the response is to the actual frequency of the external input. (For now, we’ll run the LAB-X1 and thus all the programs at 4 MHz, so all the timers will be getting internal inputs at 1 MHz.)

Caution The PICBASIC PRO compiler generates code that does not respond to interrupts while a compiled instruction is being executed. Therefore long pauses (long enough to lose an interrupt signal, which depends on how the timer is set up) can lead to lost interrupts if more than one interrupt occurs during the pause. Since interrupts are used for the express purpose of handling critical response/ timing needs, this is most undesirable. Therefore, PAUSE commands should be used with care. The program samples provided in this chapter give examples of how to use short pauses in loops to get a long pause.

Leave a comment

Your email address will not be published. Required fields are marked *