Skip to main content
zoharr
Associate II
March 19, 2014
Question

trying to understand systick and timer at STM32L1xx

  • March 19, 2014
  • 4 replies
  • 931 views
Posted on March 19, 2014 at 08:26

Hello,

I'm using STM32L152 discovery board.

I'm trying to define systick for 100mSec, and tim2 to 1 sec.

After measuring the pin togeling with scope, this is what I came up with:

I know SystemCoreClock is 32MHz.

For systick using 100mSec interrupt,

I tried using SysTick_Config(SystemCoreClock / 100) to get 100mSec,

but adjusting with scope I ended with

SysTick_Config(SystemCoreClock / 153) to get accurate 100mSec.

For tim2 at 1 sec interrupt, I tried using:

Prescaler = (TIM_CLK / TIM counter clock) - 1 = 32MHz / 32KHz - 1= 1000 - 1

Period = (TIM counter clock / timer output clock) - 1 = (32KHz / 1Hz) - 1 = 32000 -1

but adjusting with scope I came up with 

Prescaler = 1000 - 1

Period=2092 - 1

to get 1 sec interrupt.

Can you please clarify what am I doing wrong? what I don't understand?

Thanks

#mco
    This topic has been closed for replies.

    4 replies

    stm322399
    Senior
    March 19, 2014
    Posted on March 19, 2014 at 10:28

    Let me try a guess:

    1) 32000000/153 cycles generates 10Hz signal ? you run 2,0915...MHz source clock

    2) TIM1 generates 1Hz signal after being divided by 1000*2092, so you run 2,092MHz clock

    3) STM32F152 has MSI clocked by default at 2,097MHz according the RM

    That's too much for simple coincidence, isn't it ?

    If I were you, I'll start to check my system initialization code.

    good luck.

    os_kopernika
    Visitor II
    March 22, 2014
    Posted on March 22, 2014 at 18:00

    +1

    ''I know SystemCoreClock is 32MHz.''

    At least now you know you do not know.

    Read about MCO.

    Tesla DeLorean
    Guru
    March 22, 2014
    Posted on March 22, 2014 at 18:18

    I'd give the values returned by RCC_GetClocksFreq() more credibility than SystemCoreClock, but the MCO pin is the way to go to verify internal clocks.

    /**
    * @brief Returns the frequencies of the System, AHB and APB busses clocks.
    * @note - The frequency returned by this function is not the real frequency
    * in the chip. It is calculated based on the predefined constant and
    * the source selected by RCC_SYSCLKConfig():
    *
    * - If SYSCLK source is MSI, function returns constant the MSI value
    * as defined by the MSI range, refer to RCC_MSIRangeConfig()
    *
    * - If SYSCLK source is HSI, function returns constant HSI_VALUE(*)
    *
    * - If SYSCLK source is HSE, function returns constant HSE_VALUE(**)
    *
    * - If SYSCLK source is PLL, function returns constant HSE_VALUE(**)
    * or HSI_VALUE(*) multiplied/divided by the PLL factors.
    *
    * (*) HSI_VALUE is a constant defined in stm32l1xx.h file (default value
    * 16 MHz) but the real value may vary depending on the variations
    * in voltage and temperature, refer to RCC_AdjustHSICalibrationValue().
    *
    * (**) HSE_VALUE is a constant defined in stm32l1xx.h file (default value
    * 8 MHz), user has to ensure that HSE_VALUE is same as the real
    * frequency of the crystal used. Otherwise, this function may
    * return wrong result.
    *
    * - The result of this function could be not correct when using fractional
    * value for HSE crystal.
    *
    * @param RCC_Clocks: pointer to a RCC_ClocksTypeDef structure which will hold
    * the clocks frequencies.
    * @retval None
    */
    void RCC_GetClocksFreq(RCC_ClocksTypeDef* RCC_Clocks)
    {
    ..

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    AKhan.17
    Associate
    October 2, 2018

    time between 2 ticks = 100 ms so the reload value would be= 32000000*100ms-1 or 32000000*.01-1 which is 319999. can you please explain me why your are dividing 32Mhz by 100ms