仕事ーtimer0 calculator
timer0 calculator
Hi,
Its very easy...
First and most importnat is that you will know that an interupt on the timers will happen only when they reach the end, i.e. 0xFFFF +1.
So lets says this, you are using external OSC at 4MHz, and your times dosnt have any prescalar or postscale set. and you are using the OSC as timer.
So to get an interup, you first need to know how fast the timer will incramet. this is done by diving the OSC by 4 ( thats the MIPS of the PIC ) and then cahnge is to the time domain:
f = OSC/4 = 4,000,000 / 4 = 1,000,000 or 1MHz
To the time domain:
t = 1/f = 1/1,000,000 = 0.000001 Sec = 1uSec
So your timer will incrament every 1uSec. To get to 1mSec you will have to do:
1mSec/1uSec = 1000 clock cycles.
Now we need to load the timer with this settings, as i said before the timer will interupr only with it will reach the end, so we need to take the end and subtract the clock cycle that will pass, i.e.:
TMR0 = 0xFFFF - (time we want ) = 0xFFFF - 0x03E8 ( 1000 in Hex ) = 0xFC17
This is very easy to do with the PC calculator, that enable you to change from Dec. to Hex. in one button.
So the answer we have is 0xFC17. Load this value each time to your timer and then you will get an interupt every 1mSec.
Now you can play around and figure out, what is the max time you can get. And i you use the pre-scalare the your original clock wile have to divid even more. See the drawing in the data sheet from more info.
No comments:
Post a Comment