PIC18 Timer2 weirdness

From: Toby Thain (toby_at_telegraphics.com.au)
Date: 10/29/04


Date: 29 Oct 2004 10:32:03 -0700

Hi,

I'm seeing something unexpected in the running time of a Timer2 based
delay loop. With the combined prescale, period, and postscale, should
be setting TMR2IF at 500Hz? I expect the polling loop below to fall
out after approx 2ms. But it's taking closer to 3.8ms* and I can't
think of an explanation.

Part is PIC18F6680, Fosc = 40 MHz.

; THIS DOESN'T WORK...
; Timer2 input = Fosc/4 = 10,000,000 Hz
; divide by 1/16 prescale = 625,000 Hz
; divide by period (156) = ~4006 Hz
; divide by 1/8 postscale = ~500 Hz = approx 2ms!
delay2ms: ; set up Timer2
        movlw b'01110010' ; 1:8 postscale,timer off,1:16 prescale
        movwf T2CON
        clrf TMR2
        movlw 156 ; 156 x 8 x 16 cycles = ~1996.8us = approx 2ms
        movwf PR2
        bcf PIR1,TMR2IF
        bsf T2CON,TMR2ON ; start it!
          btfss PIR1,TMR2IF ; check for timer overflow
          bra $-2
        return

*The running time is measured using Timer0, and I have confidence in
the correctness of that timer, having verified expected durations for
sanity with simple cycle wasting loops.



Relevant Pages

  • Re: Bash script Q
    ... > I need to parse this to extract the title number, running time, and number ... > of audio streams. ... > file and extract that information. ... > and loop through so I can generate my menus.... ...
    (comp.os.linux.misc)
  • Re: STL - Vector - Normalization ?
    ... MaxMin(): max, min ... Vectormyvector; ... please measure the running time of this solution vs. ... there's no time saved in the instructions of the body of the loop. ...
    (comp.lang.cpp)
  • Re: Ada Shootout program for K-Nucleotide (patches)
    ... consistently shaves 6% off the running time on my machine. ... The 125 Meg array (Buffer) is read through from beginning ... The string Key.Data already contains most of the desired ... Buffer'Last loop ...
    (comp.lang.ada)
  • Re: The METHOD of creating RSA key
    ... Are you referring to the running time? ... his code is slower because he copied my code and he added some extra useless operations. ... > taken by the while loop. ... sieve that represents odd numbers only. ...
    (sci.crypt)