6 years, 9 months ago.

uart serial interupts example trouble

Here is my serial example. After I send uart "h" character, the uart stop communication and can't print anymore.

include the mbed library with this snippet

#include "mbed.h"

void Rx1_interrupt();

Serial pc(USBTX, USBRX); //tx, rx

//volatile bool serialArrived = false;
//volatile      char a = 0;
int serialArrived = 0,n=0;
char a =0;


DigitalOut myled(LED1);             // This LED is on NUCLEO-L152RE
DigitalIn BlueButton(USER_BUTTON);  // This is Blue-Button and is on NUCLEO-L153RE

#define Pressed 0
#define NotPressed 1



int main()
{
    myled=0;
    pc.printf("===================\n\r ");
    pc.printf("START MAIN \n\r");
    pc.attach(&Rx1_interrupt, Serial::RxIrq);

    while(1) {

        // Test the Blue Button
        if (BlueButton == Pressed) {
            while(BlueButton == Pressed) {
                if (n == 0)
                    pc.printf("Please release the BLUE Button\n\r");
                n++;
            }
            n = 0;
            pc.printf("button release ok\n\r");
        }
        if(serialArrived) {

            pc.printf("%c",a);
            serialArrived = 0;
            if(a=='1') {
                myled =1;
                wait(1);
                myled =0;
            } else if(a=='h') {
                pc.printf("command list\n\r");
            }
        }
    }
}



1 Answer

6 years, 9 months ago.

You have a function prototype for your interrupt handler: void Rx1_interrupt(); But there is no implementation.

//This is the missing part, the implementation of the interrupt handler:
void Rx1_interrupt()
{
    //Do your work here. Read from serial, set flag, etc.
    serialArrived = true; //Declare volatile!
    a = pc.getc(); //Declare volatile!
}