/var/www/restricted/ssh/stm32/www/stm32circle/ STM CircleOS forum / How to set correct baud rate on USART2

Username:     
Password:     
             

Forum

# 1   2008-04-27 02:00:49 How to set correct baud rate on USART2

markus.rose
New member
From: Freiburg / Germany
Registered: 2008-03-30
Posts: 3

How to set correct baud rate on USART2

Hi,

I've taken the example project "toggle_with_CircleOS" to do some first steps with my new STM32 Primer. Adding the attached code to initialize the USART2 enables me to send some bytes via pin PA2 which I can see on my Scope.
On 9600 baud I would expect to see a bit-length of 104 µs, but unfortunately every bit is about 80 µs long. What else do I need to do to set the clock/prescaler correctly?

Any comments are welcome. Many thanks in advance...

Regards,

Markus



void InitUSART()
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Enable USART2 clocks */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

  /* Configure USART2 Tx (PA.2) as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USART2 Rx (PA.3) as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
 
  /* USART2 configuration ------------------------------------------------------*/
  /* USART2 configured as follow:
        - BaudRate = 9600 baud 
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
        - USART Clock disabled
        - USART CPOL: Clock is active low
        - USART CPHA: Data is captured on the second edge
        - USART LastBit: The clock pulse of the last data bit is not output to
                         the SCLK pin
  */
  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_InitStructure.USART_Clock = USART_Clock_Disable;
  USART_InitStructure.USART_CPOL = USART_CPOL_Low;
  USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
  USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
  USART_Init(USART2, &USART_InitStructure);

  /* Enable the USART2 */
  USART_Cmd(USART2, ENABLE);
}

Offline

 

# 2   2008-04-28 05:06:06 How to set correct baud rate on USART2

TroyC3
Member
Registered: 2007-10-13
Posts: 10

Re: How to set correct baud rate on USART2

Hi Mark,

Located in " stm32f10x_conf.h" file is this line for what external crystal you are using

/* In the following line adjust the value of External High Speed oscillator (HSE)
   used in your application */
#define HSE_Value    ((u32)12000000) /* Value of the External oscillator in Hz*/

On the primer it is 12Mhz.

If this doesn't help, I can send you my working example, I did it awhile and also struggled with the bit trimming but I think that #define was it.


Troy

Offline

 

# 3   2008-04-28 20:01:16 How to set correct baud rate on USART2

markus.rose
New member
From: Freiburg / Germany
Registered: 2008-03-30
Posts: 3

Re: How to set correct baud rate on USART2

Hi Troy,

Thanks for your quick answer. The HSE_Value was already set to that value. Is there any prescaler I need to set?
Your working example would be a great help to me. Could you send it via email please?

Many thanks,

Markus

Offline

 

# 4   2008-04-29 03:45:45 How to set correct baud rate on USART2

TroyC3
Member
Registered: 2007-10-13
Posts: 10

Re: How to set correct baud rate on USART2

Hi Markus,

Okay I think I know whats going on,  you need to add the library x.c files that you are using in to the project, yes I know it will compile with no warnings with out them, but they need to be listed in project window.

on the off chance that doesn't fix it I mailed you  a simple hello World project that sends "Hello World" out com 2 9600 baud, I would like to post it here as well how do I do that?


Troy smile

Offline

 

# 5   2008-04-29 21:57:58 How to set correct baud rate on USART2

markus.rose
New member
From: Freiburg / Germany
Registered: 2008-03-30
Posts: 3

Re: How to set correct baud rate on USART2

Hi Troy,

Got your HelloWorld project and it works perfect.  :-)
Thanks for your help and your quick responses.

Markus

Offline

 

# 6   2009-01-09 12:37:45 How to set correct baud rate on USART2

starchild
New member
Registered: 2008-02-13
Posts: 2

Re: How to set correct baud rate on USART2

This "Hello World" Project would help ma also a lot.
I have a similar problem. The USART is transmitting something, but not with the correct Baudrate and without Scope it's difficult to find out more...

Offline

 

# 7   2009-01-09 12:58:00 How to set correct baud rate on USART2

Francis
Administrator
From: France-Grenoble
Registered: 2007-07-09
Posts: 890

Re: How to set correct baud rate on USART2

As said above, most of time, a wrong baud rate comes with a wrong value for "#define HSE_Value" in the stm32f10x_conf.h  file (attention: this file is often duplicated... so you have to check which one is active).

