Wiring Forum
http://wiring.org.co/cgi-bin/yabb/YaBB.pl
Suggestions >> Software Suggestions >> Attach to Timer0 Interrupt
http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1293546862

Message started by Andreas on 12/28/10 at 14:34:22

Title: Attach to Timer0 Interrupt
Post by Andreas on 12/28/10 at 14:34:22
Dear Wiring Enthusiasts,

I'd like to suggest a small improvement to the Wiring API.
Some sketches do need a periodic interrupt. Most of them "sacrifice" timer1 or timer2 for this purpose. However, Wiring already has an interrupt happening about every millisecond. Why not add a "hook" to it, so that it gets accessible by sketch functions!?

I'd suggest to modify wiring.c in the following way:


volatile static voidFuncPtr timer0_intFunc = 0;

void attachTimer0Interrupt(void (*userFunc)(void)) {
    timer0_intFunc = userFunc;
}


SIGNAL(TIMER0_OVF_vect)
{
     // copy these to local variables so they can be stored in registers
     // (volatile variables must be read from memory on every access)
     unsigned long m = timer0_millis;
     unsigned char f = timer0_fract;

     m += MILLIS_INC;
     f += FRACT_INC;
     if (f >= FRACT_MAX) {
           f -= FRACT_MAX;
           m += 1;
     }

     timer0_fract = f;
     timer0_millis = m;
     timer0_overflow_count++;

     if(timer0_intFunc)
           timer0_intFunc();

}


What do you think?

Cheers,

Andreas

Title: Re: Attach to Timer0 Interrupt
Post by barragan on 12/28/10 at 17:16:44
hi Andreas, I am concerned regarding the accuracy for commands like millis(), delay(), etc, what do you think?

Title: Re: Attach to Timer0 Interrupt
Post by Andreas on 01/10/11 at 17:05:16
Hi barragan,

the accuracy of millis() etc. should not be influenced, as the frequency of the interrupt is still determined by the HW timer itself (i.e. counting crystal clock cycles).
Only the duration of the ISR itself might be extended by the extra callback.

Happy New Year

Andreas

Title: Re: Attach to Timer0 Interrupt
Post by barragan on 01/12/11 at 00:55:25
hi Andres, precisely, you can't delay().

Wiring Forum » Powered by YaBB 2.5 AE!
YaBB Forum Software © 2000-2010. All Rights Reserved.