cancel
Showing results for 
Search instead for 
Did you mean: 

Blinking LED

KRaja.1
Associate

Hi all,

Just started with bare metal programming of stm32f411re.

Uploaded the following code to blink the onboard led of the nucleo board.

Code:

#include "stm32f4xx.h"         // Device header

void delayMs(int delay);

int main (void)

{

RCC->AHB1ENR |= 1;

GPIOA->MODER |= 0x400;

while(1)

{

GPIOA->ODR = 0x20;

delayMs(100);

GPIOA->ODR &= ~0x20;

delayMs(100);

}

}

void delayMs(int delay)

{ int i ;

for(; delay>0; delay--)

{

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

}

}

The above code had my led lit but it was very dim. While running the debugger, I found out that my code skips the delay function call.

So, I tried including a for loop as a delay directly in the while loop instead of having a separate function, but still it bypasses the statement and executes the next.

What change should be done here to get the code working?

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions

Software delay loops tend to get optimized away.

Try defining i as a volatile int, or using an actual timer/counter for the clock source.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..

View solution in original post

2 REPLIES 2

Software delay loops tend to get optimized away.

Try defining i as a volatile int, or using an actual timer/counter for the clock source.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
KRaja.1
Associate

Thank you for your reply. Since I am new to this, I didn't know that such optimizations are done.