Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC Timers - maximum time

Status
Not open for further replies.

gregmcc

Member
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.

Does this sound correct?
 
Last edited:
Pic's take 4 instruction cycles before instruction execution (4MHZ /4= 1MHZ) what pic are you using??
 
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.

Mike.
 
16F628. I think I took that into my calculations.

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.:)

EDIT: Mike beat me too it while I was typing..!!
 
Last edited:
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 :)
hi greg,
If you are already reading the i/o port once second, why not count 60 of those.??
 
I would use timer 2 as this will run automatically. With T2CON=0x4d and PR2 = 249, bit 1 of PIR1 will magically get set 100 times per second.

Mike.
 
If you are already reading the i/o port once second, why not count 60 of those.??


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'll give the Timer2 shot. Thanks.
 
If all you need to do is count the number of times the line goes high, than you can use the timers as counters with interrupts enabled.
 
Last edited:
MPLAB is good for that

Hola G,

The MPSIM included in the MPLAB environment could help you to test quite quickly any period obtained using any of the available timers.

Make sure to learn and use the "stopwatch" function.
 
Thanks - I'll look into the stopwatch function. Got the code working last night with Timer1. Was actually easier than I thought and worked first time :)
 
Thanks - I'll look into the stopwatch function. Got the code working last night with Timer1. Was actually easier than I thought and worked first time :)

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'll give the Timer2 shot. Thanks.

I think Timers used as counters would be the best solution. If you want to measure the pulse width than use the capture module.
 
Status
Not open for further replies.

Latest threads

Back
Top