Skip to main content
astaria
Associate
May 27, 2019
Solved

LED blinks only in debug mode

  • May 27, 2019
  • 3 replies
  • 1709 views

Hi!

I have a basic code - blinking LED code which uses onboard LED (PC13). The problem is that it doesn't toggle at all, it just stay switched on and doesn't go off after delay. I've tested that code in debug mode and everything worked fine, it toggled, but it doesn't work out of debug. I can't get why it works only during debugging and how to fix that.

Board - STM32F103C8T6

Utility is STM32 ST-LINK Utility v4.4.0

Driver is STSW-LINK009

Programmer st-link v2

Code:

------------------------------------------------------------------------------------------

#include "stm32f10x.h"

#include "stm32f10x_rcc.h"

#include "stm32f10x_gpio.h"

GPIO_InitTypeDef GPIO_InitStruct;

void delay(int a);

int main(void)

{

 // Enable clock for GPIOC

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

 // Configure PC13

 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;

 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;

 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;

 GPIO_Init(GPIOC, &GPIO_InitStruct);

 while (1)

 {

 // Turn on LED on PC13 

 GPIO_ResetBits(GPIOC, GPIO_Pin_13);

 delay(5000);

 // Turn off LED on PC13

 GPIO_SetBits(GPIOC, GPIO_Pin_13);

 delay(5000);

 }

}

void delay (int a)

{

 volatile int i,j;

 for (i=0 ; i < a ; i++)

 {

 j++;

 }

 return;

}

----------------------------------------------------------------------------------------------------

Waiting for your advice a lot ;)

This topic has been closed for replies.
Best answer by turboscrew

72 MHz means about 14 ns per clock tick. The processor executes most instructions in 1 clock cycle.

The compiler probably optimizes that loop to roughly 3 or 4 instructions.

that means about 55 ns per loop. So 5000 loops should take roughly 300 us.

You should not see it blink, but you should see it glow.

3 replies

Tesla DeLorean
Guru
May 27, 2019

At several million cycles, 5000 ain't much. Push out the number, and perhaps scope so you can see a square wave.

Perhaps use SysTick.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
turboscrew
turboscrewBest answer
Senior III
May 28, 2019

72 MHz means about 14 ns per clock tick. The processor executes most instructions in 1 clock cycle.

The compiler probably optimizes that loop to roughly 3 or 4 instructions.

that means about 55 ns per loop. So 5000 loops should take roughly 300 us.

You should not see it blink, but you should see it glow.

astaria
astariaAuthor
Associate
May 29, 2019

yeah, i've set it up to 5000000 and it finally started to be noticeable. Thank you!