EndingCPrograms

Thread Starter

mpuvdd

Joined Feb 11, 2007
50
Hello,
I'm new to the C programing language. Currently, I'm using the MPLAB IDE. During simulation of a simple program (shown below), the simulator kept repeating the program, and never actually stopped running. Would a
PIC16F684 also never stop?


Here's the code:

int i, j;

#define E RC4
#define RS RC5

const char Command[] = "0b00000001";
const char Text[] = "0x53, 0x65, 0x74, 0x68";

main()
{
PORTC = 0; // All ports = low state.
CMCON0 = 7; // Turn off Comparators.
ANSEL = 0; // Turn off ADC.
TRISC = 0; // PORTC = Outputs.

for (i = 0; i < 1; i++)
{

PORTC = 0b0010;
RS = 1;
E = 1; E = 0;

}
} // End cLCD2.c
 

CVMichael

Joined Aug 3, 2007
419
OK... I'm a beginner in this too, I never actually made a functional PIC program yet, but I am a good windows programmer...

Anyways... I always thought that a CPU (or PIC) never stops processing as long as the computer/PIC is ON.

From what I read on PIC books, I think that once the program ends, the chip resets, then the program starts again, and so on...

To make it "stop", is to basically make an infinite loop that does nothing (no output/input)

PS. What compiler do you use ?
 

hgmjr

Joined Jan 28, 2005
9,027
Most microcontroller programs are written using what is commonly referred to as an infinite loop.

Two common ways to set up an infinite loop using C-language are:

Rich (BB code):
for(;;)
{
 
/* Your code is inserted inside curly brackets */
 
}
also

Rich (BB code):
While(1)
{
 
/* Your code is inserted inside curly brackets */
 
 
}
The code you insert between the curly brackets will be executed over and over as long as power is applied to the microcontroller.

hgmjr
 

mrmeval

Joined Jun 30, 2006
833
Same loop deal for the atmega168. The main part is a loop. To 'stop' the controller just set it to infinitely loop. In the atmega there are interrupts so you could activate one of them and then put the chip to sleep and it will never wake unless the interrupt triggers. Just make sure the interrupt won't trigger. I think in that mode it does 15 microamps.
 

Thread Starter

mpuvdd

Joined Feb 11, 2007
50
Alright!!! I'm using PICC Lite compiler. Well, that pretty much sums it up. That's what I thought but I wasn't sure.
Thanks for all the help,
mpuvdd
 

nanovate

Joined May 7, 2007
666
Just to add a comment:

You should always have some type of infinite loop even it is a "while(1);" at the end. Otherwise the program counter will just continue past the main function and execute random code until it comes the end and wraps around -- like a reset.

As far as using sleep it is a good suggestion but would wrap it into a infinite loop since IF it were ever to wake up it will continue to execute code. Of course you could globally disable the interrupts which would also prevent it from waking up. The mega168 has several sleep modes the power-down mode is very low current consumption as mrmeval pointed out.
 
Top