Skip to main content
Ala
Senior
January 12, 2022
Solved

Timer interrupt and GPIO toggling

  • January 12, 2022
  • 3 replies
  • 2625 views

hey there

I have a STM32f030k6t6 which I wish to set a timer(TIM16 in this case) and each time timer interrupts, a GPIO pin toggles. I attach the clock configuration.

and here is my tim16 config

htim16.Instance = TIM16;
 htim16.Init.Prescaler = 10;
 htim16.Init.CounterMode = TIM_COUNTERMODE_UP;
 htim16.Init.Period = 44236;
 htim16.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 htim16.Init.RepetitionCounter = 0;
 htim16.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 if (HAL_TIM_Base_Init(&htim16) != HAL_OK)
 {
 Error_Handler();
 }

and this is start of timer

if(HAL_TIM_Base_Start_IT(&htim16)!=HAL_OK)//
	{	
		 Error_Handler();		
	}

and finally timer interrupt and GPIO toggling

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	 if (htim->Instance == TIM16) 
	 {
		 HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_3);
	 }
 }

now funny thing is that as I have set timer to interrupt every 10ms, but when I use oscilloscope, each timer interrupt is shown to be 4ms.

so why is that?

This topic has been closed for replies.
Best answer by Ala

sorry all my fault...

first of all my oscilloscope was not calibrated, so false results is not unexpected...

and second is that

htim16.Init.Prescaler = 10;

so this is crucial, as I forgot that Prescaler is added up by 1 in calculations.

when I wanted for example 10ms or more, this was ok(10 and 10+1 is almost the same)

nut when I wanted 1ms, this made me huge mistakes in measurments, as I expected 1ms but I forgot that in formula we use 1+1=2! which gave me 2times greater results...

anyways guys, thanks for you hints and clues

3 replies

thundertronics.com
Visitor II
January 12, 2022

Output frequency is

f=(44.2368*10^6)/(10+1)/(44236+1) ' frequency in Hz

f = 90.9086799

period is

t=1/f *10^3 ' period in ms

t = 11.000049732

(if prescaler=0, frequency divided by 1, if prescaler=1 then division /2, .)

For ~10ms period prescaler must be set to 9, period to 44235

it does not explain why you observe 4ms though.

Ala
AlaAuthor
Senior
January 12, 2022

yeap, and more curious is this one

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	 if (htim->Instance == TIM16) 
	 {
		 cnt++;
		 if(cnt==1000) 
		 { 
			 cnt=0;
		 HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_3);
		 }
		
	 }
	 
 }

i wish to get a 1sec toggle when I count cnt each time, but same result... why is that?

waclawek.jan
Super User
January 12, 2022

What is "same result"? 4ms between edges?

JW

Ala
AlaAuthorBest answer
Senior
January 12, 2022

sorry all my fault...

first of all my oscilloscope was not calibrated, so false results is not unexpected...

and second is that

htim16.Init.Prescaler = 10;

so this is crucial, as I forgot that Prescaler is added up by 1 in calculations.

when I wanted for example 10ms or more, this was ok(10 and 10+1 is almost the same)

nut when I wanted 1ms, this made me huge mistakes in measurments, as I expected 1ms but I forgot that in formula we use 1+1=2! which gave me 2times greater results...

anyways guys, thanks for you hints and clues