Skip to main content
Associate II
October 25, 2023
Solved

Square Wave Generation on STM32F407

Hi,

I am a beginner and I want to generate a 50Hz square wave on STM32F407 on Keil UVision4
Please mention libraries etc that are being used and if you can how to add them to Keil UVision so it doesn't give an error.

 

This topic has been closed for replies.
mƎALLEm
mƎALLEmBest answer
ST Technical Moderator
November 15, 2023

Hi @Zaidii1122 ,

As you are a beginner and if you don't want to use a timer but just GPIOs, you can simply toggle the pin you want at 20ms (1/50Hz) using: HAL_Delay(20). But this in not the way we recommend a such usage.

This is an example jhow to toggle the pin 5 on GPIOA:

/* -1- Enable GPIOA Clock (to be able to program the configuration registers) */
__HAL_RCC_GPIOA_CLK_ENABLE();

/* -2- Configure PA05 IO in output push-pull mode to
drive external LED */
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* -3- Toggle PA05 IO in an infinite loop */
while (1)
{
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);

/* Insert delay 20 ms */
HAL_Delay(20);
}
}

 

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.

8 replies

TDK
October 25, 2023

Use the Example Selector in CubeMX to generate a PWM example project.

TDK_0-1698241587749.png

 

"If you feel a post has answered your question, please click ""Accept as Solution""."
Associate II
November 7, 2023

Hi , Thanks for this but i have to complete this on Keil Uvison 4 Software

mƎALLEm
ST Technical Moderator
November 7, 2023

Hello,

If you can't use CubeMX tool for code configuration/generation, you can also refer and inspire from the example provided in STM32CubeF4 under the path STM324xG_EVAL\Examples\TIM\TIM_PWMOutput

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
mƎALLEm
mƎALLEmBest answer
ST Technical Moderator
November 15, 2023

Hi @Zaidii1122 ,

As you are a beginner and if you don't want to use a timer but just GPIOs, you can simply toggle the pin you want at 20ms (1/50Hz) using: HAL_Delay(20). But this in not the way we recommend a such usage.

This is an example jhow to toggle the pin 5 on GPIOA:

/* -1- Enable GPIOA Clock (to be able to program the configuration registers) */
__HAL_RCC_GPIOA_CLK_ENABLE();

/* -2- Configure PA05 IO in output push-pull mode to
drive external LED */
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* -3- Toggle PA05 IO in an infinite loop */
while (1)
{
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);

/* Insert delay 20 ms */
HAL_Delay(20);
}
}

 

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.