#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    A very novice question from newbie


    Friends,

    I am a new member in this forum. I am an electronics hobbyist and presently working with hobby projects based on microcontroller. I have already completed some projects for which source code has been developed through assembly language. Now I am trying to enter into the world of higher language like C. A sample source code is given below. in the code two "while" loops are there. Please let me know if condition of first "while" loop is satisfied / not satisfied, where control will pass to. So far I know "while (1)" is an infinite loop. In this code which portion will be repeated indefinitely?

    regards

    swapan


    Code:
    void main() { 
    SET_FREQ = 410; TBL_POINTER_SHIFT = 0; TBL_POINTER_NEW = 0; 
    TBL_POINTER_OLD = 0; 
    DUTY_CYCLE = 0; ANSEL = 0; //Disable ADC 
    CMCON0 = 7; //Disable Comparator 
    TRISC = 0x3F; 
    CCP1CON = 0x4C; 
    TMR2IF_bit = 0; 
    T2CON = 4; //TMR2 on, prescaler and postscaler 1:1 
    while (TMR2IF_bit == 0); // This is the first loop.
    TMR2IF_bit = 0; 
    TRISC = 0; 
    TMR2IE_bit = 1; 
    GIE_bit = 1; 
    PEIE_bit = 1; 
    while(1); 
    }
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,020
    Rep Power
    1285
    In this case, both loop will be infinite. In the first loop, you have the variable already set to 0, so as long as that's true, the loop will run. If the value changes, then control will pass to the next line.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,706
    Rep Power
    480
    If you declared variables in the most straightforward way to have the code compile, it would iterate forever at

    while (TMR2IF_bit == 0); // This is the first loop.

    because the statement body is empty.

    I suppose TMR2IF_bit could have extern linkage and volatile quality. As such an external event would let the program continue through to the second loop which runs forever. I'd write the last loop as
    while(1) sleep(9999);
    to conserve electrons. If you can't save electrons, no one can.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo