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)

\$\endgroup\$
8
  • \$\begingroup\$ Do you have a code for the interrupt service routine? \$\endgroup\$
    – Tagli
    Commented 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.
    Commented Jan 18, 2021 at 8:43
  • \$\begingroup\$ Thank you, I have updated the post content. \$\endgroup\$
    – John Jin
    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\$
    – John Jin
    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\$
    – Tagli
    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);

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.