cancel
Showing results for 
Search instead for 
Did you mean: 

LED blinks only in debug mode

astaria
Associate II

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 😉

1 ACCEPTED SOLUTION

Accepted Solutions
turboscrew
Senior III

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.

View solution in original post

3 REPLIES 3

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
Up vote any posts that you find helpful, it shows what's working..
turboscrew
Senior III

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.

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