Cannot get SAMD21 Interrupt to function

I've been working on this for a couple of days. However I'm at a loss as to why my interrupt is not working.

Issue is the variable (PWMcount) that should be incremented in the ISR is not changing.
D2 and D3 are connected to an encoder (manual, contact type). My Logic analyzer shows the encoder working.
Can anyone post a functioning external pin interrupt sketch that is working?

Thanks
John

/*
  This example shows how to wake up the SAMD21 from sleep with an external interrupt... NOT!
  This sketch will break the USB during normal operation.
  Double click the button contacts to enable bootloader mode for a new sketch.

  The circuit:
  - SEEED XIAO SAMD21.

*/

#include "Arduino.h"

#define ENCODERDATA    D2   // aka PA10
#define ENCODERCLK     D3   // aka PA11 and EXTINT[11]

uint16_t PWMcount = 100;

uint32_t startingMillis;
uint32_t now;

void setup()
{
  Serial.begin(57600);
  pinMode( ENCODERCLK, INPUT_PULLUP );
  pinMode( ENCODERDATA, INPUT_PULLUP );
  attachInterrupt( digitalPinToInterrupt( EXTERNAL_INT_11) , EIC_11_Handler, FALLING );
}

void loop()
{
  // Simply prints out PWMcount every 5 seconds
  now = millis();
  if(startingMillis + 5000 < now){
    Serial.print("PWMcount = ");
    Serial.println(PWMcount);
    startingMillis = millis();
  }
}

void EIC_11_Handler(void) {
    PWMcount = PWMcount + 1;
}

Variables shared between an interrupt routine and other code need to be declared volatile.

1 Like

I found the issue. I was over thinking the PIN definition in the attachInterrupt pin callout.

The below works fine.

Thanks to those who provided help.

John

/*
  This example shows how to an interrupt for the SAMD21 from with an external interrupt

  The circuit:
  - SEEED XIAO SAMD21.

*/

#define BUTTON   D3   // aka PA11 and EXTINT[11]

volatile uint16_t ButtonCount = 10;

uint32_t startingMillis;
uint32_t now;

void setup()
{
  Serial.begin(57600);
  pinMode( BUTTON, INPUT );
  attachInterrupt( digitalPinToInterrupt( D3) , EIC_11_Handler, FALLING );
}

void loop()
{
  // Simply prints out count every 5 seconds
  now = millis();
  if(startingMillis + 5000 < now){
    Serial.print("Count = ");
    Serial.println(ButtonCount);
    startingMillis = millis();
  }
}

void EIC_11_Handler(void) {
    ButtonCount = ButtonCount + 1;
}

1 Like