2015-12-10 03:43 AM
so here the problem:
I'm trying to make a communication between two xbee module. The first is connect to stm32f10x (steval-mki121v1 eval board) and the second to the pc via usb.i want to trasmits data from the first to the second using the uC UART and reads data using the xctu tool.I'm having some trouble and i really don't understand where i'm wrong.here the code to set the uart:void UARTInit(void){ USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; //NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); /* Configure USART2 pins: Rx and Tx ----------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); GPIO_PinLockConfig(GPIOA, GPIO_Pin_2); GPIO_PinLockConfig(GPIOA, GPIO_Pin_3); USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; 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(USART2, &USART_InitStructure); /*NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);*/ need this? USART_Cmd(USART2,ENABLE); USART_ITConfig(USART2, USART_IT_TXE, ENABLE); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);}the Tx pin is connect to DIN pin of xbee module and the Rx pin to the DOUT pin.maybe some configuration to do with xctu?2015-12-10 06:03 AM
Would help if you reviewed the Data Manual about the pin utilization, and didn't turn on interrupts you're not using.
void UARTInit(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
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(USART2, &USART_InitStructure);
USART_Cmd(USART2,ENABLE);
}
2015-12-10 07:32 AM
if you are talking about uC pin, from the manual:
PA2Basic Function: USART2_TX/TIM5_CH3/ADC123_IN2/TIM2_CH3PA3Basic Function: USART2_RX/TIM5_CH4/ADC123_IN3/TIM2_CH42015-12-10 07:42 AM
Correct, you'll observe that they aren't remapped, doing so moves the USART2 pins elsewhere.
2015-12-10 08:59 AM
i add this string to the code you posted for remapping
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);but it still doesn't work and i really don't know why2015-12-10 09:56 AM
but it still doesn't work and i really don't know why
They are NOT remapped, if you remap the pins they will move AWAY from PA2/PA3 Perhaps you need to start looking at other parts of the code?2015-12-11 12:16 AM
Perhaps you need to start looking at other parts of the code?
For example?Edit: i found the error. The code was correct, just i don't insert the function in the main XD.Anyway, another problem: when i trasmit numbers i receive strange letters (probably ascii codes). so what is the function for the conversion from ascii to numeric format?
2015-12-11 07:02 AM
From ASCII, atoi() and sscanf()
To ASCII itoa() and sprintf()char *dec32(unsigned long i)
{
static char str[16];
char *s = str + sizeof(str);
*--s = 0;
do
{
*--s = '0' + (char)(i % 10);
i /= 10;
}
while(i);
return(s);
}
2015-12-11 08:47 AM
can u explain how to use atoi and itoa functions? thx
2015-12-11 11:54 AM
These would seem to be things you could get from Google/Bing, or basic C programming tutorials. I really can't explain to you how the XBee works, or how you're transmitting data over the link, I don't have adequate context/foundation.
http://www.cplusplus.com/reference/cstdlib/atoi/
void OutString(char *s)
{
while(*s)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, *s++); // Send Char
}
}
{
char buffer[16];
itoa (12345,buffer,10);
OutString(buffer);
}