cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_Delay not affecting frequency of a Blinking Light

Splatt
Associate II

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)
  {
  }
}

11 REPLIES 11

Are you sure you are compiling/downloading this program?

JW

KnarfB
Principal III

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

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

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
Guru

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

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

> 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 Employee

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

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.