• AVR Freaks

Timer2 Discrepancy on PIC16F628A

Author
Kwooda
New Member
  • Total Posts : 3
  • Reward points : 0
  • Joined: 2019/06/16 21:51:02
  • Location: 0
  • Status: offline
2019/06/19 15:07:42 (permalink)
0

Timer2 Discrepancy on PIC16F628A

I have a PIC16F628A running at 12MHz and I wanted to create an approximate 20ms timer. At 12MHz, and an instruction cycle of 4 clock cycles, this works out to be 3,000,000 instructions per second, or 60,000 instructions per 20ms. Since Timer2 advances with the instruction cycle (FOSC/4), I felt it was perfect for counting out 60,000 instructions. 
 
The specs indicate I can set a prescaler to 1:16 as well as a postscaler to 1:16, giving a total divisor of 16x16 or 1:256. Dividing 60,000 by 256 leaves 234.375. Rounding down to 234, if I count 234 * 256 instructions, that's 59,904, getting me very close to my desired 20ms interval. So, I set PR2 = 234 (233, actually, since it wraps after counting 0-233, giving 234 total count), fully expecting to have my 20ms interval. 
 
However, after running this, I get an 80ms interval -- 4x longer than expected. Looking at the prescaler, there are two bits that can select 00 = 1:1, 01 = 1:4, or 1x = 1:16. I set the bits to 11, for the 1:16 setting. But I'm curious if maybe the specs don't mention that maybe 10 = 1:16, and 11 = 1:64? Could that be possible? I haven't tried setting the bits to 10, and now I'm out of town so can't try it, but I'm scratching my head trying to determine where the discrepancy is. I originally thought maybe I wasn't taking into account the fact that Timer2 is driven by FOSC/4, but if it was driven directly by the clock, it would be 4x shorter than expected.
 
I get around the problem by setting the prescaler to 1:4, but this doesn't explain the discrepancy. Is there something wrong with my thought process on this, or is this a genuine issue? If the prescale and postscale are truly 1:16, and I set PR2 to 233 (0xE9), that should be 16x16x(233+1) = 59904 instructions. 59904 instructions with a 12MHz clock = 1/(12Mhz/4) * 59904 = 19.968ms.  Right???
 
#1

6 Replies Related Threads

    ric
    Super Member
    • Total Posts : 22757
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Timer2 Discrepancy on PIC16F628A 2019/06/19 15:27:39 (permalink)
    0
    I agree with your calculations.
    How are you measuring the interval?
    What interval are you getting with the 1:4 prescale value?
     
    Could you show us the exact code you are using to test this?
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    Kwooda
    New Member
    • Total Posts : 3
    • Reward points : 0
    • Joined: 2019/06/16 21:51:02
    • Location: 0
    • Status: offline
    Re: Timer2 Discrepancy on PIC16F628A 2019/06/19 16:13:55 (permalink)
    0
    I'll see about getting some code together in the next day or two. I measured it on an oscilloscope, with a "debug" pin that I pulsed on Timer2 wrap. Because it was 4x longer than expected, I reduced the prescale from 1:16 to 1:4 and am getting the desired 20ms. I actually encountered this a while back, and rediscovered it after reading through my development notes (it's for a circuit that samples and reproduces pulse width signals from an old RC receiver). I'm making a revised version of it, and noted that Timer2 was still configured for 1:4 + 1:16 pre- and post-scalers and working as it should. The 20ms loop is used as a timeout when sampling input signals, which come at approx. 17-18ms intervals.
     
    I'm out of town and away from my setup, so will run some tests when I get back. I was just curious if there was any known issue with Timer2 that I had not come across in the meantime.
    #3
    Kwooda
    New Member
    • Total Posts : 3
    • Reward points : 0
    • Joined: 2019/06/16 21:51:02
    • Location: 0
    • Status: offline
    Re: Timer2 Discrepancy on PIC16F628A 2019/06/19 16:40:03 (permalink)
    +1 (1)
    OK, I have a copy of the code with me, and I think there must be a discrepancy in my notes, because the comments in the code do not describe the actual settings. Here's from my code:
     
    ; Timer2 1:16 pre- and post-scalers together should yield 1:256. But for some reason, it was 4x too long.
    ; 1:4 prescaler + 1:16 postscaler yields the 1:256 that is needed, though not sure how...
    mPR2      EQU 0xE9 ;Timer2 period register setting (0xE9 with 1:4 prescaler and 1:16 postscaler gives ~20ms period)
    mT2CON EQU 0x7E ;Timer2 1:4 prescaler + 1:16 postscaler (= 1:256 scaler) - Timer2 on
     
    The notes say 1:4 prescaler, but T2CON is set to 0x7E, which selects a 1:16 prescaler. So, apparently something was amiss in my understanding of what I was doing at the time. Oh, well - mystery solved! Looks like the PIC is doing what it should be doing. Time to update my notes. :-)
     
    Thanks for the response!
     
    #4
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11218
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Timer2 Discrepancy on PIC16F628A 2019/06/19 16:40:04 (permalink)
    0
    11 = 1:64? Could that be possible? I

     
    No.
     
    I was just curious if there was any known issue with Timer2

     
    No.
     
    How sure are you that you're running at 12 MHz?
    #5
    ric
    Super Member
    • Total Posts : 22757
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Timer2 Discrepancy on PIC16F628A 2019/06/19 16:46:43 (permalink)
    +1 (1)
    Kwooda
    The notes say 1:4 prescaler, but T2CON is set to 0x7E, which selects a 1:16 prescaler. So, apparently something was amiss in my understanding of what I was doing at the time. Oh, well - mystery solved! Looks like the PIC is doing what it should be doing. Time to update my notes. :-)
     

    That's why we ask to see the code, so we can see what you ARE doing, not what you think you're doing... ;)
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    dan1138
    Super Member
    • Total Posts : 3123
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: Timer2 Discrepancy on PIC16F628A 2019/06/19 17:23:06 (permalink)
    +1 (1)
    TIMER2 in the PIC16F628A the prescaler can be 1:1, 1:4 or 1:16
     
    To get TIMER2 to assert an interrupt request after 60,000 instruction cycles:
    • Set the prescaler at 1:16
    • Set the PR2 to the value of 249 (reload after 250 clocks)
    • Set the postscaler at 1:15
    The total cycle count is 16*250*15  = 60000.
    #7
    Jump to:
    © 2019 APG vNext Commercial Version 4.5