Skip to main content
NNar
Associate II
April 5, 2019
Question

HAL_Delay works and doesn't work

  • April 5, 2019
  • 3 replies
  • 2263 views

I'm still very new to STM32 processors, and using CubeMX to generate my basic code structure (for TrueStudio). In the code below, if I uncomment code section A, the yellow LED blinks, but not evenly...

  • At low values such HAL_Delay(100), the high time is always 100 or 101ms, but the low time is sometimes 100ms and sometimes 87ms.
  • At higher values, such as HAL_Delay(500), the high time is consistently 390ms and low time is less than 1ms.
  • Using HAL_Delay(1000) instead gives me the same results as HAL_Delay(500);

What am I missing here?

Now, if I comment out section A, so that section B runs, the red LED comes on, but then the code hangs at the HAL_Delay(1000) so the yellow LED never comes on. If I drop that to HAL_Delay(100), the red LED comes on again, but the yellow LED blinks at approx 2.5Hz. There are no other timers or interrupts running.

I don't know what to check next. If this is somehow expected behaviour, which doc explains how this is supposed to work?

Thanks

/* USER CODE BEGIN 2 */
 
// HAL_TIM_Base_Start_IT(&htim17);
 
 // Code section A...
 while (1)
 {
	 HAL_GPIO_TogglePin(IENC1_GPIO_Port, IENC1_Pin);		// Toggle yellow LED
	 HAL_Delay(500);
 }
 
 // Code section B...
 HAL_GPIO_WritePin(IENC0_GPIO_Port, IENC0_Pin, GPIO_PIN_SET);		// Red LED on
 HAL_Delay(1000);
 HAL_GPIO_WritePin(IENC1_GPIO_Port, IENC1_Pin, GPIO_PIN_SET);		// Yellow LED on
 while(1);
 
 ...

This topic has been closed for replies.

3 replies

Tesla DeLorean
Guru
April 5, 2019

HAL_Delay typically depends on the SysTick interrupt, that it fires and is configured correctly. Usually via HAL_Init()

I'd suspect the tool chain or generated code.

Review what else is on the pin.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal
April 5, 2019

If you use HAL_Delay inside an interrupt routine, havok behaviour may be seen. Check your ISRs and callbacks if any outside the shared code extract.

NNar
NNarAuthor
Associate II
April 5, 2019

I have no interrupts set. See my response below to Clive about my configuration.

Thanks.

NNar
NNarAuthor
Associate II
April 5, 2019

Thanks Clive. I completely re-created the project in CubeMX in a new directory, with a new name. All I have set is SWD enabled and 2 outputs for the LEDs. And I set the FCLK to 48MHz. Image below shows what I have. That's all. No other timers or peripherals enabled. Same exact behaviour.

I'm going to see if I can download an older version of CubeF0 to see if that makes a difference.