7 years, 11 months ago.

Uart Reception not working in My Nucleo STM32F302R8 board?

Please check my code

  1. include "mbed.h"

void Rx1_interrupt();

Serial pc(PB_3, PB_4); tx, rx

char a=0;

int main() {

pc.printf("serial pwm set ");

pc.attach(&Rx1_interrupt, Serial::RxIrq);

while(1) {

pc.printf("%c",a); } }

void Rx1_interrupt() {

a = pc.getc(); return; }

1 Answer

7 years, 11 months ago.

Hello,
To format a text as code please enclose it between 'code' tags as below ( for more details see also the link to Editing tips at the bottom of your edit box).

<<code>>
int max(int x, int y) {
    return (x > y) ? x : y;
}
<</code>>


#include "mbed.h"

void Rx1_interrupt();

Serial pc(PB_3, PB_4);  //tx, rx

char a=0;

int main()
{
    pc.printf("serial pwm set ");
    pc.attach(&Rx1_interrupt, Serial::RxIrq);

    while(1) {
        pc.printf("%c",a);
    }
}

void Rx1_interrupt()
{
    a = pc.getc();
    return;
}


I would suggest to improve the while loop, since it's continuosly sending the content of variable 'a' over the serial connection (even when no new serial data available) which most likely results in overloading the connected serial terminal. One solution would be to introduce a flag indicating arrival of new serial data. Although the code below isn't perfect I thing it should work.

#include "mbed.h"

void Rx1_interrupt();

Serial pc(PB_3, PB_4); //tx, rx

volatile bool serialArrived = false;
volatile      char a = 0;

int main()
{
    pc.printf("serial pwm set ");
    pc.attach(&Rx1_interrupt, Serial::RxIrq);

    while(1) {
        if(serialArrived) {
            pc.printf("%c",a);
            serialArrived = false;
        }
    }
}

void Rx1_interrupt()
{
    a = pc.getc();
    serialArrived = true;
}

Thanks

posted by Eldho George 22 Dec 2016