2015-08-07 12:58 AM
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; }2015-08-07 04:55 AM
I changed the function for starting the timer for HAL_TIM_Base_Start_IT(&htim3);
and now it's counting in one pace but still too fast, I had to multiply my prescaler 4 times to receive a frequency I wanted.2015-08-07 06:22 AM
The maths not complicated here.
1Hz would require 72,000,000 ticks (at 72 MHz) 100Hz would require 720000 720000 = 72 * 10000 htim3.Init.Prescaler = 72-1; htim3.Init.Period = 10000-1; I'd probably use the Update