Trouble getting started with interrupts

PickyBiker

Well-known member
I found this page for using interrupts on teensy 4.0. https://www.pjrc.com/teensy/interrupts.html

Code:
#include <avr/io.h>
#include <avr/interrupt.h>

void setup() {
}

void loop() {
}

ISR(TIMER0_OVF_vect)
{
    /* Timer 0 overflow */
}


The code example shown there does not compile in the Arduino IDE, it give an error
Arduino: 1.8.19 (Windows 10), TD: 1.56, Board: "Teensy 4.0, Serial, 600 MHz, Faster, US English"

TeensyInterruopts:12: error: expected constructor, destructor, or type conversion before '(' token

ISR(TIMER0_OVF_vect)

^
TeensyInterruopts:12: error: expected constructor, destructor, or type conversion before '(' token

ISR(TIMER0_OVF_vect)
^
expected constructor, destructor, or type conversion before '(' token
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

According to the Interrupt Vector, Mask & Flag Names chart, the name TIMER0_OVF_vect is correct.

What am I missing?
 
Looks like you want a timer interrupt? If so, use the IntervalTimer which is the native timer for all ARM Teensies.

Code:
IntervalTimer t1;

void onTimer()
{
    digitalToggleFast(LED_BUILTIN);
}


void setup()
{
    pinMode(LED_BUILTIN, OUTPUT);
    t1.begin(onTimer, 125'000);  // invoke onTimer every 125 ms
}

void loop()
{
}
 
I actually want multiple pin change interrupts. The code I posted was just the example from the webpage on how to use interrupts on teensy. I figured if I could get that to work, I could probably get the rest to work. That said, I appreciate the example from luni on using the timer because that will likely be needed as well.

Based on kd5rxt-mark's response, it seems the Teensy 4.0 uses the same interrupts commands as Arduino Uno i.e. attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);

So I tried this and it seems to work!

Thank you!

Code:
volatile long n;

void setup()
{
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), ISR2, CHANGE);
}

void loop() 
{
  Serial.println(n);
  delay(10);
}

void ISR2()
{
  n++;
}
 
I found this page for using interrupts on teensy 4.0. https://www.pjrc.com/teensy/interrupts.html

That page dates from the days when all Teensy's were Atmel based, ie Teensy 1 and 2. Its specific to that hardware, so not relevant to T3's or T4's (which didn't exist when the page was written I believe).

The attachInterrupt() call is the portable Arduino way to do interrupts for digital input pins and should work on any Arduino
compatible for the relevant supported pins.
 
Back
Top