Home \ Help \ Search \ Login RSS

Welcome, Guest. Please Login

This forum is now READ-ONLY! Please visit http://forum.wiring.co/ for the new forum.

Wiring ForumSuggestionsSoftware Suggestions › Attach to Timer0 Interrupt

Page Index Toggle Pages: 1
Attach to Timer0 Interrupt (Read 3984 times)
12/28/10 at 14:34:22

Andreas   Offline
YaBB Newbies

Posts: 2
*
 
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
 
IP Logged
 
Reply #1 - 12/28/10 at 17:16:44

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
hi Andreas, I am concerned regarding the accuracy for commands like millis(), delay(), etc, what do you think?
 
IP Logged
 
Reply #2 - 01/10/11 at 17:05:16

Andreas   Offline
YaBB Newbies

Posts: 2
*
 
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
 
IP Logged
 
Reply #3 - 01/12/11 at 00:55:25

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
hi Andres, precisely, you can't delay().
 
IP Logged
 
Page Index Toggle Pages: 1