Skip to main content
KRaja.1
Associate
April 4, 2021
Solved

Blinking LED

  • April 4, 2021
  • 2 replies
  • 1387 views

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.
Best answer by Tesla DeLorean

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.

2 replies

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
April 4, 2021

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 VenmoUp vote any posts that you find helpful, it shows what's working..
KRaja.1
KRaja.1Author
Associate
April 5, 2021

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