The PICmicro contains a special register called a timer register. Like all the other registers in the PICmicro it is 8 bits in size, which means that it can hold values in the range 0-255. Tne PIC16F88 we are using has three, Timer0, Timer1 and Timer 2, which are controller by the OPTION, T1CON, and T2CON registers respectively.

These timer are actually a counter in that it is able to automatically count up when triggered. It can be triggered from an external source (for example if you wanted to use a PIC microcontroller to count pulses) or it can be triggered by the PIC microcontroller clock, if you want to use it to keep track of time.

Because the timer is only 8 bits wide it will overflow when it is incremented past the maximum value which it can hold, i.e. when it goes from 255 back to 0. By setting up INTCON you can configure the PIC microcontroller to generate interrupts when the timer overflows. This is a very popular way to set up a "heartbeat" for your system. By setting the timer running you can get the PIC microcontroller to repeat an action at a particular interval.

The rate at which the timer overflows depends on how fast it is "clocked". You can select the clock source between the PIC microcontroller internal instruction clock and an external signal. You can also control the pre-scaler which can be used to divide the internal signal by a particular power of two. This means that you can use the timer to count longer periods of time (or numbers of pulses) by setting the pre-scaler to an appropriate value.

We will do this in a lot of the practical work.

On the right you can see how the OPTION register is configured with respect to the timer. Remember that the INTCON register is used to enable timer interrupts and also lets you test for timer interrupt flags.

If you want more precise control over the frequency of interrupts caused by the timer you can write a value to the timer register at any time. This will cause it to count up from the new value. However, remember that it takes the PIC microcontroller 2 instruction cycles to "notice" this change and so if you are after really precise timing you should write a value 2 less than the one you really want. Of course the fact that the PIC microcontroller can be driven from a very stable crystal means that you can use these techniques to produce extremely accurate pulse timings, should you need to.

 

OPTION - OPTION Control register

Bit 7

Bit 6

Bit 5

Bit 4

Bit 3

Bit 2

Bit 1

Bit 0

!RBPU

INTDG

T0CS

T0SE

PSA

PS2

PS1

PS0

Items in red are not relevant to the timer.

T0CS - TIMER0 Clock Source

If this bit is clear it tells the PICmicro to use bit 4 of PORTA as the clock source. You would clear this bit if you wanted to use the timer to count pulses. The timer will increment when it sees an edge on the input signal. If you want to increment on a rising (i.e. low to high transition) edge you must clear bit T0SE. To increment on a falling edge you must set bit T0SE. Note that the timer will generate interrupts (if enabled) irrespective of the clock source. You must of course have configured this port as an input (using TRISA) for this to work.

If this bit is set it tells the PICmicro to use the instruction clock as the clock source. This is updated each time the PICmicro finishes executing an instruction. The PICmicro takes four clock pulses to complete an instruction, and so the clock is updated at 1/4 of the speed of the crystal. For the PICmicro development board, which has a 19.6608 MHz crystal, this means that the clock updates at 4,915,200 Hz.

T0SE - TIMER0 Source Edge

This bit selects the edge direction of signals which will cause the counter to count if the source is set to bit 4 of PORTA. If the bit is set the timer will increment on the rising edge of a pulse. If the bit is clear the timer will increment on the falling edge of a pulse. If the timer is set to use the instruction clock as the source this bit has no meaning.

PSA - PreScaler Assignment Bit

The pre-scaler lets you control the rate at which the timer counts. It divides the timer input frequency by a particular power of 2, so that you can slow down the clock. There is only one prescaler on the PICmicro, and it is shared between the watchdog timer (of which more later) and the timer. If this bit is cleared the PSA is assigned to the clock.

PS2, PS1, PS0 - PreScaler control bits

These bits allow you to select one of eight pre-scaler values. These are the numbers which the clock is divided by. The assignments are as follows:

PS2

PS1

PS0

divide by

0

0

0

2

0

0

1

4

0

1

0

8

0

1

1

16

1

0

0

32

1

0

1

64

1

1

0

128

1

1

1

256

For information on using the other timers, consult the PIC Microcontroller datasheet.