cancel
Showing results for 
Search instead for 
Did you mean: 

Timer interrupt and GPIO toggling

Ala
Senior

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Ala
Senior

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

View solution in original post

4 REPLIES 4

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.

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?

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

JW

Ala
Senior

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