Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 8 months ago.
Serial interrupt doesn't work on NUCLEO_L152RE and NRF51-DK?
Hi,
I tested the serial interrupt example on NUCLEO_L152RE and NRF51-DK.
When I input character to the board, it stopped blinking and nothing echoed.
- include "mbed.h"
DigitalOut led1(LED1);
DigitalOut led2(LED2);
Serial pc(USBTX, USBRX);
void callback_ex() {
Note: you need to actually read from the serial to clear the RX interrupt
printf("%c\n", pc.getc());
led2 = !led2;
}
int main() {
pc.attach(&callback_ex);
while (1) {
led1 = !led1;
wait(0.5);
}
}
1 Answer
7 years, 8 months ago.
Hello Huayuan,
It's OK for debugging but generally it is not recommended to use the printf
function in interrupt service routines because it's quite slow.
Try the following:
#include "mbed.h" DigitalOut led1(LED1); DigitalOut led2(LED1); Serial pc(USBTX, USBRX); void callback_ex() { pc.printf("received: %c\n", pc.getc()); led2 = !led2; } int main() { pc.attach(&callback_ex); while (1) { led1 = !led1; wait(0.5); } }
Notice the pc.
in front of the printf
function in the body of the callback_ex
function. Theoretically, the simple printf
function should work as well since it suppose to be initialized for the Serial2 peripheral which is usually associated with the USBTX and USBRX pins. Unless there is something else in the mbed library for those boards. It worked fine on my NUCLEO-F103RB using Tera Term as serial terminal.
NOTE: If you enclose a code with
markers as below (each marker on its own line!) then it's going to be nicely formatted :)<<code>> and <</code>>
<<code>> #include "mbed.h" DigitalOut led1(LED1); DigitalOut led2(LED1); Serial pc(USBTX, USBRX); void callback_ex() { pc.printf("received: %c\n", pc.getc()); led2 = !led2; } int main() { pc.attach(&callback_ex); while (1) { led1 = !led1; wait(0.5); } } <</code>>