cancel
Showing results for 
Search instead for 
Did you mean: 

How can I read the state of a pin, set up as a PWM output?

Brian Clough
Associate
Posted on July 31, 2017 at 16:09

 I'm using the PWM function of TIM4, on an STM32F429, to control an RC servo.  This requires a pulse between 0.6 and 2.4 msec, at a rate of about 50Hz.  Everything works fine, if I don't need to change the pulse duration. 

However, I need to change the duration frequently, and when I do, it can cause problems.  If I try to change the value of the Cap/Com register, while the output is high, it cuts the pulse short, causing an erroneous control output to the servo.  20 msec later, the PWM puts out the correct pulse width, and the servo snaps back to its proper location. 

I need to eliminate this 'twitch', by making sure I don't change the pulse width while the pin is high.

I'm using HAL_TIM_PWM_ConfigChannel() to set the new pulse width, and then calling HAL_TIM_PWM_Start() to re-enable the PWM output.

Is there a way to read the state of the timer or the pin, so that I can be sure to call HAL_TIM_PWM_ConfigChannel() when the output is low?

Or is there another way to change the pulse width, without interrupting a pulse in progress?

Brian

#pwm #stm32f4-timer
1 REPLY 1
Posted on July 31, 2017 at 16:13

Look at the autoload/preload type functionality, use the setting that moves the shadow CCR to the active one at the Update event.

The phase can be read via TIM->CNT, the pin level via GPIO->IDR

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..