Hi,
While writing this, I have a feeling that the PIC programmer folk are snickering behind by back, but...
I wanted to generate a 104us periodic interrupt on an 8-bit PIC (e.g. PIC12F675) using Timer0. The clock is the internal oscillator, effectively 1MHz (1us per instruction). Now, Timer0 can only generate an interrupt when its 8-bit counter, TMR0, overflows from 0xFF to 0x00 - meaning once per 256us, which is too slow for my requirements.
However, as the datasheet heartily suggests, we can write new values directly to TMR0. If we set TMR0 at the beginning of the ISR function to 152, it will only need 104 more cycles* to reach 0x00 and I'll have our desired 104us period.
* Yes, I know about the two-cycle count inhibition... let's ignore that for nowBut here's the trap: try the above and you won't get 104us. It'll be closer to 125us. My understanding is that it's because there's a time gap between the interrupt event and the setting of TMR0: The MCU needs time to enter the ISR and run the instructions until the new value is in place. Makes sense, right? What's really weird for me is that I found no reference to this anywhere. Every online/offline "timer calculator" I tried, even Microchip's own "MPLAB Code Configurator" that generates code for you, makes the mistake of ignoring this time gap (I tested it on a PIC12F1840). Is everyone's timing off by ~20 clock cycles? Can anyone provide some feedback on this finding?
Thanks!