1
\$\begingroup\$

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)

asked Jan 16, 2021 at 19:57
\$\endgroup\$
8
  • \$\begingroup\$ Do you have a code for the interrupt service routine? \$\endgroup\$ Commented Jan 17, 2021 at 19:39
  • \$\begingroup\$ What library are you using? This does not look like the ST HAL drivers. \$\endgroup\$ Commented Jan 18, 2021 at 8:43
  • \$\begingroup\$ Thank you, I have updated the post content. \$\endgroup\$ Commented 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\$ Commented 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\$ Commented Jan 20, 2021 at 9:29

1 Answer 1

1
\$\begingroup\$

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);

answered Jan 20, 2021 at 9:03
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.