I hate you infinite loops

Status
Not open for further replies.

Makaram

Member
Hey,
writing my first c program for pic 16f84a

got everything working good, heres my code. only one problem... my code loops infinitely regardless of for, when or if loops?!?!?!?!
Using hi-tech

heres my code


#include <pic.h> // pic specific identifiers
# define _XTAL_FREQ 20000000
__CONFIG(0x3ff2); // Config bits

void main(void) // program entry
{
int i = 0;
TRISA = 0x0; // Both ports
TRISB = 0x0; // as outputs
while(i<10) // Loop forever
{
PORTB = 0xff; // All on!!
PORTA = 0xff;
__delay_ms(180); // Wait a while
PORTB = 0x0; // All off!!
PORTA = 0x0;
__delay_ms(180); // Wait a while
i = i+1;
}
} // End!!



and I've tried


#include <pic.h> // pic specific identifiers
# define _XTAL_FREQ 20000000
__CONFIG(0x3ff2); // Config bits

void main(void) // program entry
{
int i
TRISA = 0x0; // Both ports
TRISB = 0x0; // as outputs
for(i=0; i<10; i++) // Loop forever
{
PORTB = 0xff; // All on!!
PORTA = 0xff;
__delay_ms(180); // Wait a while
PORTB = 0x0; // All off!!
PORTA = 0x0;
__delay_ms(180); // Wait a while

}
} // End!!

my LED constantly flashes infinitely. If i change the delays the LED blink time changes as it should but it still continues for ever. this is driving me insane X_X

Please help
thanks,
Matt
 
The reason you need to add the while(1); is that the micro controller finishes your program and starts over. The while one keeps it looping at that statement. The while(1); is an infinite loop!. In short it was the lack of an infinite loop that caused your problem. Strange huh.
 
Yes, your 'void main(void){}' is an infinite loop in and of itself. You need to put another loop inside of it so that it never gets back to the beginning of "main". That is why a 'while(1)' loop at the end is necessary--it never lets the processor go back to the beginning, hence it does not flash the LED again.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…