cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f405 help for timer seconds counter

Angelo1
Associate II
Posted on April 01, 2014 at 18:47

Dear,

i need to implement a timer, with seconds step, just as a watch.

I have now the follwing clock (internal) set

static void SetSysClock(void)

{

        /* PLL (clocked by HSI) used as System clock source */

        /* Select regulator voltage output Scale 1 mode,

         * System frequency up to 168 MHz */

        RCC->APB1ENR |= RCC_APB1ENR_PWREN;

        PWR->CR |= PWR_CR_VOS;

        /* HCLK = SYSCLK / 4*/

        RCC->CFGR |= RCC_CFGR_HPRE_DIV4;

        /* PCLK2 = HCLK / 2*/

        RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;

        /* PCLK1 = HCLK / 4*/

        RCC->CFGR |= RCC_CFGR_PPRE1_DIV4;

        /* Configure the main PLL */

        RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |

                 (RCC_PLLCFGR_PLLSRC_HSI) | (PLL_Q << 24);

        /* Enable the main PLL */

        RCC->CR |= RCC_CR_PLLON;

        /* Wait till the main PLL is ready */

        while((RCC->CR & RCC_CR_PLLRDY) == 0)

        {

        }

        /* Configure Flash prefetch, Instruction cache, Data cache and wait state */

        FLASH->ACR = FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;

        /* Select the main PLL as system clock source */

        RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));

        RCC->CFGR |= RCC_CFGR_SW_PLL;

        /* Wait till the main PLL is used as system clock source */

        while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);

        {

        }

}

From here, can i have a 1 second interrupt ?

Ciao

Angelo
3 REPLIES 3
Posted on April 01, 2014 at 19:22

Ok, so timers need a basic grasp of math, specifically factors, and ones that fit within certain number spaces. There are often many solutions.

For example with a 168,000,000 Hz TIMCLK (APB2) you are trying to get 1 Hz

168,000,000 / 1 = 168,000,000

16800 * 10000 = 168,000,000 (Note both fit in 16-bit)

Prescaler = 16800 - 1; // TIM ticks at 10000 Hz (10 KHz)

Period = 10000 - 1; // TIM rolls over at 10000 ticks @ 10 KHz, So 1 Hz, 1 Second

Update Interrupt at 1 Hz

For an 84 MHz TIMCLK (APB1)

8400 * 10000 = 84,000,000

Prescaler = 8400 - 1;

Period = 10000 - 1;

Update Interrupt at 1 Hz

For 10 Hz, I could use

Prescaler = 840 - 1;

Period = 10000 - 1;

or

Prescaler = 8400 - 1;

Period = 1000 - 1;

8400 * 1000 = 8,400,000

and

84,000,000 / 10 = 8,400,000

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
chen
Associate II
Posted on April 02, 2014 at 10:10

Hi

I was curious what your reply would be clive1.

Would the RTC not be a better solution?

I know it can generate an alarm IRQ.

Not sure if this is one shot only or can be repeatable.

If the goal is to be able to track 'real seconds' - would the RTC be better for this
Angelo1
Associate II
Posted on April 02, 2014 at 22:27

Dear,

many thanks for both kind answers. I tested both cases, i can have working the 1 second increment for both cases.