So it's possible to set the initial starting point of the timer inside ISR ?
yes, definitely.
I do that all the time to change how often the overflow ISR occurs.
When it overflows and calls the interrupt you can give it a new value in the ISR and it will instantly start counting from there.
=255 and it will overflow on the next clock or =0 and it will have to count all the way.
You can then put a variable in the ISR that is incremented up to a set point to flag X counts has occurred. This flag can be used to do stuff in your main program loop.
eg
ISR
{
if (Sdelay!=0) { Sdelay++; if (Sdelay>3) {Sdelay=0;} }
if (Ldelay!=0) { Ldelay++; if (Ldelay>200) {Ldelay=0;} }
TMR=255-50;
};
main {
while(1) {
if (Sdelay==0) {
// do some stuff
Sdelay=1;
}
}
}
The advantage of this is you can have many flags all set with different timing from the one timer ISR
So there are 3 ways to adjust your timing
- The clock source/prescaler for the hardware timer your using. ( clk/256 etc.)
- The timer's count register, high vales overflow quickly, low values will have to count up.
- The set point of how many counts it takes before setting a flag
NOTE: Most micros have PWM modes that do special things with output compare interrupts, if you put the timer into a special PWM mode it's behavior changes. So if you're using a hardware PWM mode the timer may not behave like a normal timer.