cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407 Using GPIO to generate 5V PWM

LPetr.1
Senior

I am working on the STM32F407 and we made some bad choises when selecting a motor. The motor control signal is 5V hence we need to generate 5V PWM signal using the STM32F407. Sadly, we have wired the GPIO from the STM32F407 directly to the motor control pin.

 

I am aware about the 3.3V - 5V logic level converters but I was wondering whether it is possible to achieve what I want using just the STM32F407.

 

 

I know that some pins are 5V tolerant. For example, PD15 pin on the STM32F047 is 5V tolerant as it is marked with FT:

LPetr1_0-1707223802637.png

 

1. I have configured this GPIO as Output Open Drain:

LPetr1_0-1707224113013.png

 

2. I have soldered 30k OHM resistor from the GPIO8 to 5V

 

And simply tried to set the ping to high using the following command:

 

 

	HAL_GPIO_WritePin(GPIO8_GPIO_Port, GPIO8_Pin, 1);
	HAL_Delay(1000);
	HAL_GPIO_WritePin(GPIO8_GPIO_Port, GPIO8_Pin, 0);

 

 

 

I can confirm that the GPIO is toggling between 0 and 5V:

LPetr1_0-1707225389413.png

 

 

Next, I try to configure PWM for this pin:

 

1. Enable pin stacking for this pin since I need this pin to be GPIO_Output (to configure Open drain mode) and I need to configure TIM_CH4:

LPetr1_1-1707225618823.png

LPetr1_2-1707225720025.png

 

 

With this configuration, I can confirm that the PWM is working as expected but the voltage only goes up to 3.3V. Could it be that when setting a pin to TIM4, it disables the open drain mode?

 

In my main.c I simply do:

	TIM4->CCR4 = 6000;
	HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4);

 

 

LPetr1_3-1707225788006.png

 

 

 

 

 

 

 

4 REPLIES 4
LPetr.1
Senior

UPDATE:

I have discovered that when setting the pin to TIM4_CH4 while pin stacking is enabled automatically disables GPIO_Output and vice versa.

 

Perhaps these two are not compatible with each other and not possible to use both at once? Is there any workarround to generate 5V pwm?

Well, just stop clicking in CubeMX, and set up the pin in GPIO yourself.

You want to set it to Open Drain in GPIOx_OTYPER, while set to AF in GPIOx_MODER and the proper AF number selected in GPIOx_AFR[].

There may be some way to coax Cube and/or CubeMX into doing this; I don't use them.

Note, that the rising edge's slewrate is dependent on the parasitic capacitances of connected pins and track, and the pullup resistor.

JW

TDK
Guru

A pin can't be TIM4_CH4 and a GPIO output at the same time. How would that work?

You can set it to be open-drain and TIM4_CH4 if that's what you're after. Set it up as TIM4_CH4 and change the mode to AF_OD in the GPIO tab.

TDK_0-1707228031636.png

 

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

You are totally right! I was under the impression that the Gpio mode open drain setting is only available if the pin is configured as GPIO Output, but that is definately not the case.

 

The pin can be configured as TIM4_CH4 and still configured the Gpio mode as you have suggested.

 

I can confirm that by setting the mode to alternate function open drain I can generate 5V pwm