Go Down

Topic: attachInterrupt(...) improvement (Read 741 times) previous topic - next topic

ytorroja

Hi,

I have observed that attachInterrupt() does not clear the interrupt flag before doing the "attachment". I think it would be a good idea to do it. Otherwise, pending interrupts could be executed immediatly after calling attachInterrupt(). Although we could discuss a lot about it, I would say 95% of cases this would not be the desired behavior, specially taking into account the average Arduino user. 

This can be easily done in the file WInterrupts.c, clearing the interrupt flag before the mask activation, i.e.

void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
  ...
  ...
    switch (interruptNum) {
#if defined(EICRA) && defined(EICRB) && defined(EIMSK)
    case 2:
      EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
      EIFR |= (1 << INT0);  <<<<<<<<<<<<<<<<<<<<<<<<<<  pending interrupt clearing
      EIMSK |= (1 << INT0);
      break;
  ... and so on
  ...

Thanks :-)

Yago

Coding Badly


Go Up