Timer1 calculation?

Thread Starter

p80nf10

Joined Mar 21, 2010
1
hai,
I write a auto step up program in pic 16f877A c level language.This program working is ok.I think to add a timer delay 5 sec in this program. how calculate this time? I used 20mhz crystel.please help me.
 

ATM

Joined Oct 8, 2009
31
You want the Timer 1 register to overflow every 5 seconds and generate an Interrupt?

Timer1 is a 16bit register made up of Timer1H and Timer1L, for it to overflow it must exceed the value of 65,535. With a 20MHz crystal, the Timer1 register is going to overflow in (1/20,000,000) x 65535 = 3.28 milliseconds.

So you're gonna have to either use a combination of pre-scalers and pre-loading Timer1 register with a value or consider using a slower crystal.
 

AlexR

Joined Jan 16, 2008
732
The maximum delay you can get from timer1 running from a 20MHz clock is about 104.8mSec. If you make the timer trigger an interrupt you could adjust the delay down to 100mSec and count 50 interrupts to get a 5 sec delay or you can run timer1 from its own lower frequency crystal and adjust the register settings to get the 5 second delay directly.
 

AlexR

Joined Jan 16, 2008
732
.....or you can set prescaler to 1:256; load timer with 46004 (at each overflow) and you will get an overflow every 1000.0398 milliseconds. Set a variable to count up to 5, inside the IRH and you will have your 5 secs delay almost in the background.

Alberto
Only problem there is that the maximum prescaler value for timer1 is 1:8, other than than that minor point its a great idea.:D
 

retched

Joined Dec 5, 2009
5,208
You can use a code based timer. Check if you have a wait, or pause command with your compiler. You can also do an if then loop for 100,000,000 nops that will give you ~5sec delay.
 
Top