2013-08-06 10:12 PM
Im using usart2 and i know its sending data but will not receive im using :
rx_data = USART_ReceiveData(USART2);to save the data then using a while(rx_data > 0 ) { do stuff}any suggestions about why its not receiving? #poorly-framed-question #usart #usart-stm32f1032013-08-07 06:46 AM
Are you first checking the RXNE flag to see if data is available before trying to read it from the USART?
Jack Peacock2013-08-07 09:07 AM
yes i use
while( USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET); // checking flag
{rx_data = USART_ReceiveData(USART2);
}
then once rx_data >0 it does stuff, but i dont understand why it doesnt react. Im feeding the USART2.TX right into the USART2.RX. Am i doing something wrong?
2013-08-07 09:56 AM
Am i doing something wrong?
Evidently, perhaps some more details about how you've initialized things, and how they are connected. You probably wantwhile( USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
if you want it to spin while NOT asserted2013-08-07 10:00 AM
did you set USART mode:
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; and both USART GPIO pins as AF ?2013-08-07 11:40 AM
GPIO_InitTypeDef GPIO_InitStructure; // GPIO structure
USART_InitTypeDef USART_InitStructure; // USART Structure
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* Configure USART Tx as alternate function push/pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA9-UART.TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// USART Configure
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_InitStructure.USART_Parity = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_Parity_No;
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
Here is how i initialized everything. After further testing it works once and stops.
while( USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
{ do stuff}
Any ideas why it would stop? the do stuff mentioned here works already im just trying to have it work only when USART is receiving data.
2013-08-07 12:42 PM
Don't enable the RXNE interrupt.
If you're using an interrupt, you a) need to initialize NVIC also, and b) have a service routine to handle it.