2021-04-04 11:58 AM
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.
Solved! Go to Solution.
2021-04-04 01:07 PM
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.
2021-04-04 01:07 PM
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.
2021-04-05 05:04 AM
Thank you for your reply. Since I am new to this, I didn't know that such optimizations are done.