Prepare for a nooby question.
I'm writing some ultra-simple code for this new PIC which I've just got. All I'm trying to do is to flash an LED. Below are two code samples - the first works but the second doesn't. Why?? I can't see any problem with the second one.
WORKS:
while(1)
{
i=99999;
while(i--) {
LATAbits.LATA0 = 0; // set RA0 to logic 1
}
i=99999;
while(i--) {
LATAbits.LATA0 = 1; // set RA0 to logic 0
}
}
DOESN'T WORK:
while(1)
{
LATAbits.LATA0 = 1; // set RA0 to logic 1
for(i=0;i<99999;i++) {}
LATAbits.LATA0 = 0; // set RA0 to logic 0
for(i=0;i<99999;i++) {}
}
Thanks in advance for the help!
Try this:
while(1)
{
for(i=0;i<99999;i++)
{
LATAbits.LATA0 = 1; // set RA0 to logic 1
}
for(i=0;i<99999;i++)
{
LATAbits.LATA0 = 0; // set RA0 to logic 0
}
}
Maybe your compiler is optimizing and ignoring the for
statements as they have no code to execute inside. What I did was put the RA0 assignment inside these statements, forcing the compiler to keep the delay loops.
You can also pass the argument -S
to your compiler to see the assembly code generated and confirm that the for
statements were removed. The -S
option generates an intermediate file with a ".S" extension.