TomP wrote:That sounds like an eminently reasonable design, but simply suggesting that people "use interrupts" doesn't make it clear how they need to adjust their sketches to use them effectively. Maybe you could publish some tutorials on the Arduino playground site to show people how to use interrupts this way?
all three of the timers on the arduino's mega168 are used for pwm generation, and the timer0 overflow interrupt is used to count milliseconds, but you can enable the overflow interrupt on one of the other timers and add write your own interrupt handler.
to use the timer2 overflow interrupt you need to add the following to your init() routine...
- Code: Select all
TIMSK = (1 << TOIE2); // enable timer 2 overflow interrupt
and then you'll have to write an interrupt handler. variables used within the interrupt handler should be declared with the attribute volatile so that the compiler does not optimize away references to it.
- Code: Select all
volatile int variable_I_use_in_my_interrupt_handler;
ISR (TIMER2_OVF_vect) {
//
// your code goes here
//
}
timer2 is an 8 bit timer setup with a prescaler of 64 and will therefore overflow 16000000 / 256 / 64 times a second. that's 976.5625 for those of you without a calculator handy.


