2015-04-11 02:38 PM
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. #usart2015-04-11 05:15 PM
Any particular STM32 or board?
For example the STM32F4-DISCO can't use PA9/PA102015-04-12 02:55 AM
Hi Clive,I am using STM32F072RB discovery board.
I tried both USART1 and USART3 but there are no response. Many thanks.2015-04-12 05:31 AM
So is it supposed to echo in the fashion you're testing?
What if you remove the Xbee and just short the TX and RX pins, do you see reception then? What if you connect it to a terminal, via a suitable adapter/voltage converter? Also, it really helps if you provide the #defines you're using, because guessing is not a good debugging technique.2015-04-12 08:19 AM
Yes, Clive. I am able to get receive the data by shorting the RX and TX pins.
This is my full code, sorry for insufficient post last time. Full code: #include ''stm32f0xx.h'' #include ''stm32f072b_discovery.h'' #include ''stm32f0xx_usart.h'' #include ''delay.h'' #include <stdio.h> #include <math.h> #include <string.h> #define USART3_TX_PIN GPIO_Pin_10 #define USART3_TX_GPIO GPIOC #define USART3_RX_PIN GPIO_Pin_11 #define USART3_RX_GPIO GPIOC #define USART3_TX_PS GPIO_PinSource10 #define USART3_RX_PS GPIO_PinSource11 #define LED1_Pin GPIO_Pin_9 //Green #define LED2_Pin GPIO_Pin_8 //Orange #define LED3_Pin GPIO_Pin_7 //Blue #define LED4_Pin GPIO_Pin_6 //Red #define LED_GPIO GPIOC //Define Pushbutton=PB pins #define PB_Pin GPIO_Pin_0 #define PB_GPIO GPIOA void USART_Config(void) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); //Connect USART to pin A10 and A9 GPIO_PinAFConfig(USART3_TX_GPIO, USART3_TX_PS, GPIO_AF_1); GPIO_PinAFConfig(USART3_RX_GPIO, USART3_RX_PS, GPIO_AF_1); /* Configure USART3 pins: Rx and Tx ----------------------------*/ GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_Pin = USART3_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(USART3_TX_GPIO, &GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = USART3_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(USART3_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(USART3, &USART_InitStruct); USART_Cmd(USART3,ENABLE); } 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 Pushbutton(void) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE); GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 ; GPIO_InitStruct.GPIO_Mode =GPIO_Mode_IN; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_2; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA , &GPIO_InitStruct); } 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_ResetBits(LED_GPIO, LED3_Pin); GPIO_SetBits(LED_GPIO, LED2_Pin); if(PB_Input) { GPIO_SetBits(LED_GPIO, LED3_Pin); while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET); USART_SendData(USART3,0x55); GPIO_SetBits(LED_GPIO, LED4_Pin); *** while(USART_GetFlagStatus (USART3, USART_FLAG_RXNE)== RESET); temp1= USART_ReceiveData(USART3); if (temp1== 0x55) GPIO_SetBits(LED_GPIO, LED1_Pin); else GPIO_ResetBits(LED_GPIO, LED1_Pin); } else { GPIO_ResetBits(LED_GPIO, LED3_Pin); } } }2015-04-12 01:30 PM
Ok, if your short the pins and can see data, but doing it on the Xbee you can't suggests that it's not sending or echoing anything too you.
If you have two, configure one to send a constant stream of characters, and the other to receive. Look at the pins with a scope. Check also that the USART is not flagging an error condition, like a framing error or overrun.