cancel
Showing results for 
Search instead for 
Did you mean: 

Setting up the Clock for HSE 16mhz and usart2 baudrate is lower as configured

sam239955
Associate II
Posted on December 06, 2011 at 21:27

hi,

I have a stm32f103zet6 board and im using keil. the external crystal (HSE) has 16mhz.

When im setting up my USART2 @ 38400bau in Keil Debugger it shows me that its running with 12668 baud ??? (=38400/3,19).

Where is my fault? With the Eval board from keil everything seems to be allright (but it has 8mhz HSE).

Here is my code:

 Please help.

Is my PLL/RCC Setup correct?  I want to use USB as VCOM too.

11 REPLIES 11
Posted on December 06, 2011 at 23:17

For Keil to know what the crystal frequency is you'd need to configure Options->Target->Xtal

For the library code to know, you'd need to define HSE_Value correctly in your project.

Versions of the library are also capable of setting the CPU speed prior to executing main(), and this can be problematic for systems not using 8 MHz because some of the values are hard coded in system_stm32f10x.c

Want to confirm internal clocks, use the MCO pin.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sam239955
Associate II
Posted on December 07, 2011 at 00:03

#if !defined  HSE_VALUE

 #ifdef STM32F10X_CL   

  #define HSE_VALUE    ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */

 #else

  #define HSE_VALUE    ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */

 #endif /* STM32F10X_CL */

#endif /* HSE_VALUE */

i can define HSE_VALUE 16    ?
Posted on December 07, 2011 at 00:50

i can define HSE_VALUE 16    ?

Well you'd typically configure it in the command line option for the compiler in your project.

Options-> C++ -> Preprocessor Symbols -> Define

HSE_VALUE=16000000

You should however look at SetSysClock72() in system_stm32f10x.c, at least in V3.3.0 the PLL values are hard coded to the values assuming 8MHz

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sam239955
Associate II
Posted on December 07, 2011 at 01:44

hi clive,

btw. thank you for helping me!

I done a new Keil Project with cmsis, stlib 3.5 and set the xtal value in keil to 0 that helped me to get the correct sysclock and everything. but the usart has now the double (76759baud) instead of my written value 38

I attached a screenshot.

________________

Attachments :

keil_xtal_16mhz.JPG : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0zQ&d=%2Fa%2F0X0000000bgd%2FPjCvFqPy6pzy7jOFhgWro_xcNSS3q155rKB3VRl_uoA&asPdf=false
sam239955
Associate II
Posted on December 07, 2011 at 01:48

OK! Thank you so much clive!!!

setting up HSE=16000000 and XTAL 16.0 in keil preprocessor helped!!
Posted on December 07, 2011 at 02:05

The USART2_BRR = 0x1D5 is certainly indicative of 76KBaud for a 36 MHz APB1 clock. Now I presume this value was achieved using USART_Init()? As that would mean the library thinks APB1 is at 18 MHz, and it still thinks, or has set, HSE_Value = 80000000

The values in the RCC seem to be (HSE/4)*9, however I didn't fully decode it.

You could use

    RCC_ClocksTypeDef RCC_ClockFreq;

    RCC_GetClocksFreq(&RCC_ClockFreq);

to inquire what the system thinks it is up to.

You could also look at the value of 'HSE_Value', the lowercased version being the one used by the library to do speed computations. You might want to check that the define you used of HSE_VALUE has global scope, and you didn't just modify the options on a single file.

You could try manually altering USART2_BRR to 0x3AA or 0x3A9 to confirm if you can get data out at 38400 baud

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sam239955
Associate II
Posted on December 07, 2011 at 03:22

RCC_ClockFreq,0x0A:

SYSCLK_Freq: 72000000

HCLK_Freq: 72000000

PCLK1_Freq: 36000000

PCLK2_Freq: 72000000

ADCCLK_Freq: 36000000

sam239955
Associate II
Posted on December 07, 2011 at 23:20

what do you think about this values clive?

Posted on December 08, 2011 at 00:58

what do you think about this values clive?

With the exception of the ADCCLK (14 MHz max) they would appear reasonable.

If the baud rate is still not being set up right, I'd need to see more of the project/library code. You could zip that up and attach if you wish, and I'll take a look when I get a chance.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..