AnsweredAssumed Answered

STM32F3 timer3 ch1 interrupt

Question asked by oziom.dominik on Aug 7, 2015
Latest reply on Aug 7, 2015 by Clive One
Hello,
I'm trying to set my timer3 on ch1 to make an interrupt with some frequency (100Hz). The idea is to switch it on and off by using a USER button situated on my stm32f3 board. I programmed it but it sometimes count way too fast than it's configured, I mean I placed a variable counter which is increasing after every interrupt from TIM3. Sometimes when I switch this timer for 1 sec it counts 100 impulses but sometimes 500 or even 1000. Can anyone tell me where am I making mistakes?

TIM3CH1 configuration:

void MX_TIM3_Init(void)
{

  TIM_MasterConfigTypeDef sMasterConfig;
  TIM_OC_InitTypeDef sConfigOC;

  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 2;
  htim3.Init.CounterMode = TIM_COUNTERMODE_DOWN;
  htim3.Init.Period = 65535;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  HAL_TIM_OC_Init(&htim3);

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);

  sConfigOC.OCMode = TIM_OCMODE_TIMING;
  sConfigOC.Pulse = 0;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  HAL_TIM_OC_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1);

}

and a part where I turn the timer on and off:

int tim_trigger=0;

while(1){
      button1=HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
      if(button1==GPIO_PIN_SET){                               
          if(tim_trigger==1) tim_trigger=0;
          else tim_trigger=1;
      }

      if(tim_trigger==1){
          HAL_TIM_OC_Start_IT(&htim3, TIM_CHANNEL_1);      // start timer
      }
      else{
          HAL_TIM_OC_Stop_IT(&htim3, TIM_CHANNEL_1);       // stop timer
      }
.
.
.
}



and of course timer interrupt function:

void TIM3_IRQHandler(void)
{
  HAL_TIM_IRQHandler(&htim3);
  licznik=licznik+1;  
}

Outcomes