Hello,
I want to use the USART RXRDY interrupt to receive data on Serial2 (Pin17) with an arduino due.
I tried this simple program that just toggle a LED when a byte is received on Serial2 = USART1-RX = Arduino Due Pin 17 (RX2).
Does someone can explain me why this simple program doesn't work ?
// Toggle Built-in LED when a byte is received on Serial2 = USART1-RX = Arduino Due Pin 17 (RX2)
volatile char data ;
volatile char led_state = 0 ;
void USART1_Handler (void) __attribute__ ((weak));
void USART1_Handler(void)
{
data = USART1->US_RHR;
digitalWrite(LED_BUILTIN, (led_state++)%2);
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial2.begin(115200);
// Enable interrupts on end of receive
USART1->US_IER = US_IER_RXRDY;
NVIC_EnableIRQ(USART1_IRQn);
}
void loop()
{
}
Thanks,
Stephane
Both Serial1, Serial2, Serial3 are using interrupts. Therefore you can't write your own USART1_Handler() code as you did. Before writing your own USART1_Handler(), you would have to alter variant.h.
See this thread, reply #4:
Anyway, you don't have to alter anything to read something from USART1 (Serial2). See this example sketch to read bytes from Serial1:
/*********************************************************************/
/* Jumper between RX1 and TX2, and another one between TX1 and RX2 */
/*********************************************************************/
char c = 0;
void setup() {
Serial.begin(250000);
Serial1.begin(5000000);
Serial2.begin(5000000);
Serial2.print("Hello");
}
void loop() {
String s;
s.reserve(10);
s = "";
while (Serial1.available() > 0) {
c = Serial1.read();
s += c;
}
if (s.length() > 0) {
Serial.println(s);
Serial2.print(s);
}
delay(1000);
}
Thanks for your answer.
I understand my mistake.
Considering the topic you told me about DMA and USART, I think I will use the DMA controller to read USART1 instead of interrupts.