cancel
Showing results for 
Search instead for 
Did you mean: 

Receiving bad character through the USART

mnemxys
Associate II
Posted on July 10, 2015 at 17:36

Hi, I am working in a wired point to point communication system but I have a problem receiving characters.

I have two electronics communicating each other through USART. I want to send a char periodically from the transmitter (TX) to the receiver (RX) but RX does not read the same char sent from TX.

TX is the development kit STM32F4 Discovery. I have no problems sending chars through USART1 in this kit. For my tests I just send the same char periodically. 

RX is my own design where I use the microcontroller STM32F405. I use USART2 to receive the chars from TX.

The configuration of the USART is the same in TX (USART1) and RX (USART2). It is as follows:

void USART1_Config(){

    GPIO_InitTypeDef GPIO_InitStructure;

    USART_InitTypeDef USART_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

    // Configure AF (Alternate Function) of pins PA0, PA1

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);

    USART_InitStructure.USART_BaudRate = 2400;

    USART_InitStructure.USART_WordLength = USART_WordLength_8b;

    USART_InitStructure.USART_StopBits = USART_StopBits_1;

    USART_InitStructure.USART_Parity = USART_Parity_No;

    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

    // Configure USART Tx

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // Configure USART Rx

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

    USART_Init(USART1, &USART_InitStructure);

    USART_Cmd(USART1, ENABLE);

    // Config IT

    NVIC_InitTypeDef NVIC_InitStructure;

    // Enable the USARTx Interrupt RX

    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

    // Config RX IT USARTx

    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

}

As I want to check all the characters received in RX I have my interrupt routine as it is indicated next:

void USART2_IRQHandler(void){

    if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)

      {

        /* Read one byte from the receive data register */

        char rxchar = USART_ReceiveData(USART2);

        ...

      }

}

The problem is that the variable rxchar does not contain the same character sent from TX. I have checked the signal from RX and TX with an oscilloscope and they are the same (as it was expected since the system is wired point-to-point). At this point I suppose that the problem is related to the USART configuration but both have the same. I am completely getting stuck on this and I don't know what can I do.

Can anybody help me with this? Thank you very much in advance for your help.

Greetings!

mnem
4 REPLIES 4
Posted on July 10, 2015 at 18:33

PA9 is not viable for USART1_TX on the STM32F4-DISCO, C49 represents a huge problem.

Got the grounds connected between the boards?

Show enough code to actually be able to demonstrate the problem.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mnemxys
Associate II
Posted on July 13, 2015 at 10:12

I'm sorry, I forgot to say that in STM32F4-DISCO I use PB6 as USART1_TX and PA10 as USART1_RX.

Regarding to the grounds, yes, they are connected between the boards.

And the code is simply like that, in TX I just configure the USART1 and then in the main loop I send all the characters.

int main(void){

    int i=0;

    USART1_Config();

    for(i=0; i< 2500; i++); //delay

    while (1){

        USART_SendData(USART1, 68); // char D

        while (!USART_GetFlagStatus(USART1, USART_FLAG_TXE));

    }/* Infinite loop */

}

 Then, in the main() on RX I configure the USART2 and in the interrupt routine in my previous post, I just read the character so with the debug tool I can check the value of the variable containing the expected character but it never agrees on the one sent from RX. 

mnem

mnemxys
Associate II
Posted on July 13, 2015 at 16:27

Ok, I made some progress but still does not work... I made a test with another USART. I was using that USART to communicate with the PC through a bluetooth module. This USART receives commands from the PC and it was working perfectly. I have changed de code to see if that USART receives correctly the character from TX and I found that it works the same way, the cahracter is wrong. The only change I made was that I have changed the baudrate from 19200 to 2400 so I think that the USART is not working well at 2400 bauds in rx.

Maybe the values used by the libraries I use to generate the baud rate are not good. Can anybody show me a piece of code to program the USART at 2400 bauds changing the registers? I suppose I need to specify the good values for the baud rate.

Thank you very much for your help in advanced.

mnem

Posted on July 13, 2015 at 16:45

Are you using HSI or HSE base clocks?

The 2400 should have a pretty wide tolerance for error, would suggest you review bit timing with scope to ensure it's not wildly off.

There is code in stm32f4xx_usart.c to set the rate. The method is explained/documented in the reference manual.

 

The basic math is USARTx->BRR = APBxClock / Baud

As I recall USART1 and 6 are on APB2 (the fast bus), the rest on APB1

ie USART1->BRR = 84000000 / 2400;

You could check the current setting, and print computed baud rate as a float, the compute % error in setting.

Other than that you're going to need to look a fundamental problems with you hardware, because the USART should be pretty solid.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..