cancel
Showing results for 
Search instead for 
Did you mean: 

UART, Xbee and XCTU

mauro2
Associate II
Posted on December 10, 2015 at 12:43

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?
9 REPLIES 9
Posted on December 10, 2015 at 15:03

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);
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mauro2
Associate II
Posted on December 10, 2015 at 16:32

if you are talking about uC pin, from the manual:

PA2

Basic Function: USART2_TX/TIM5_CH3/ADC123_IN2/TIM2_CH3

PA3

Basic Function: USART2_RX/TIM5_CH4/ADC123_IN3/TIM2_CH4

Posted on December 10, 2015 at 16:42

Correct, you'll observe that they aren't remapped, doing so moves the USART2 pins elsewhere.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mauro2
Associate II
Posted on December 10, 2015 at 17:59

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 why

Posted on December 10, 2015 at 18:56

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?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mauro2
Associate II
Posted on December 11, 2015 at 09:16

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?

Posted on December 11, 2015 at 16:02

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);
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mauro2
Associate II
Posted on December 11, 2015 at 17:47

can u explain how to use atoi and itoa functions? thx

Posted on December 11, 2015 at 20:54

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);
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..