cancel
Showing results for 
Search instead for 
Did you mean: 

USART receive data on STM32F4

nakulrao
Associate II
Posted on January 18, 2013 at 19:01

Hi

I am trying to receive data via USART and then put it on the LCD. I am using the following code.

void print_response(void)

{

uint8_t i, resp[5];

for (i=0;i<5;i++)

{

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

resp[i] = USART_ReceiveData(USART3);

USART_ClearFlag(USART1, USART_FLAG_RXNE);

}

sprintf((char*)buffer, ''0x%x 0x%x 0x%x 0x%x 0x%x'', resp[0], resp[1], resp[2], resp[3], resp[4]);

LCD_DisplayStringLine(LINE(13), (uint8_t*)buffer);

Delay(200);

}

As far as I can say, this code gets stuck in line 6. I am new to this and unable to get it right. Any help will be appreciated.

Thank you.
6 REPLIES 6
Posted on January 18, 2013 at 19:29

So USART1 or USART3? You don't need to clear RXNE, reading the data register DR will automatically clear it.

Assuming it stuck in the while(RXNE) loop you might want to check the initialization of the USART, and pins attached to it, and confirm you're actually sending data to the right place, or at all.

Mark failing line with a comment, or arrows. Or at the very least paste it in with Format Code Block tool and enable line numbering.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
nakulrao
Associate II
Posted on January 18, 2013 at 19:46

Thanks for the reply. Sorry about my formatting (or the lack of it). I am using USART3. Here

is the code i use.

void USART_Init1(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Peripheral Clock Enable -------------------------------------------------*/
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC , ENABLE);
/* Enable USART clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
/* Connect USART pins to AF7 */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);
/* Configure USART Tx and Rx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_Init(GPIOC, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
/* When using Parity the word length must be configured to 9 bits */
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART3, &USART_InitStructure);
/* Enable USART */
USART_Cmd(USART3, ENABLE);
}

nakulrao
Associate II
Posted on January 18, 2013 at 19:52

Also, here is a function I wrote which has to see if it receives 0xFFD8 via USART and then write the received data (which is of fixed size) to the SRAM. Again this function does no perform as required.


void read_data(void)

{

uint8_t i, flag, d1, d2, rx_data;

uint16_t j, wr;

uint32_t addr = Bank1_SRAM2_ADDR;

flag = 0x00;

for (i=0; i< sizeof(READ_DATA); i++)

while(USART_GetFlagStatus(USART3,USART_FLAG_TXE) == RESET);

USART_SendData(USART3, READ_DATA[i]);


while(flag != 0xFF)

{

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

rx_data = USART_ReceiveData(USART3);

if (rx_data == 0xFF)

{

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

rx_data = USART_ReceiveData(USART3);

if (rx_data == 0xD8)

flag = 0xFF;

}

}

i=0;

for(j=0; j<(size/2); j++)

{

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

d1 = USART_ReceiveData(USART3);

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

d2 = USART_ReceiveData(USART3);

wr = d1;

wr <<= 8;

wr|=d2;

SRAM_WriteBuffer(&wr, addr, 0x01);

addr++;

}

}

Posted on January 18, 2013 at 20:09

Configuration doesn't seem problematic, where is this data coming from? The USART will not register data it is not seeing, or that is not framed correctly. Check the input.

The following code needs a compound statement like this

for (i=0; i< sizeof(READ_DATA); i++)
{
while(USART_GetFlagStatus(USART3,USART_FLAG_TXE) == RESET);
USART_SendData(USART3, READ_DATA[i]);
}

Otherwise you just do the while loop sizeof(READ_DATA) times, and the USART_SendData() once. I wouldn't indent the line after the while, as the while has a semi colon after it, and is free standing. It does not repetitively call USART_SendData().
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
nakulrao
Associate II
Posted on January 18, 2013 at 20:13

It was really stupid of me not to put the proper braces for the 'for loop'. Thanks a lot.

nakulrao
Associate II
Posted on January 18, 2013 at 20:21

The data is coming from a serial camera. And it goes into an infinite loop when it has to receive data. Its 1am in the morning and I don't have access to an oscilloscope now.