TMR0 Confusion


Closed Thread
Results 1 to 16 of 16

Thread: TMR0 Confusion

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: TMR0 Confusion

    With Timer0 on 16F's, there's no reason to stop the timer while reloading since it's an 8-bit timer.

    With 16-bit timers there is the slim possibility that the lowbyte will overflow to the highbyte at the same time you are reloading it.
    But that can't happen with an 8-bit timer, so just reload the 8-bit value and don't worry about it.
    DT

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: TMR0 Confusion

    Thanks it's working on the bench now just need to check my TMR0 calculations.

    Using http://eng-serve.com/pic/pic_timer.html I get a prescaler of 1/4 and preload of 6 giving 500us interrupts (2khz) so that gives me 250 ticks per cycle with 8mhz clock.

    That works out at 2.5ticks per 1% duty.
    So for 10% duty I need to load 25 + 6 into TMR0 for the 90% off part of the cycle? and 225 + 6 for the 10% on part of the cycle?
    Does that look sound right?

    So my modified code looks like this.

    Code:
        TMR0_ON_TICKS = DutyCycle           '(DutyCycle must be between 25 - 225 (10-90%)
        TMR0_OFF_TICKS = 250 - TMR0_ON_TICKS
        TMR0_ON_VAL = 250 - TMR0_ON_TICKS + 6
        TMR0_OFF_VAL = 250 - TMR0_OFF_TICKS + 6
    Last edited by retepsnikrep; - 4th August 2011 at 07:32.

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: TMR0 Confusion

    Although this appears to be working in my application I'm confused by

    PEIE var INTCON.6 'Peripheral Interrupt enable

    (Does this need to be on for TMR0 interrupt?) I currently have it enabled.

    I understand PEIE needs to be enabled for TMR1 but TMR0?

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: TMR0 Confusion

    Appears not as seems to be working without it set?

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: TMR0 Confusion

    In the Interrupts section of the datasheet (under SPECIAL FEATURES OF THE CPU), there is a diagram that shows the Interrupt Logic.

    Only the peripherals in the red box are affected by PEIE.

    Name:  PEIE.gif
Views: 1297
Size:  19.2 KB
    DT

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: TMR0 Confusion

    Thanks Darrel that's very clear, little by little i'm learning.

  7. #7


    Did you find this post helpful? Yes | No

    Default Re: TMR0 Confusion

    How long to execute the reload?

    I'm now using the timers for a few things but how do you work out the value to add on to the reload value for the time it takes to do the reload?

    Code:
     
     T1CON.0 = 0            'Stops Timer
     
     TMR1RunOn.Highbyte = TMR1H    'Load the Run-On (Over-Run) value (if any)
     TMR1RunOn.Lowbyte = TMR1L   'Load the Run-On (Over-Run) value (if any)
     TMR1RunOn = PreLoad + TMR1RunOn   'Calculate new Preload setting to include any timer overrun 
     TMR1H = TMR1RunOn.HighByte   'Load the timer with new value
     TMR1L = TMR1RunOn.LowByte      'Load the timer with new value
     PIR1.0 = 0     'Clear TMR1 int flag 
         T1CON.0 = 1            'Starts Timer
    The above for instance how much to add to keep the accuracy? How long does that all take? Any rough estimates? It's a pretty standard reload code but I could not find any examples. I'm running at 8mhz with 1/8 prescaler.

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