-1
\$\begingroup\$

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


}
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Weird characters usually come about because of wrong baud rate. Measure with a scope to confirm it is really what you think it is. And if it isn't, then start looking at all the registers involved in setting the baud rate. You will have to dig into the whole clock generation chain for the processor. \$\endgroup\$ – mkeith Apr 14 at 23:16
  • 1
    \$\begingroup\$ Yeah, that was exactly that, I just found out that my clock speed was divided by 2, meaning that the actual baudrate was 57600. Thank you very much! \$\endgroup\$ – Nathaniel Brochu Apr 15 at 14:08
  • \$\begingroup\$ It might be helpful to future people if you could answer your own question and accept the answer. It is perfectly OK to accept your own answer if it is the best one (or the only one). \$\endgroup\$ – mkeith Apr 15 at 15:28
0
\$\begingroup\$

I just found out that my clock speed was divided by 2, meaning that the actual baudrate was 57600. Thanks for the help!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.