PIC18 Timer2 weirdness

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
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.

Re: PIC18 Timer2 weirdness
Quoted text here. Click to load it

Your postscale bits are wrong.


Re: PIC18 Timer2 weirdness
On 29 Oct 2004 19:06:15 GMT, the renowned snipped-for-privacy@aol.com (Gary Kato)

Quoted text here. Click to load it

Yup. Specifically, you're getting 1:15 rather than 1:8.


Best regards,
Spehro Pefhany
--
"it's the network..."                          "The Journey is the reward"
snipped-for-privacy@interlog.com             Info for manufacturers: http://www.trexon.com
We've slightly trimmed the long signature. Click to see the full one.
Re: PIC18 Timer2 weirdness
Quoted text here. Click to load it

Yes, I just found this out too. What a fool. Thanks! :-)
--T

Quoted text here. Click to load it
We've slightly trimmed the long signature. Click to see the full one.
Re: PIC18 Timer2 weirdness


Others have pointed out the postscale problem.  For future reference,
here's an Excel spreadsheet for working out those pre/post-scaler
issues:

http://dhenry.home.sprynet.com/pic_tmr2.xls

--
Dan Henry

Re: PIC18 Timer2 weirdness

Quoted text here. Click to load it

Plus using that spreadsheet, you'll quickly discover that 1:16 pre/1:8
post with PR2 = 156 only gets you close to 500Hz and that 1:16/1:10
PR2 = 125 is spot-on.

--
Dan Henry

Re: PIC18 Timer2 weirdness
Quoted text here. Click to load it

Yes, that imprecision is also due to my misreading of the manual; I
overlooked that Timer2 actually allows all divisors 2-16 (more
familiar with the other timer modules that only have powers-of-2
scales). You know how it is when you stare at something too long! :)
--T

Re: PIC18 Timer2 weirdness
On 30 Oct 2004 09:58:33 -0700, the renowned snipped-for-privacy@telegraphics.com.au

Quoted text here. Click to load it

The comments that indicate what you *wanted* the code to do can impede
troubleshooting. Maybe they should all left off 'til the end. ;-) ;-)


Best regards,
Spehro Pefhany
--
"it's the network..."                          "The Journey is the reward"
snipped-for-privacy@interlog.com             Info for manufacturers: http://www.trexon.com
We've slightly trimmed the long signature. Click to see the full one.
Re: PIC18 Timer2 weirdness
On 30 Oct, in article
....
Quoted text here. Click to load it

It stares back and makes silly faces at you :-^

--
Paul Carpenter          | snipped-for-privacy@pcserviceselectronics.co.uk
<http://www.pcserviceselectronics.co.uk/ PC Services
We've slightly trimmed the long signature. Click to see the full one.

Site Timeline