#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. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,366
    Rep Power
    1870
    > Please let me know if condition of first "while" loop is satisfied / not satisfied, where control will pass to.
    Well it waits while TMR2IF_bit is zero (or put another way, waits until it is not zero - or being a single bit, one).
    The very next line TMR2IF_bit = 0 sets it back to zero again.

    > So far I know "while (1)" is an infinite loop. In this code which portion will be repeated indefinitely?
    Only the last line.
    You've written
    while ( 1 ) { /* do nothing forever */ }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Did you write this or did you get it from somewhere else?

    In a normal C program, that code is a sure loser. Neither of those while loops do anything, especially nothing that would ever change the value of the variable being tested in the while statements. In a normal C program, that would mean either nothing happens or you'd be locked in an infinite loop. Very bad programming practices.

    But this is embedded code. How is TMR2IF_bit declared? Is the keyword volatile used? If not, then you are in trouble. A C compiler will try to optimize the assembly code that it generates, which means that it will normally not check whether a variable's value has changed out from under you, so the value will be saved for easy access and never be updated. In embedded programming, interrupt service routines (ISRs) or hardware levels can change independent of the C code. In such cases, you need to use the volatile declaration qualifier to tell the compiler that every time you access that variable's value, it needs to actually access that variable itself and not some intermediate optimized register value or the like.

    Is that what this is about?

    BTW, very very nice that you used code tags, but the purpose of code tags is to preserve your code's formatting (HTML strips out extra spaces, including the leading spaces and tabs needed to format code listings). Your code should have looked like this:

    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); 
    }
    Actually, now we see that TMR2IF_bit is not volatile and never gets changed. Your code is useless.
  6. #4
  7. 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


    Thanks for quick reply. Actually the code has been collected from internet. It is a code to generate sine wave using PWM technique with the help of CCP Module of microcontroller. I have wrongly given a portion of the code. The code including ISR is given for your comment please.


    regards


    swapan


    Code:
    unsigned char sin_table[32]={0,25,49,73,96,118,137, 159,177,193,208,220,231,239,245,249,250,249,245, 239,231,220,208,193,177,159,137,118,96,73,49,25}; 
    
    unsigned int TBL_POINTER_NEW, TBL_POINTER_OLD, TBL_POINTER_SHIFT, SET_FREQ; 
    unsigned int TBL_temp; 
    unsigned char DUTY_CYCLE; 
    void interrupt(){ 
    if (TMR2IF_bit == 1){ 
    TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ;
     if (TBL_POINTER_NEW < TBL_POINTER_OLD){
     CCP1CON.P1M1 = ~CCP1CON.P1M1; //Reverse direction of full-bridge 
    
    } 
    TBL_POINTER_SHIFT = TBL_POINTER_NEW >> 11; DUTY_CYCLE = TBL_POINTER_SHIFT; 
    CCPR1L = sin_table[DUTY_CYCLE];
    TBL_POINTER_OLD = TBL_POINTER_NEW; 
    TMR2IF_bit = 0; 
    } 
    } 
    
    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); 
    TMR2IF_bit = 0; 
    TRISC = 0; 
    TMR2IE_bit = 1; 
    GIE_bit = 1; 
    PEIE_bit = 1; 
    while(1);
     }

IMN logo majestic logo threadwatch logo seochat tools logo