small timer problem...

Status
Not open for further replies.

SnM

New Member
Here is part of my code... I am using a 12F629, and I am trying to get this nailed down in MPLAB. I have the TMR0 interupt enabled, with no prescaler.
org 4
bcf 0x0B,2
movlw 0xA5
movwf 01h
retfie

An interrupt happens every
.1 mS

Using the stopwatch, with a breakpoint on the "bcf 0x0B,2" line, I am able too see that it stops every .1 mS. This works fine until it is suppose to go to 1.4 mS, instead it goes to 1.399 mS. This doesn't happen again until after 21 mS. My stopwatch is reading 21.498 mS. right now.

Any tips on solving this little timing error?

Thanks,
Steve
 
Assuming you are running on a 4 Mhz oscillator, with its internal /4, you're getting an effective 1 MHz clock, so 1 uS per instruction.

Counting the instuction cycles used by your code:
2 uS: the interrupt vector
1 uS: bcf
1 uS: movl
1 uS: movwf
= 5 uS.

you load 0xA5 (decimal 165) into TMR0, making an interrupt occur every (256-165) = 91 uS. Your interrupt takes 5 uS to get to the TMR0 loading, and allow 2uS for the TMR0 loading to take effect, so you actually get one every 98 uS, not the 100 uS you are aiming for. It's constantly firing a smidgeon faster than you are wanting until finally the fractions become noticeable that it's too fast.
 
Or change the value to 163 (0xA3) so that it waits 93 uS plus the 2 and 5 = 0.1 ms per interrupt. Any changes (lines added, removed, gotos, etc...) made to the ISR will need to be reflected in the value that the timer counter is set to.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…