Skip to main content
Splatt
Associate
February 28, 2022
Question

HAL_Delay not affecting frequency of a Blinking Light

  • February 28, 2022
  • 11 replies
  • 2645 views

I am new to STM32, and I am using a STM32L152c discovery board to run a sample blinking light program. I am using HAL_Delay to control the frequency of the blinking light. However, the frequency does not change when I change the argument to HAL_Delay, or even if I remove HAL_Delay altogether. I would appreciate any assistance. I have included the main function and the HAL_Delay code that was provided in the project setup.

Also, where can I find code documentation? I was trying to find something to help explain HAL_Delay and other functions, but I could not find anything.

int main(void){
 HAL_Init();
 SystemClock_Config();
 MX_GPIO_Init();
 MX_ADC_Init();
 MX_LCD_Init();
 MX_TS_Init();
 while (1){
	 HAL_GPIO_TogglePin(LD3_GPIO_Port, LD3_Pin);
	 HAL_Delay(1000);
 }
}
//This function was sourced from the stm32l1xx_hal.c file generated at project start
__weak void HAL_Delay(uint32_t Delay){
 uint32_t tickstart = HAL_GetTick();
 uint32_t wait = Delay;
 
 /* Add a period to guaranty minimum wait */
 if (wait < HAL_MAX_DELAY)
 {
 wait += (uint32_t)(uwTickFreq);
 }
 
 while((HAL_GetTick() - tickstart) < wait)
 {
 }
}

This topic has been closed for replies.

11 replies

waclawek.jan
Super User
February 28, 2022

Are you sure you are compiling/downloading this program?

JW

Splatt
SplattAuthor
Associate
February 28, 2022

Yes. If I change which light is blinking, the program does respond.

KnarfB
Super User
February 28, 2022

HAL_Delay takes the delay in ms. If you remove that line, the LED will toggle at very high speed and you won't see it blinking. Chances are that your compilation/linking/downloading of the new prog. failed and the original prog. is still executed.

The HAL docs are generated from the source code, so there is no much new information in it. You can learn it best from examples and tutorials and by debug-stepping into the HAL code. There are many videos around, perhaps not so many for L1 but other series.

hth

KnarfB

Splatt
SplattAuthor
Associate
February 28, 2022

Removing the line or changing the time delay does not change the frequency of the blinks, and the blinking is visible (looks about like a 100ms delay) whether or not the HAL_Delay is included.

TDK
February 28, 2022

By far the most likely explanation is that you're not running what you think you're running. Removing the delay should absolutely have an effect. If it does not, then the problem isn't with HAL_Delay anymore, since it's no longer being run.

HAL docs:

https://www.st.com/resource/en/user_manual/um1816-description-of-stm32l1-hal-and-lowlayer-drivers-stmicroelectronics.pdf

Load up STM32CubeProgrammer and verify the flash changes when you recompile and flash the program. Debug and step through the main loop.

"If you feel a post has answered your question, please click ""Accept as Solution""."
S.Ma
Principal
March 1, 2022

Go debug mode, put breakpoints in the code, step by step, and find the bug.

waclawek.jan
Super User
March 1, 2022

> If I change which light is blinking, the program does respond.

> the blinking is visible (looks about like a 100ms delay) whether or not the HAL_Delay is included.

Maybe the bliking is not caused by the program itself, but e.g. by watchdog.

Review the option bits setting (namely Hardware watchdog), using e.g. CubeProgrammer.

Do you have anything connected to the Disco board?

JW

Amel NASRI
ST Technical Moderator
March 2, 2022

Hi @Splatt​ ,

Try to follow the steps as described in the tutorial Blinking LED with STM32CubeMX and HAL.

For sure, you need to select your own hardware at the beginning.

-Amel

To give better visibility on the answered topics, please click on "Best Answer" on the reply which solved your issue or answered your question.
Splatt
SplattAuthor
Associate
March 2, 2022

I have gone through this tutorial, and this is exactly how I have done it, except I am using the CubeMX embedded inside the STM32CubeIDE, and I cannot find the button that lets you generate the code. Other than that, I did not have to change any of the IDE setting defaults working through the example. The light is still not changing frequency.

Amel NASRI
ST Technical Moderator
March 3, 2022

Hi @Splatt​ ,

To generate code, you need to click on Project > Generate Code.

-Amel

To give better visibility on the answered topics, please click on "Best Answer" on the reply which solved your issue or answered your question.
Splatt
SplattAuthor
Associate
March 25, 2022

None of the above methods have worked for some reason. I ended up trying to load the example blink program for the stm32l152c board, and it worked if I did it a very specific way:

  1. Create a new STM32 project for my board (Default ioc setup)
  2. Replace the Src and Inc folders with the folders from the example
  3. Occasionally, a file would need to be added that was not included. For instance, the stm32l152c_discovery.h and .c files were missing, so I got those from GitHub. Would this indicate an incorrect ioc setup?

This way works to load up sample projects, but it ultimately does not answer the question this discussion is about. A second question I would ask is: is there a better way to load a sample program into STM32cubeIDE? I tried converting from one of the included toolchains (SW4STM32), but this resulted in files missing.