I'm having problems communicating with UART on my STM32L100RC discovery board. I get character such as xþ when I'm sending the letter 'F'. I'm using Hercule to receive data. Hercule's configuration is : Baudrate = 9600, Data size = 8, Parity = None, Hankshake = OFF and Mode = Free. So here is my code and if you can tell me what I do wrong, I'd be very grateful!
Also, I think it is important that I specify that I can get my code working using the HAL library (but I can't use it for my program), so that puts my physical hardware out of the equation.
void UART_Initialization (void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
// Initializing RX_Pin
GPIO_InitTypeDef GPIO_InitStructRX;
GPIO_InitStructRX.GPIO_Pin = RX_Pin;
GPIO_InitStructRX.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructRX.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructRX.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructRX);
// Initializing TX_Pin
GPIO_InitTypeDef GPIO_InitStructTX;
GPIO_InitStructTX.GPIO_Pin = TX_Pin;
GPIO_InitStructTX.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructTX.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructTX.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructTX.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructTX);
USART_InitTypeDef USART_InitStructUSART;
USART_InitStructUSART.USART_BaudRate = 9600;
USART_InitStructUSART.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructUSART.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructUSART.USART_Parity = USART_Parity_No;
USART_InitStructUSART.USART_StopBits = USART_StopBits_1;
USART_InitStructUSART.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &USART_InitStructUSART);
// Setting alternate functions for RX_Pin and TX_Pin
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
USART_Cmd(USART1, ENABLE);
}
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_DATA(Data));
/* Transmit Data */
USARTx->DR = (Data & (uint16_t)0x01FF);
}
int main(void)
{
UART_Initialization();
while(1)
{
for (int i = 0; i <500000; i++)
{}
USART_SendData(USART1, 'F');
}
}