Skip to main content
KRaja.1
Associate
Apr 4, 2021 6:58 PM UTC
Solved

Blinking LED

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.

This topic has been closed for replies.
Tesla DeLorean
Tesla DeLoreanBest answer
Guru
Apr 4, 2021 8:07 PM UTC

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..

2 replies

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
Apr 4, 2021 8:07 PM UTC

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
KRaja.1
KRaja.1Author
Associate
Apr 5, 2021 12:04 PM UTC

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