2020-03-12 02:31 AM
I want to configure timer of 10 milliseconds
so for that according to calcuation
UPDATE Frequency = TIMCLK / ((PSC+1) * (ARR+1))
My clock frequency is 108 MHz
so when i set prescaler is 107 and ARR value is 9999
it should be updated frequcy is 100 Hz which is 10 millisecond timer
but i am not getting desired output here
Dipak Garasiya
Solved! Go to Solution.
2020-03-13 02:30 PM
Oh, a new thread. I just copy my last post to the other thread here.
Ahh, not 10 ms period, but 10 ms ticks that the timer counts.
You need to divide the clock by 1080000. That won't fit into a 16-bit prescaler. you need to prescale the APB bus timer clock too. The down side is that all timers on the same APB bus are slowed down. 54000 fits in a 16-bit prescaler, so you need to divide the bus timer clock by 20. I don't have the reference manual for that chip at hand to check what you can do with the bus clocks.
If you have a timer with 32-bit prescaler, you write (1080000-1) into the prescaler (supposing that the bus clock to that timer is 108MHz too).
Then you probably have 32-bit counter (and ARR) too. The maximum is then 42949672,95 seconds = ~ 1.3 years.
The other way could be the earlier 10 ms timer and a software counter. The timer interrupts at ARR (=10000-1) and the interrupt adds 1 to a global variable.
2020-03-12 03:55 AM
The peripheral Timer clock is not necessary the core clock.
Why not using the Systick timer, and the CMSIS function SysTick_Config() ?
As in SysTick_Config (SystemCoreClock / 100).
2020-03-12 04:05 AM
is my calculation is right ? or need some correction
2020-03-12 02:01 PM
Yes. 108 MHz = 108 000 000Hz = 108 000 000 peripheral clock ticks/second.
The prescaler divides that leaving 1 000 000 timer ticks per second
The counter then counts 10 000 timer ticks and wraps + generates an UG interrupt (if programmed to do so).
1 000 000 timer ticks per second means 1 microsecond per tick and 10 000 us = 10 ms.
Note that HAL_Delay takes MILLIseconds as a parameter.
2020-03-12 02:17 PM
The approach generally looks sound provided the clock inputs are known correctly.
>>but i am not getting desired output here
Ok, so expand on this so one might make some logical deductions about what's going on.
Toggling a pin will be at half the frequency.
In non-DIV1 cases the TIMCLK is typically 2x the APBxCLK to which it is attached.
>>Note that HAL_Delay takes MILLIseconds as a parameter.
I think the point would be to NOT grind in a loop, but rather do something productive.
One could also count off sub-tasks in SysTick, or use that to schedule.
2020-03-12 08:29 PM
Thanks for replying,
if my calculation is right then
// timer start here
while(1)
{
HAL_Delay(1000);
read timer here
}
it should be increment counter 100 count ( 10 ms * 100 == 1000 ms),but i am incrementing 1000 count why this is happen
2) If I configure timer for 1 ms then above code works perfactly
like if put HAL_Delay(1000) it increment 1000 counts
HAL_Delay(100) it increment 100 counts
HAL_Delay(10) it increment 10 counts
HAL_Delay(1) it increment 1 counts
Dipak Garasiya
2020-03-13 02:30 PM
Oh, a new thread. I just copy my last post to the other thread here.
Ahh, not 10 ms period, but 10 ms ticks that the timer counts.
You need to divide the clock by 1080000. That won't fit into a 16-bit prescaler. you need to prescale the APB bus timer clock too. The down side is that all timers on the same APB bus are slowed down. 54000 fits in a 16-bit prescaler, so you need to divide the bus timer clock by 20. I don't have the reference manual for that chip at hand to check what you can do with the bus clocks.
If you have a timer with 32-bit prescaler, you write (1080000-1) into the prescaler (supposing that the bus clock to that timer is 108MHz too).
Then you probably have 32-bit counter (and ARR) too. The maximum is then 42949672,95 seconds = ~ 1.3 years.
The other way could be the earlier 10 ms timer and a software counter. The timer interrupts at ARR (=10000-1) and the interrupt adds 1 to a global variable.
2020-03-17 02:00 AM
Thanks @turboscrew for your all your support
I have done it using third way
Thanks again,
Have a nice day!
Dipak Garasiya