I would like to determine in software if the PWM output signal is currently high or low.
As far as I know GPIO_ReadOutputDataBit(GPIOD, GPIO_Pin_12) does not work with a GPIO pin set up as an alternate function (AF).
-
\$\begingroup\$ Some microcontrollers have a "port state" register that can give the actual voltage level (active/inactive) of a port, regardless of if it is configured as output or input. If such registers can be used when a peripheral hardware is using the same port also depends on the specific MCU. So I would suggest to read the chapter describing the gpio hardware in the manual. \$\endgroup\$Lundin– Lundin2016年10月14日 13:45:07 +00:00Commented Oct 14, 2016 at 13:45
2 Answers 2
You should read the applicable GPIO input data register, not the output one. Only when the pin is configured as a GPIO output would they match.
The programmer's manual will explain what the actual register is; the documentation for whatever IO or HAL library you are using will show how to read it at a higher level.
It is also likely that you should be able to read the relevant timer's count register and compare it to the applicable output compare value.
I can't find a place in the timer register structure to directly read OCxREF or OCx, but you may have luck with this:
Set an interrupt on the update and reset timer events and set a flag manually that way.
Although the output state may be reflected in the GPIO input state as Chris Stratton mentions as well.