How to trigger interrupt on falling/rising edge of sent PWM signal?

john celo

New member
Teensy 4.1 generates 240kHz clock/pwm signal that it sends to a target device.

Code:
analogWriteFrequency(A9, 240000); //240kHz
analogWrite(A9,128); // duty cycle 50%

And I need to generate interrupt on either rising/falling edge of the sent signal to capture bitsequence that is sent back from target device.
For some reason I struggle to find a simple example of how to do this, preferably without any thick, hard to understand abstraction layers.

(ideally this would be done via DMA, each sent clock pulse would read multiple pins, save them to memory and then increase memory address, but even this simpler case eludes me)
 
You might want to look at the Arduino documentation for attachInterrupt:

Code:
const byte ledPin = 13;
const byte interruptPin = A9;
volatile byte state = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}

void loop() {
  digitalWrite(ledPin, state);
}

void blink() {
  state = !state;
}
sightly edited their example for your pin number
You could use RISING or FALLING depending on which one...
 
You might want to look at the Arduino documentation for attachInterrupt:

Code:
const byte ledPin = 13;
const byte interruptPin = A9;
volatile byte state = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}

void loop() {
  digitalWrite(ledPin, state);
}

void blink() {
  state = !state;
}
sightly edited their example for your pin number
You could use RISING or FALLING depending on which one...
Attaching interrupt to PWM output pin (via arduino HAL) doesn't work, it simply stops sending PWM signal once you do that (as far as I can tell)
I suspect that an interrupt has to be attached to the timer that generates PWM signal, but I dont know how to do that.
 
Simplest would be to connect the PWM pin to another pin and attach the interrupt to this.
 
Back
Top