Timer1 calculation?

Discussion in 'Embedded Systems and Microcontrollers' started by p80nf10, Mar 21, 2010.

  1. p80nf10

    Thread Starter New Member

    Mar 21, 2010
    1
    0
    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.
     
  2. ATM

    Member

    Oct 8, 2009
    31
    0
    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.
     
  3. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    Do you want to trigger an interrupt or JUST a delay the next instruction?
     
  4. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    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.
     
  5. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    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
     
  6. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    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.
     
  7. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    That sure takes the fun out of it! ;)

    And it also gets you moving along.
     
Loading...