WARNING: Untested code ahead! What we really need is a way to ensure that Timer 1 interrupts every 5000 instructions or 1 millisecond. The problem is that there can be jitter on reaching the interrupt handler for the timer. This jitter is caused by 2-cycle instructions, other interrupt handlers, and interrupts being disabled at 'task' level. We can get around the jitter problem by always adding a constant to the timer, rather than trying to set it directly. What we are doing is effectively advancing time forward the rest of the 65536-5000 instructions to get the timer to interrupt again at the right point. Here is my suggestion (this code would be in the interrupt handler) fudge = ...number to account for instructions timer is turned off... magic_value = D'65536'-D'5000'+fudge bcf T1CON,TMR1ON ; turn off timer movlw low(magic_value) addwf TMR1L,F btfsc STATUS,C incf TMR1H,F movlw high(magic_value) addwf TMR1H,F bsf T1CON,TMR1ON ; turn it back on Also, sorry this is in ASM, not C, but the same thing should work in C. Bob Ammerman RAm Systems (contract development of high performance, high function, low-level software) ----- Original Message ----- From: Brent Brown To: Sent: Thursday, September 28, 2000 6:32 AM Subject: Re: [PIC]: interrupt accuracy > Hi Bryce, > > Welcome to the PIC list! > > Like Jinx said, I think you just need to reload the counter directly. > Do it as soon as possible after the interrupt to minimize timing > errors. TMR1 increments at 1/4 Fosc, with 20MHz XTAL that's > 5MHz so you should be able to get 200ns resolution. Don't know > how much to allow for interrupt latency. Try some code like this:- > > interrupt void int_routine(void){ > if(TMR1IF){ > TMR1H = 0xEC; > TMR1L = 0x77; > TMR1IF = 0; > count++; > } > } > > Brent Brown > Electronic Design Solutions > 16 English Street > Hamilton, New Zealand > Ph/fax: +64 7 849 0069 > Mobile: 025 334 069 > eMail: brent.brown@clear.net.nz > > -- > http://www.piclist.com hint: To leave the PICList > mailto:piclist-unsubscribe-request@mitvma.mit.edu > > -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu