With my little knowledge on timers I've started a project which is going to make use of a timer instead a delay loops. I just want to double check on my thinking before I write the code and then wonder why its not working
From my understanding or timers and prescalers:
a 4Mhz PIC with a:
Timer0 with prescaler of 1:256 will increment the counter ever 65ms. ie If I I increment a counter everytime it overflows, the max time I can get out of a 8 bit variable is 256 * 65ms = 16.6s
Timer1 with prescaler of 1:8 will increment the counter every 524ms. Again the max time I can get using a 8 bit variable is 134s
So for example for a 60 second time I could use Timer0 with 1:8 prescaler with a 8 bit variable. When the variable reaches 114 decimal then 60 secs has passed. Give or take a few seconds, its not critical.
PIC microcontrollers: an ... - Google Books
Click on this for code example. Think it would be best to read this text on the subject as you said in the original post you had little knowledge of timers.
Your figures look correct. What I normally do for long delays is setup a 10mS interrupt and count 100ths of a second. Timer2 can be setup to automatically generate the interrupt at 10mS.
If you let us know what you are trying to do and which language you will use we may be able to help more.
hi,
For timer0, with the prescaler at 256 and the register set for the maximum count of 256, that a time multiplication of 65536.
If you consider the time period of the 1mHz internal clock as 1micro-sec, this means a multiplier of 65536 will give 65.536mSec or 0.065536 secs.
So if you want about 60secs then divide 60/0.065536 =915 counts of a incremented WORD counter.
Its easier to use the timer1, with prescaler set to 8, this gives a prescaler output period of 8microsecs.
Set the tmr1 register for its maximum count of 65536, this will give an interrupt every 0.524288 secs, so for a 60sec its 60/0.524288 = 114.4 , which is what you said.
Thanks all the for replies - looks like I'm more or less on the right track. My project reads a i/o port every second - if it's high then I increment a counter. Every 60 seconds I need to upload this value to the computer via serial port.
Is there any benefit for me over using Timer0 and Timer1?
Once I've got the 60 sec routine working I'll work on the serial comms. (I've already got my eye on Nigel's rs232 tut
Thanks all the for replies - looks like I'm more or less on the right track. My project reads a i/o port every second - if it's high then I increment a counter. Every 60 seconds I need to upload this value to the computer via serial port.
Is there any benefit for me over using Timer0 and Timer1?
Once I've got the 60 sec routine working I'll work on the serial comms. (I've already got my eye on Nigel's rs232 tut
I lie - I"m not reading it every second. I'm reading when it goes high, which is normally every second or two. I basically need to read in how many times a line goes high in 60 secs.
I lie - I"m not reading it every second. I'm reading when it goes high, which is normally every second or two. I basically need to read in how many times a line goes high in 60 secs.