0
votes

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

1
What is the value of uwTickFreq, of tickstart and of uwTick each time through?Tom V
The code you have posted is clearly not where the error is. You have been too aggressive in the fragments you have shown. Showing good code fragments and asking why they don't work will get you guessed not answers.Clifford
@TomV I found the following line of code designed in my Keil project. HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT; /* 1KHz */dharmik
I know that line, but can you use the debugger to examine the contents of the variables?Tom V
@TomV I have debugged few things see the attached waveform image I got from STstudio. I don't think the value of variable a should reset once it reaches around 300 right?dharmik

1 Answers

0
votes

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.