2014-04-01 09:47 AM
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 Angelo2014-04-01 10:22 AM
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,0002014-04-02 01:10 AM
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 this2014-04-02 01:27 PM