cancel
Showing results for 
Search instead for 
Did you mean: 

Change Period of a timer dynamically

SSchu.4
Associate II

I am experimenting with timers...

I have a list of time values that I want to use to replay a digital signal.

What I try to do is to wait for the interrupt of the base timer, then set a new

period value, then wait for the next interrupt.

But it seems I do something wrong.

Timer init

htim6.Instance = TIM6;
  htim6.Init.Prescaler = 49;
  htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim6.Init.Period = 65530;
  htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }

Timer interupt

 if(htim->Instance == TIM6){
 
	  if(cmd == 1){
		  HAL_GPIO_TogglePin(IR_SND_GPIO_Port, IR_SND_Pin);
                  // tried without stop/start - did not change
		  HAL_TIM_Base_Stop_IT(&htim6);
		  __HAL_TIM_SET_AUTORELOAD(&htim6,vol_up[sq++]);
                  // did not do that - no change
		  __HAL_TIM_SET_COUNTER(&htim6,0);
		  HAL_TIM_Base_Start_IT(&htim6);
		  __NOP();
	  }
  }

This works for the first value, but then I get interrupts at a more or less random pattern.

The values got up and down in my array (between 600 and 1600). I tried with and without Stop/Start and with/without __SETCOUNTER__ - no change as well.

Ideas are highly appreciated

10 REPLIES 10

Ok, I see your point better now. Thats the thing with learning - I have to change my opinion

every second day :)

Thank you for explaining the critique in detail, again, I understand it better now.

Will be considered once I release a library of some sort on my own..

Best regards

Stefan