I need to enable a general timer interrupt after generating each pulse in PWM mode. I wrote this program. I will be grateful if you kindly let me know how to enable timer interrupt. This my SPL schematic code:
void PWMControl(unsigned int Freq){
TIM_TimeBaseInitTypeDef TimeStructureure;
TIM_OCInitTypeDef TIMOCStructureure;
TimeStructure.TIM_Prescaler=Prescale;
TimeStructure.TIM_CounterMode=TIM_CounterMode_Up;
TimeStructure.TIM_Period=Period;
TimeStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TimeStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM4, &TimeStructure);
TIM_Cmd(TIM4, ENABLE);
TIMOCStructure.TIM_OCMode=TIM_OCMode_PWM2;
TIMOCStructure.TIM_OutputState=TIM_OutputState_Enable;
TIMOCStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
TIMOCStructure.TIM_Pulse=Pulse;
TIM_OC1Init(TIM4, &TIMOCStructure);
//TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
}
According to this post I need to generate N pulse without/ the least CPU workload. (Max Frequency=1MHz)
-
\$\begingroup\$ Do you have a code for the interrupt service routine? \$\endgroup\$Tagli– Tagli2021年01月17日 19:39:51 +00:00Commented Jan 17, 2021 at 19:39
-
\$\begingroup\$ What library are you using? This does not look like the ST HAL drivers. \$\endgroup\$A.R.C.– A.R.C.2021年01月18日 08:43:55 +00:00Commented Jan 18, 2021 at 8:43
-
\$\begingroup\$ Thank you, I have updated the post content. \$\endgroup\$John Jin– John Jin2021年01月19日 03:57:20 +00:00Commented Jan 19, 2021 at 3:57
-
\$\begingroup\$ I have added the interrupt code but it doesn't work. It just generate a continuous PWM wave form. \$\endgroup\$John Jin– John Jin2021年01月20日 08:33:23 +00:00Commented Jan 20, 2021 at 8:33
-
\$\begingroup\$ 1 MHz is a high frequency for interrupts. You need to use method 3 from the linked question. You need to chain 2 timers, and enable interrupts in the slave timer so that it can stop the master timer. \$\endgroup\$Tagli– Tagli2021年01月20日 09:29:48 +00:00Commented Jan 20, 2021 at 9:29
1 Answer 1
Are you using CubeMX?
If yes then you have to first enable it in NVIC after configuring TIM4.
Next, inside your logic you have to enable the timer as HAL_TIM_IC_Start_IT(&htim4, TIM_CHANNEL_1);