sohaib_a said:
Need some help with the timer settings...I know that it increments every instruction cycle (Tosc x 4) but I cant figure out what the pre-scaler and post scaler do...what do the ratios mean?
OK, here goes... If anyone sees errors in my explanation, feel free to jump in.
Fosc is your clock, or crystal speed. The timer gets that clock divided by four. So if you have a 20MHz crystal, then Fosc/4 = 5MHz, or 5,000,000 cycles per second.
With a 1:1 prescale, the timer increments once per Fosc/4. We'll call Fosc/4 "clock" from now on.
With a 1:16 prescale, the timer increments once every 16 clocks. With a 1:256 prescale, the timer increments once every 256 clocks. You get the idea, I'm sure. Those bigger prescales give longer timer periods between interrupts.
Then with a 1:1 postscale, every time the timer rolls over it triggers an interrupt.
With a 1:16 postscale, an interrupt isn't triggered until 16 rollovers. This again makes the timer period longer between interrupts.
Just in case you don't understand what I mean by rollover, here it is: The timer is an 8 bit register or a pair of 8 bit registers forming a 16 bit register. An 8 bit timer counts up to 255 ($ff),
rolls over to 0 and triggers an interrupt as it rolls over. Then it starts counting up again. A 16 bit timer counts up to 65535 ($ffff) and rolls over to 0 as it triggers an interrupt.
With some scratch paper and a calculator you can calculate the timer periods very accurately. At first it'll seem very difficult, but you'll soon catch on to how to figure it.
Then you can do stuff like set the timer value to the number that gives you the timer period you need. In your interrupt service routine you just reset it to that value every time it rolls over. Or you can have the value vary according to the time period you need, like when using a timer to generate a variable pulse train for a servo.
Oh ya, some timers can use multiple different clock sources too. You have to choose how you want your timer clocked when you set up.