cembeddedstm32microcontrollerstm32cubemx

STM32F103C8 Bluepill board HAL_delay() problem


I am using STM32 cubeMX for configuration and Keil for programming. Have set onboard led pin pc13 pin as an output pin and default in push-pull mode. Set debugger to the serial wire as I am using ST-link V2 as a debugger. RCC set HSE to crystal/ceramic resonator. and clock configuration set to default and generated project. enter image description here

enter image description here

enter image description here

Now I started with a simple LED blink program. As below

    HAL_GPIO_TogglePin(led_GPIO_Port,led_Pin);
    HAL_Delay(1000);

build successfully with no error and uploaded and wonder my led was not blinking and shocked as I have done this before and now this is not working. when I debugged step by step and my code was just going from two functions repeatedly.

  while ((HAL_GetTick() - tickstart) < wait)
  {
  }
__weak uint32_t HAL_GetTick(void)
{
  return uwTick;
}

Nothing happens more in this code I know the code is right but there is some error in the HAL_delay configuration. After scratching my head for a day I tried uploading the following code

    HAL_GPIO_TogglePin(led_GPIO_Port,led_Pin);
    HAL_Delay(100);

And strange thing is that now my led is blinking only I have change the HAL_dealy value from 1000 to 100 and it works fine but, when using 1000 does not work at all. So for testing, I gradually increased the delay value and I find that more than HAL_delay(400) it does not work.

Not able to find cause for this Any help will be appreciable.

As suggested by Tom I debugged uwTickFreq using STstudio. and I got the following output waveform. enter image description here

After that, I also uploaded the following code. And defined a variable as "unsigned long int a;"

    HAL_GPIO_TogglePin(led_GPIO_Port,led_Pin);
    HAL_Delay(100);
    a= HAL_GetTick();

Now I debugged the value of a using STstudio. And strange the value of a becomes 0 once it reached around 300. enter image description here


Solution

  • It seems like finally, I got the problem when I noticed the reset problem in the controller I searched around and find something here.

    So I checked my optional bytes set in MCU with the STM32 cube programmer. It was set as below.

    enter image description here

    Therefore I enabled these three optional bytes.

    enter image description here

    And the problem of reset was gone and I am now able to use the HAL_delay function properly and now the value of HAL_GetTick() is also increasing more than 300.

    Still have one dought I think watchdog was causing reset but why it only cause that when I use the timing function.