Timer Questions


Closed Thread
Results 1 to 10 of 10

Thread: Timer Questions

Hybrid View

  1. #1
    Join Date
    Oct 2004
    Location
    Hangover, Germany
    Posts
    289


    Did you find this post helpful? Yes | No

    Default

    As an example:

    Timer1 (16 Bit) is used and feed with:

    I use an 4MHz resonator.
    f_OSC/4 (this is the "normal" countrate für a PIC!)
    Presacler: 1/8 (Set in T1CON or so)

    Then I get 4.000.000 / 4 = 1.000.000
    / 8 = 125.000 counts per second

    So, i can handle up to 0,5 seconds (62500 counts) in this 16-bit-timer.

    I load the Timer1 with -62500 (=0BDCh), clear TMR1IF and start TIMER1.

    When it reaches 0000, the TMR1IF is set, I will stop TIMER1, count down my WORD-variable in order to build the watchdog-timer, clear the TMR1IF and start TIMER1 again.

    If you are using higher speeds, then the numbers must be changed.

    It is quick-n-dirty !
    PBP 2.50C, MCS+ 3.0.0.5, MPLAB 8, MPASM 5.14, ASIX Presto, PoScope, mE mikroBasic V7.2, PICKIT2

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    With an 8MHz osc, Timer1 will increment by 1 every 500nS with a prescaler
    of 1:1.

    It's a 16-bit timer so it can count from 0 to $FFFF or 0 to 65,535. Once it
    reaches 65,535 and rolls-over back to 0, it will generate the interrupt if the
    interrupt is enabled.

    That means you have 65,536 Timer1 ticks before roll-over. 65,536 * 500nS
    = 32.768mS before each Timer1 interrupt.

    1 second/32.768mS = 30.517. Once Timer1 has over-flowed 30 times, you
    know a 30 * 32.768mS (0.98304 seconds) period has expired.

    If 0.98304 seconds is close enough to your "approximate" period of 1 second,
    then you could do something simple like this;

    Setup Timer1 with a 1:1 prescaler, clear Timer1, start the timer. Then count
    each over-flow by monitoring the Timer1 interrupt flag bit. PIR1.0.

    Just monitor PIR1.0. Once this flag bit is set, clear it, then increment your
    variable;
    Code:
    IF PIR1.0 THEN
        PIR1.0 = 0
        MyVar = MyVar+1
    ENDIF
    If MyVar = 30 & no serial data has been received from your external device,
    then reset the external device.

    If the external device responds before MyVar = 30, then reset MyVar to 0 to
    restart your countdown.

    You can do this without interrupts by just watching the Timer1 interrupt flag
    bit. This flag bit is automatically set each time Timer1 rolls-over, whether you
    have the Timer1 interrupt enabled or not.

    If you need more time, then just use the prescaler.

    Example: Setup Timer1 prescaler to 1:8. This gives you 8 * 65,536 * 500nS
    (8 * 32.768mS) for 0.262144 seconds before the flag bit is set. You would
    then only need to increment your variable to 4 (4 * 0.262144 = 1.048576
    seconds) for your approximate 1 second period.

    You only need a 1 byte variable for the over-flow counter, your program
    never gets interrupted while it's doing other things, and you'll be darn close
    to your 1 second period before resetting the external device.

    If you need better precision, you can let the timer free run and roll-over x
    number of times until the roll-over count is just short of your required time
    period.

    After x number of roll-overs, stop the timer, and load it with a value that will
    cause the last roll-over at the remaining time required for your 1 second
    period.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

Similar Threads

  1. Elapsed Timer Demo
    By Darrel Taylor in forum Code Examples
    Replies: 111
    Last Post: - 29th October 2012, 17:39
  2. High Resolution Timer & Speed Calculator
    By WOZZY-2010 in forum Code Examples
    Replies: 4
    Last Post: - 7th February 2010, 16:45
  3. Timer + rc5
    By naga in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 19th November 2009, 07:56
  4. Timer interrupt frequency
    By Samoele in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 17th January 2009, 23:49
  5. timer interupt help 16f73
    By EDWARD in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 3rd July 2005, 08:41

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts