Note: "When writing to TMR0, two instruction clock cycles are lost. Often you have a speciﬁc time period you want to count, say 100 decimal. In that case you might put 156 into TMR0 (256 - 100 = 156). However, since two instruction cycles are lost when you write to TMR0 (for internal logic synchronization), you should actually write 158 to the timer".[source mid-range reference manual : DS31011]
Note: If you look at the Timer 0 block diagram in the datasheet the internal clock synchronization uses Fosc/4, so do not add 2 when calculating the period if you are not using the internal clock as a clock source! The main calculation will use your external clock and you need to add 2 Fosc/4 cycles to that period.
TCL Scripts for PIC Timer calculation
Every time you use any built in PIC timer you have to do these type of calculations and the hardware in each timer is different and you'll also have to do battle with interrupts.
To make the process easy you can use three on-line (free) interactive script modules written in TCL (Tool Command Language). They operate in a similar way to a Java applets.
Each of these scripts is geared towards the most typical use of each timer and lets you change prescaler, postscaler or register value using slider controls. This makes it easy to experiment with different values as the result is immediately displayed in the web page (frequency and period are displayed from each part of the timer e.g. after the prescaler, after the register, after the postscaler).
The above calculation is now trivial just move the sliders around until you get close to your desired PIC timer period and then adjust the timer value to home in on the correct answer.
So you can do what-if type operations (all without a calculator in sight) e.g. 'I need a 15ms repeat rate'.
From Timer 2 the closest I could get is 15.136 (took 1 minutes to test) - perhaps I'll try timer 1 - Ah that gives an exact 15ms (took 30 seconds to test). Just check with Timer 0 - this gives 15.040ms (took ~30 seconds to test).
Of course you can also set the main clock frequency (internal or external crystal) as well.
PIC Timer 0
This has an 8 bit prescaler and an 8 bit timer and can be driven from an external clock.
Prescaler : 8 bit
Timer register : 8 bit
Link to Timer 0 calculator
Note: In 18F Series devices Timer 0 is enhanced to be 16 bit capable
(although it is backwards compatible with the 8 bit version).
PIC Timer 1
This has a 4 bit prescaler and an 16 bit timer and can be driven from an external clock. It can also be driven from a slow speed crystal e.g. 32kHz.
Prescaler : 4 bit
Timer register: 16 bit
Link to Timer 1 calculator.
Note: In 18F Series devices Timer1 has enhanced 16 readability (High byte is
double buffered to allow easy, exact, capturing of the timer register value.
PIC Timer 2
This has a 4 bit prescaler and an 8 bit timer and an 8 bit period register and is only driven from the internal clock (Fosc/4)
Prescaler : 2 bit (1:1, 1:4, 1:16)
Timer register: 8 bit
Period register : 8 bit
Postscaler : 4 bit (1:1 to 1:16 inclusive)
Note: In 18F Series devices Timer 2 has the same structure as in 16F versions.
Link to Timer 2 calculator.
Claim Your: Useful
"Arduino Software Guide"
Use an Hitachi HD44780 as a simple display for any project.
How to make a Pulse Rate Sensor using a simple single opamp circuit with an Arduino and a few other components.
74HC595 : How to add nearly unlimited outputs to any microcontoller.