Interrupt service routine for SAMD21G18

I’m using a SAMD21G18 microcontroller with Arduino as the development platform and I want to implement a UART interrupt service routine. Is there a way to achieve this?

Hi there,
Something like this would do.

void setup(){
 Serial.begin(9600);  
  pinMode(7, INPUT);      // Make pin 7 RXD an input
   
    // attach our interrupt pin to it's ISR
    attachInterrupt(0, recISR, CHANGE);
    // we need to call this to enable interrupts
    interrupts();
}

// The interrupt hardware calls this when we hit our left bumper
void recISR(){

  //  rec = 1;
     Serial.println("interrupt!"); 
 
}

Give something like that a try.
HTH
GL :slight_smile: PJ :v:

1 Like

I’ve successfully set up a UART-based interrupt system using the code below. The interrupt is triggered on changes to the RX pin (pin 7), and it correctly prints “interrupt!” to Serial1 when triggered.

void setup() {
  Serial1.begin(115200);
  pinMode(7, INPUT);  // Configure pin 7 as an input

  // Attach interrupt to pin 7 with ISR for CHANGE event
  attachInterrupt(digitalPinToInterrupt(7), recISR, CHANGE);
  // Enable interrupts
  interrupts();
}

// ISR for handling interrupts
void recISR() {
  Serial1.println("interrupt!");
}

I made a small change to the code by adding digitalPinToInterrupt(7) to the attachInterrupt function, which resolved the issue. However, I am still encountering difficulties reading the received UART data from the RX register and transmitting it via the TX line.

Could anyone provide guidance on how to read the incoming data from the RX register and send it through the TX line within the ISR, or suggest a more effective approach for handling UART communication with interrupts?

Hi there,
You almost have it…

HTH
GL :slight_smile: PJ :v:

Don’t forget to mark this as the solution so others may find it. :+1: