EndingCPrograms

Discussion in 'Programmer's Corner' started by mpuvdd, Dec 27, 2007.

  1. mpuvdd

    Thread Starter Active Member

    Feb 11, 2007
    50
    0
    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
     
  2. CVMichael

    Senior Member

    Aug 3, 2007
    416
    17
    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 ?
     
  3. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    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:

    Code ( (Unknown Language)):
    1.  
    2. for(;;)
    3. {
    4.  
    5. /* Your code is inserted inside curly brackets */
    6.  
    7. }
    8.  
    also

    Code ( (Unknown Language)):
    1.  
    2. While(1)
    3. {
    4.  
    5. /* Your code is inserted inside curly brackets */
    6.  
    7.  
    8. }
    9.  
    The code you insert between the curly brackets will be executed over and over as long as power is applied to the microcontroller.

    hgmjr
     
  4. mrmeval

    Distinguished Member

    Jun 30, 2006
    833
    2
    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.
     
  5. mpuvdd

    Thread Starter Active Member

    Feb 11, 2007
    50
    0
    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
     
  6. nanovate

    Distinguished Member

    May 7, 2007
    665
    1
    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.
     
  7. mpuvdd

    Thread Starter Active Member

    Feb 11, 2007
    50
    0
    Gotch ya...
    Thanks a lot