2016-08-22 07:25 AM
Hi,
i have a question about Timer1 of the �C mentioned above.
I want to use the Timer1 for a SW-Timer with second-resolution.
I have the idea, to start the timer with a frequency of 1 KHz and use the Auto-repition-Counter to Keep times with second-Resolution.
Does anybody have a Startup or initialization-code to start the tim1 with 1 KHz frequency and to configure the Auto-Repition-Counter of Timer1 Count with a frequency of 1 Hz?
Thanks
in advance for any useful support and please excuse my bad English.
#tim1 #timer2016-08-22 07:46 AM
The repetition counter is only 8-bit, so isn't going to handle 1000, but could handle 250
Most people use SysTick for a 1KHz (1ms) ticker. To use TIM1 to do 1Hz (1second) you'd select the Prescaler (16-bit) and Period to divide down the MHz the timer is running at. If the TIMCLK is 48MHz, that's like 48000 x 1000, so Prescaler = 48000 - 1; // Encoded as N-1, so [0 .. N-1] has N states Period = 1000 - 1;2016-08-22 09:26 AM
Thanks for the answer. But then Timer is still counting with 1 KHz when you are Setting the prescaler to 47999, thats ok.
But how can I start the TIM1 with any other functions then just counting up and then increment the auto-repition counter every second, I know that the autoreload-value has to be like 999.
It's ok that the repition counter is just 8 bit width. That's good enough.
An init-code would be nice, compatible with the newest HAL-Drivers of STMCubeF0
2016-08-22 10:35 AM
Sorry, no HAL code from me
The timer will clock at 1KHz, the Update will occur at 1Hz. If you set the repetition count to 60-1 you'd get an interrupt once per minute, but as far as I recall you can't see which second the repeat is counting by looking at the TIM registers.2016-08-25 03:43 AM
Hi Mr. µCells,
I recommend to get a look to ''TIM_TimeBase'' example in
at this path STM32Cube_FW_F0_V1.60\Projects\STM32072B_EVAL\Examples\TIM\TIM_TimeBase
For your case to get 1Hz frequency you just consider Prescaler and period as follow:
/* set the Timer prescaler to get 1MHz as counter clock */
Tim1Prescaler= (uint16_t) (SystemCoreClock / 1000000) - 1;
/* Initialize the PWM period to get 1Hz as frequency from 1MHz */
Period = 1000000 / 1;
/* Configure the Timer prescaler */
TIM1->PSC = Tim1Prescaler;
/* Configure the period */
TIM1->ARR = Period-1;
You can aslo use
tool to generate correctly your initialization code and start you project development.
For other Timer related information you can check the new Timer cookbook application note -Hannibal-