View previous topic :: View next topic |
Author |
Message |
flightdecksoft
Joined: 09 Feb 2007 Posts: 2 Location: San Diego
|
While / For Loops Problem |
Posted: Fri Feb 09, 2007 11:16 am |
|
|
Hello,
PIC16F84A
I have written a simple code like this to flash an LED:
void main()
{
while (TRUE)
{
loop();
}
}
int loop(void)
{
OUTPUT_HIGH(PIN_B4);
delay_ms(1000);
OUTPUT_LOW(PIN_B4);
return 0;
}
Now when I compile, I get a warning about the while loop always being true. But when I run the program the LED does not blink. If I remove the while loop, the LED will turn on, then off after one second as expected. Does anyone know why the while loop is not working?
It seems like the delay_ms function is kicking it out of the loop function and back to main, then repeating.
Frustrated,
Jason |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Feb 09, 2007 11:41 am |
|
|
Your led goes off, but only so short that your eyes don't see it. Add a delay between switching off and on again.
Funny to see that so many people make the same mistake, it must be an error in the human brain. This same question has been on this forum for at least 5 times the last couple of years. Here is a link to a similar question: http://www.ccsinfo.com/forum/viewtopic.php?t=28516 |
|
|
flightdecksoft
Joined: 09 Feb 2007 Posts: 2 Location: San Diego
|
|
Posted: Fri Feb 09, 2007 11:53 am |
|
|
WOW! Stupid.
Thanks! |
|
|
vsmguy
Joined: 13 Jan 2007 Posts: 91
|
|
Posted: Sat Feb 10, 2007 12:11 pm |
|
|
This is a common mistake in iterative programs.... specially when the person has not been close to hardware recently
I did the same mistake the first time ;) |
|
|
|