First off, You can't expect ON INTERRUPT to perform accurately in the range of 20khz. It's more of a, "Whenever I get around to it" interrupt system. If you want an accurate frequency in this range, you need to use ASM.
Second, you need to ADD the constant to the existing timer value, instead of just loading the number on each interrupt. Otherwise you lose however many "Ticks" the ON INTERRUPT waited before finally servicing it.
Third, you have to account for the time it takes to ADD the constant to the timer value. And for that, you need to know how long that takes. Hard to know with Basic Interrupts.
Forth, when everything is working correctly, a Const of 65430 will give an interrupt frequency of 22.8 khz instead of 19.8.
and Fifth, You're loading a value into the TIMER registers, not the prescaler.
Ok, let's go for sixth. Setting the T1SYNC (T1CON.2) bit has no effect when using the system clock (FOSC/4) for the timer input. Doesn't hurt, but doesn't do anything constructive either.
And, yes I've been having a bad day. Sorry!

<br>
Bookmarks