cancel
Showing results for 
Search instead for 
Did you mean: 

Square Wave Generation on STM32F407

Zaidii1122
Associate II

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

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 on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

8 REPLIES 8
TDK
Guru

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".

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

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 on "Accept as Solution" on the reply which solved your issue or answered your question.

I have to use the Board STM32F407VGT6 and the example offers  to measure an external signal while I want to output a square wave on any of the output pins of the board without any external signal. Could you please guide how to do that

That's a CHIP, whats the make/model of BOARD? Are you talking about the STM32F4-DISCOVERY board?

Can you use the old SPL library with uV4 ?

https://www.st.com/en/embedded-software/stsw-stm32068.html

STM32F4-Discovery_FW_V1.1.0\Project\Peripheral_Examples\TIM_PWM_Output\main.c

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

I will try this rn and get back to you Thank you sm

Plus do have any videos or resources that can help me in learning how to use stm32cube ide to accomplish the task or can you explain it 

 

I'm from a generation that reads the manual.

https://legacy.cs.indiana.edu/~geobrown/book.pdf

https://github.com/nalepae/stm32_tutorial

https://github.com/RobGah/Discovering_STM32

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

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 on "Accept as Solution" on the reply which solved your issue or answered your question.