Go Down

Topic: Pure AVR equivalent of attachInterrupt() ? (Read 2244 times) previous topic - next topic

cmagagna

I'd like to add a "system halt" button to my mill running GRBL. The button is on D2, hardware interrupt 0. In Arduino-speak I can set it up with attachInterrupt(0...) but for GRBL I need a pure AVR C implementation.

Before I start trying to pull apart the source code in WInterrupts.c etc. I was wondering if anyone's already done this or has a reference to an example.

Thanks in advance.

Coding Badly


Processor?

Trigger (CHANGE, RISING, FALLING, LEVEL)?

cmagagna

I apologize. I'm using a Uno with a 328P and would like the trigger to be on RISING.

Thanks!

Coding Badly


For INT0 on the 328 processor, we end up at the interrupt service routine on line #276...
https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/WInterrupts.c#L276

We'll use the modern macro instead of the deprecated SIGNAL macro...

Code: [Select]
ISR( INT0_vect )
{
  // Your stuff goes here
}


The EICRB register does not exist does we end up with this initialization code on line #95...
https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/WInterrupts.c#L95

Code: [Select]
      EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
      EIMSK |= (1 << INT0);


RISING is defined as 3...
https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/Arduino.h#L40
...so the initialization code becomes this...

Code: [Select]
      EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (3 << ISC00);
      EIMSK |= (1 << INT0);


And, we're done.

cmagagna

That's great, thanks! I won't be able to test it out until tomorrow evening but I'll let you know how it goes.

Thanks again.

cmagagna

It compiled fine but didn't function until I added

Code: [Select]
#include <avr/interrupt.h>

Once I did that it worked as designed.

Thanks again for your help.


Coding Badly


Excellent.  I'm glad you have it working.

Go Up