Offline

 

# 8   2009-01-09 14:29:43 How to set correct baud rate on USART2

starchild
New member
Registered: 2008-02-13
Posts: 2

Re: How to set correct baud rate on USART2

I've already checked the HSE value bevore asking for an example. It's correct (12000000)

Offline

 

# 9   2009-01-19 16:04:41 How to set correct baud rate on USART2

domen
Member
Registered: 2008-10-27
Posts: 10

Re: How to set correct baud rate on USART2

I've been bitten by this too... the problem is that fwlib included with RIDE is compiled with value 8000000, so just changing the header doesn't help, you need to recompile fwlib... or *2/3 baudrates ;-)

Offline

 

# 10   2009-01-20 10:49:41 How to set correct baud rate on USART2

Francis
Administrator
From: France-Grenoble
Registered: 2007-07-09
Posts: 890

Re: How to set correct baud rate on USART2

You can also copy some of the STM32LIB files in your own project, without using the complete library. It's what we do now in CircleOS.

Offline

 

# 11   2009-03-31 03:46:47 How to set correct baud rate on USART2

FR4
Member
Registered: 2008-11-09
Posts: 26

Re: How to set correct baud rate on USART2

Getting the usart2 to work is driving me crazy.

I followed all the hints from above, but can't get a reasonable result.

Here´s my code: (CircleOS 3.6)

  USART_InitTypeDef USART_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
  /* Enable USART2 clocks */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

/* Configure USART2 Tx (PA.2) as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configure USART2 Rx (PA.3) as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* USART2 configuration ------------------------------------------------------*/
  /* USART2 configured as follow:
        - BaudRate = 9600 baud
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
        - USART Clock disabled
        - USART CPOL: Clock is active low
        - USART CPHA: Data is captured on the second edge
        - USART LastBit: The clock pulse of the last data bit is not output to
                         the SCLK pin
  */
  USART_InitStructure.USART_BaudRate = 4800;
  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(USART2, &USART_InitStructure);

  /* Enable the USART2 */
  USART_Cmd(USART2, ENABLE);


Primer2 is receiving data but not the correct ones.

How can i calculate the correct baudrate. Reference manual says(page 622):

baudrate = fck / 16*USARTDIV

with baudrate=4800 and fck=50MHz -> USARTDIV=651,0417 which is Mantissa=0x28b and fraction= 1

but that gives me no meaningfull data as  well.

Can anybody who succeded in getting usart2 to work give me a working example?

Offline

 

# 12   2009-06-25 07:50:04 How to set correct baud rate on USART2

Mysterio
Member
From: Germany/NRW
Registered: 2009-05-22
Posts: 23

Re: How to set correct baud rate on USART2

@TroyC3 or markus.rose

Hello,

In my case nothing does work correctly.
Are you still having this working "Hello World"-Project.

Could you send me an email with this project, please?

Many Thanks

Last edited by Mysterio (2009-06-26 14:19:51)


The only definitly knowledge we have, is that we don't know anything. -Sokrates-

Offline

 

# 13   2009-06-25 09:59:05 How to set correct baud rate on USART2

Francis
Administrator
From: France-Grenoble
Registered: 2007-07-09
Posts: 890

Re: How to set correct baud rate on USART2

I believe that the ST libraries assume that HSE=8MHz, and it is 12MHz for Primer2. Perhaps the issue is there.

Offline

 

# 14   2009-07-01 12:39:49 How to set correct baud rate on USART2

cronicon
New member
Registered: 2009-02-09
Posts: 7

Re: How to set correct baud rate on USART2

For 9600 baud
add this to your Init code ate the bottom


"USART2->BRR = 0x782;//Sets the baud rate to 9600"


Got this from "HANDHELD-DATACOLLECTION-TERMINAL-STM32-Primer2" project and it works

Last edited by cronicon (2009-07-01 14:20:56)

Offline

 

# 15   2010-08-17 15:38:44 How to set correct baud rate on USART2

VanKurt
Member
Registered: 2010-08-12
Posts: 10

Re: How to set correct baud rate on USART2

I would need that project too. Could someone mail it to me? Thanks :-)

Offline

 

# 16   2010-08-19 07:10:18 How to set correct baud rate on USART2

yrt
Administrator
From: Grenoble-France
Registered: 2008-06-11
Posts: 520
Website

Re: How to set correct baud rate on USART2

Offline

 

Board footer