Question
USART problem
Posted on April 11, 2015 at 23:38
Hi, I'm working on Xbee S2 module and I want to make it ' talk to itself' (just for testing).
My data is sent out but I am not able to receive it back. My code: void USART_Config(void) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //Connect USART to pin A10 and A9 GPIO_PinAFConfig(USART1_TX_GPIO, USART1_TX_PS, GPIO_AF_1); GPIO_PinAFConfig(USART1_RX_GPIO, USART1_RX_PS, GPIO_AF_1); /* Configure USART1 pins: Rx and Tx ----------------------------*/ GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_Pin = USART1_TX_PIN ; // Pin 9 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_3; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(USART1_TX_GPIO, &GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = USART1_RX_PIN ; // Pin 10 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_3; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(USART1_RX_GPIO, &GPIO_InitStruct); USART_InitTypeDef USART_InitStruct; USART_InitStruct.USART_BaudRate = 9600; 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(USART1, &USART_InitStruct); USART_Cmd(USART1,ENABLE); // NVIC_InitTypeDef NVIC_InitStruct; // NVIC_InitStruct.NVIC_IRQChannel= USART1_IRQn; //NVIC_InitStruct.NVIC_IRQChannelPriority = 0; // NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; // NVIC_Init(&NVIC_InitStruct); // USART_ITConfig( USART1,USART_IT_TXE, ENABLE); // USART_ITConfig( USART1,USART_IT_RXNE, ENABLE); // Enable USART Interrupts /* Enable USART1 global interrupt */ // NVIC_EnableIRQ(USART1_IRQn); } void LED_Init() { GPIO_InitTypeDef GPIO_InitStruct; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC,ENABLE); GPIO_InitStruct.GPIO_Pin =LED1_Pin | LED3_Pin |LED2_Pin | LED4_Pin; GPIO_InitStruct.GPIO_Mode =GPIO_Mode_OUT; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_2; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOC , &GPIO_InitStruct); } //void USART1_IRQHandler(void){} int main(void) { USART_Config(); LED_Init(); Pushbutton(); unsigned char temp1; uint32_t PB_Input = 0; while(1) { PB_Input = GPIO_ReadInputDataBit(PB_GPIO, PB_Pin); GPIO_SetBits(LED_GPIO, LED2_Pin); if(PB_Input) { GPIO_SetBits(LED_GPIO, LED3_Pin); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); USART_SendData(USART1,'X'); GPIO_SetBits(LED_GPIO, LED4_Pin); *** while(USART_GetFlagStatus (USART1, USART_Flag_RXNE)== RESET); temp1= USART_ReceiveData(USART1); if (temp1== 'X') GPIO_SetBits(LED_GPIO, LED1_Pin); else GPIO_SetBits(LED_GPIO, LED1_Pin); } else { GPIO_ResetBits(LED_GPIO, LED3_Pin); } } } My code stucked at ***. I don't really understand the difference between GetFlagStatus and GetITStatus. Could someone explain to me. Many thanks in advance. #usart