AnsweredAssumed Answered

UART junk values

Question asked by vivek on Sep 3, 2012
Latest reply on Sep 4, 2012 by fm
I was using UART4 pin PA0 and PA1 for my project. I have done all the initialization procedures right but ended up getting junk values at the output. below is my initialization function.

void UART_LowLevel_Init(void) {
  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;
  /* enable peripheral clock for USART2 */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
    /* GPIOA clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);   //for tx,rx

    //Connect GPIO pins to the UART
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource0 ,GPIO_AF_UART4); //Tx
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource1 ,GPIO_AF_UART4); //Rx  
  //Configure the GPIO Pins
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
  //Initialize the USART
  //USART_InitStructure.USART_BaudRate = 2625000;   //MAximum rate for APB1 with 16 over-sampling
  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_Rx | USART_Mode_Tx;
  USART_Init(UART4, &USART_InitStructure);
  //Enable (UE) the USART


I use USART_SendData function to send characters to the output. When I monitored the Tx line through a logic analyzer the values seem to be inverted. For example: If i send value 0x61, I get a value 0x86 at the output. This value is totally different to the value that i see in the serial monitor software in pc and it varies from time to time. Also I see a continuous output of value 0x00 in the hyperterminal even though  the program is not running.

I also tried doing the below which I found in another post:

If you do not defined HSE_VALUE for the proper crystal freq it gets the default value of 25000000 Hz, irrelevant to the selected board (f.e. Discovery), see lines 90..93 in the Libraries/Device/STM32F4xx/Include/stm32f4xx.h (meaning Project Explorer).

The best way to declare  the proper frequency is to set HSE_VALUE globally in Atollic IDE:
Project->Poperties->C/C++ General-> Paths and Symbols-> #Symbols

Press Add.. btn, then type HSE_VALUE and in the next line type 8000000 (for Discovery).

Any help?