cancel
Showing results for 
Search instead for 
Did you mean: 

UART communication problem (incorrect received data and synch.)

danesh
Associate II
Posted on August 04, 2015 at 23:12

Hello all,

 

I am using a STM32F103 MCU and a FT232RQ USB controller which is connected to UART4 in my configuration. I use polling to read data from UART4 (later I will use interrupt as soon as I am sure that everything work fine). I read data from UART4 in the main ''while'' loop as below:

 

int main() {

  initF232RQ();

 

  while (1) {

    while (USART_GetFlagStatus(UART4, USART_FLAG_RXNE) == RESET);

    uint8_t ch = (uint8_t)USART_ReceiveData(UART4);

  }

}

 

and ''UART4'' is initialized in the function ''initF232RQ'' as below:

 

void initF232RQ() {

  USART_InitTypeDef USART_InitStruct;

 

  USART_InitStruct.USART_BaudRate            = 115200;

  USART_InitStruct.USART_WordLength          = USART_WordLength_8b;

  USART_InitStruct.USART_StopBits            = USART_StopBits_1;

  USART_InitStruct.USART_Parity              = USART_Parity_No;

  USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

  USART_InitStruct.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;

 

  USART_Init(UART4, &USART_InitStruct);

  USART_Cmd(UART4, ENABLE);

}

 

The code above seems to work fine when I use echo which means, send the received character in PuTTY, but the only problem in echo test, is that all upper case characters are sent as lower case characters. The other problem is that when I check the received value of the sent char, the value is totally different from the ASCII code of the sent character. For instance, when I send ''a'', the value ''3'' will be sent, or if I send ''b'' the value ''4'' will be sent to the MCU. and if I send ''w'', value ''223'' will be sent to the MCU.

 

I also tried to send a block of data from a Windows host machine, using the code:

 

::WriteFile(GetFileHandle(), pData, pWriteByteCount, &lNumberOfBytesWritten, lPtrAsyncStruct)

 

 

where:

 

''pData'' is an array of data, ''pWriteByteCount'' is the size of array in bytes, ''lNumberOfBytesWritten'' is the number of bytes written to the serial port, and ''lPtrAsynchStruct'' is a data structure used in the write function. In this case, when I send a block of data, using the code above at the MCU side, only one byte is always received and the rest of data seems to be lost. I tried to follow the examples for USART, but it seems that I have missed something. Can anybody help?

 

Regards,

 

Dan.

#know-your-tools
10 REPLIES 10
danesh
Associate II
Posted on August 11, 2015 at 12:35

Thanks a lot clive1! The setting with the external clock was the problem and now the USB module is working correctly.