cancel
Showing results for 
Search instead for 
Did you mean: 

Pwm mode stm32

AKh.1
Associate II

How can I use PWM output pulse in my program?

Normally the PWM mode for instance for TIM1 will be generated on pin PA8, but I want to use the PWM signal internally in my code. How is it applicable?

7 REPLIES 7
TDK
Guru

There are hundreds of STM32 chips. Which one?

You could use the timer period elapsed interrupt to do something within the code at a specified duration. In HAL, this can be done by implementing the HAL_TIM_PeriodElapsedCallback function.

If you feel a post has answered your question, please click "Accept as Solution".
AKh.1
Associate II

Thank you so much for your reply.

Actually, I want to make a pulse with 250 ms high and 4s low. I made this pulse by using PWM but I dont know how to apply this "on and off" action on a variable and see it's effect on a specific GPIO instead of the default PWM output pin.

would be thankfuI​ if you share your ideas.

Piranha
Chief II

You cannot output timer signal on any pin, only the dedicated ones, of which there can be several options for a single channel. But if you want to use the generated times in software, then enable the update and/or compare interrupts in TIM_DIER register. And of course implement the corresponding ISR.

Hello

You can use the Update flag and Capture Compare Interrupt flag to use it internally to your code.

You can use also the interrupts for above flagσ

HAL contains the apropriate functions , macros and callbacks to do your job.

Thank you vangelis.

Is it possible for you to write an example code using what you mentioned, pleases?

Thanks a million;

The internet is literary full of such examples.

Hello again.

Assuming that you use STM32CubeMX ,

1. Configure the CubeMX to produce initialization code for PWM generation (eg mode1), on Channel 1.

2 Define void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) calback function. It will be called at every PWM cycle.

3.Define void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) callback function, it will be called at every end of pulse event.

Inside your main function

4 __HAL_TIM_ENABLE_IT(&htim,TIM_IT_UPDATE);// update event interrupt enable

5 __HAL_TIM_ENABLE_IT(&htim,TIM_IT_CC1); // capture compare 1 event interrupt enable

6 HAL_TIM_PWM_Start(htim, TIM_CHANNEL_1);// start PWM generation

5,6 steps can be replaced by HAL_TIM_PWM_Start_IT(htim, TIM_CHANNEL_1);// CC 1 interrupt enable and start PWM