Timer calculator algorithm

Timer Calculator is a free software development tool used for easier calculation of timer interrupts.
Post Reply
Author
Message
pspeirs
Posts: 181
Joined: 05 Sep 2012 07:54

Timer calculator algorithm

#1 Post by pspeirs » 08 Jan 2023 03:04

Hi,

I'm looking at embedding the calculation for the timer into my ARM C application, can anyone share a calculation (or maybe the code snippet) to generate the most appropriate pre-scaler and timer period values.

Playing with the below code at the moment and the results are close to the timer calculator however I think the calculator may be a touch more refined.

Code: Select all

int cal_pwm(float freq_hz, int duty) {

	uint32_t period_cycles = MCU_CLOCK / freq_hz;
	uint16_t PrescalerValue = (uint16_t)(period_cycles / MAX_RELOAD + 1);
	uint16_t overflow = (uint16_t)((period_cycles + (PrescalerValue / 2)) / PrescalerValue);
	uint16_t CCR2_Val = (uint16_t)(overflow * ((float)duty / 100.));

	printf("\r\nPrescaler: %u", PrescalerValue);
	printf("\r\nOverflow: %u", overflow);
	printf("\r\nCCR2 Value: %u", CCR2_Val);

	return 1;
}

As an update, I ran the values between the timer calculator and the above function to work out the values to select for a 0.5Hz pulse

The above function returns Period: 65514 and PSC: 2198 which works out to 2000.94 ms and the timer calculator to exactly 2000ms, so would prefer to see the formula for the timer calculator.


Cheers,
Paul

frank.malik
Posts: 96
Joined: 09 Apr 2021 20:37

Re: Timer calculator algorithm

#2 Post by frank.malik » 09 Jan 2023 19:21

Hi Paul,

thanks for this question.

First, what MCU are you using. Obviously an STM32, but which one exactly.
Basically the calculation should be the same for most of the devices. If you say "Period: 65514", is this the value
you would place in the ARR register?
Kind regards
Frank

Fusion for STM32 v8, STM32F407ZG@168MHz, 4" TFT capacitive
mikromedia 3, PIC32MZ2048EFH100@200MHz, 3" TFT capacitive
NECTO Studio 3.0.0, mikroC AI for ARM/PIC32, mikroSDK v.2.7.2

pspeirs
Posts: 181
Joined: 05 Sep 2012 07:54

Re: Timer calculator algorithm

#3 Post by pspeirs » 10 Jan 2023 12:35

Hi,

I'm using the STM32F107VCT6 specifically. yes, sorry, my post may look a little confusing . . . The period is the value that would be placed in the ARR register.
Just to make things clear, the below is what I'm currently calculating and where the resulting values are used.

Code: Select all

#define CLOCK_CYCLES_PER_SECOND  72000000   // More likely 36MHz
#define MAX_RELOAD               0xFFFF

 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
 TIM_OCInitTypeDef  TIM_OCInitStructure;

uint32_t period_cycles = CLOCK_CYCLES_PER_SECOND / freq_hz;
uint16_t PrescalerValue = (uint16_t)(period_cycles / MAX_RELOAD + 1);
uint16_t overflow = (uint16_t)((period_cycles + (PrescalerValue / 2)) / PrescalerValue);
uint16_t CCR1_Val = (uint16_t)(overflow * ((float)percent_duty / 100.));

 /* Time Base configuration */
 TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
 TIM_TimeBaseStructure.TIM_Period = overflow;
 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
 TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
 

AntiMember
Posts: 136
Joined: 02 Jan 2020 19:00

Re: Timer calculator algorithm

#4 Post by AntiMember » 10 Jan 2023 16:50

pspeirs wrote:
08 Jan 2023 03:04
.........................
As an update, I ran the values between the timer calculator and the above function to work out the values to select for a 0.5Hz pulse
The above function returns Period: 65514 and PSC: 2198 which works out to 2000.94 ms and the timer calculator to exactly 2000ms, so would prefer to see the formula for the timer calculator.
Sorry, I missed zero.. Corrected:
On the Windows calculator:
period_cycles = 144000000
PrescalerValue = 144000000/(65535 + 1) = 2197,26
overflow = (144000000 + 2197,26/2) / 2197,26 = 65536,66

TimerClock = 72000000
PrescalerValue = 2197
Overflow = 65535
72000000/2197/(65535+1) = 0,5000604517523897Hz == 1,999758222222222 sec.

AntiMember
Posts: 136
Joined: 02 Jan 2020 19:00

Re: Timer calculator algorithm

#5 Post by AntiMember » 11 Jan 2023 19:27

Correct parentheses, add check (MAX_RELOAD+1) < period_cycles.
And can be used.

AntiMember
Posts: 136
Joined: 02 Jan 2020 19:00

Re: Timer calculator algorithm

#6 Post by AntiMember » 19 Jan 2023 18:36

The fate of the calculator remains unknown.

carterodell
Posts: 1
Joined: 29 Dec 2023 02:42

Re: Timer calculator algorithm

#7 Post by carterodell » 29 Dec 2023 02:43

pspeirs wrote:
08 Jan 2023 03:04
Hi,

I'm looking at embedding the calculation for the timer into my ARM C application, can anyone share a calculation (or maybe the code snippet) to generate the most appropriate pre-scaler and timer period values.

Playing with the below code at the moment and the results are close to the timer calculator however I think the calculator may be a touch more refined.

Code: Select all

int cal_pwm(float freq_hz, int duty) {

	uint32_t period_cycles = MCU_CLOCK / freq_hz;
	uint16_t PrescalerValue = (uint16_t)(period_cycles / MAX_RELOAD + 1);
	uint16_t overflow = (uint16_t)((period_cycles + (PrescalerValue / 2)) / PrescalerValue);
	uint16_t CCR2_Val = (uint16_t)(overflow * ((float)duty / 100.));

	printf("\r\nPrescaler: %u", PrescalerValue);
	printf("\r\nOverflow: %u", overflow);
	printf("\r\nCCR2 Value: %u", CCR2_Val);

	return 1;
}

As an update, I ran the values between the timer calculator and the above function to work out the values to select for a 0.5Hz pulse

The above function returns Period: 65514 and PSC: 2198 which works out to 2000.94 ms and the timer calculator to exactly 2000ms, so would prefer to see the formula for the timer calculator.


Cheers,
Paul
Identify the MCU you're using and inform me first. Of course it's an STM32, but which model exactly? The vast majority of devices probably will employ the same formula. Could you please tell me what the value would be to enter "Period: 65514" into the ARR register?

Post Reply

Return to “Timer Calculator”