cancel
Showing results for 
Search instead for 
Did you mean: 

UART junk values

vivek2
Associate II
Posted on September 03, 2012 at 09:20

Hello,

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

  USART_Cmd(UART4, ENABLE);

  return;

}

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-> &sharpSymbols

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

Any help?

#wait-for-you--clive1-!-!-! #wait-for-you--clive1-!-!-! #wait-for-you--clive1-!-!-!
9 REPLIES 9
vinothraj
Associate II
Posted on September 03, 2012 at 12:21

hi vivek,

if you got junk characters in serial monitor,u would do the mistake in the HSE value. i attached code.if you want it,download and replace your files.Then try it.:)

________________

Attachments :

stm32f4xx.h : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HzaA&d=%2Fa%2F0X0000000bNv%2FLZdsrp6gcnB0odrBMoX6X.U29ng9DR6.21cwfSLnUBI&asPdf=false

system_stm32f4xx.c : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HzRs&d=%2Fa%2F0X0000000bNr%2Ft7X2K1bWSD1gO6ZXP7_D48ULbzQmXVsoZkc9lIiHYdQ&asPdf=false
Posted on September 03, 2012 at 16:56

It's going to depend on what board and chip you're talking about, and what the HSE crystal is, and how the PLL is set, and the way that value is reflected by the compiler.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
vivek2
Associate II
Posted on September 04, 2012 at 02:38

Hi vinoth,

I tried replacing the two files that u sent me and unfortunately it did not make any change. any suggestions?

vivek2
Associate II
Posted on September 04, 2012 at 02:45

Hi clive1,

Im using STM32F4-Discovery board with STM32F407VGT6 IC. The crystal present on the board is a 8Mhz crystal and im using a keil uv4 IDE. As i said earlier i tried replacing the HSE value in stm32f4xx.h file to 8Mhz and it did not make any change. I guess the problem might be bcoz of the voltage value on the tx pin which is 3V. Any suggestion wud be much appreciated.

vivek2
Associate II
Posted on September 04, 2012 at 03:04

Hi Clive,

I found the below suggestion in another forum. Do u think this is my problem:

your PC is expecting data in the ‘official’ RS232 -3V to -15V / +3V to +15V range. The 3.3V from the STM32F4 might not be enough. You need to use a level converter for this and that’s why the data is inverted. If you already do use a level converter, check if the level converter inverts the signal.

Have a look at this picture:

http://upload.wikimedia.org/wikipedia/commons/d/de/RS-232_timing.png

Posted on September 04, 2012 at 03:06

If you use the standard library, you have to be conscious of the PLL settings reflect the external oscillator. See system_stm32f4xx.c

If you think the speeds are wrong, output a stream of data and confirm the bit timings against the baud rate.

The USART uses CMOS level signalling, you'd need to connect that to RS232 drivers to connect to a PC serial port. These drivers act as inverters.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
vinothraj
Associate II
Posted on September 04, 2012 at 06:59

tell your email id and i will send any alternate files for uart.

vivek2
Associate II
Posted on September 04, 2012 at 07:04

Hello,

Unfortunately, i don hav max232 at the moment and cud never get it at earliest. All i have is max3222cpn and max237cng. Can any1 suggest which is the better option here.

thnx
frankmeyer9
Associate II
Posted on September 04, 2012 at 07:50

Some other files will not do it. You will need some hardware.

Either  using a MAX323, or something simple like

http://www.acmesystems.it/106