Pages: [1]   Go Down
Author Topic: Attach to Timer0 Interrupt  (Read 1149 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear Arduino Enthusiasts,

I'd like to suggest a small improvement to the Arduino API.
Some sketches do need a periodic interrupt. Most of them "sacrifice" timer1 or timer2 for this purpose. However, Arduino 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
Logged

Pages: [1]   Go Up
Jump to: