This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Interruption with IF

I'm trying trying to program a square signal frequency to simulate the signal obtained in
the sensor of the rpm of an engine (in the flywheel). This frequency is generated in a
timer interruption. If I don't take into consideration the two missing teeth (forget about
the bold lines in the source code) of the flywheel (60 teeth - 2teeth) the code is
working, but when I try to get rid of the two first teeth my software is doing nothing.

What could be wrong?

Thanks a lot.

#pragma RB(1)
timer0() interrupt 1 using 1
{ //takes into account the two missing teeth of the flywheel
TR0=0; //stop the timer
EAL=0; //disable all interrupts
counter=counter++; //
if (counter!=121) //60 teeth + 60 holes
{
if (counter>6)
{ //
frequen_1=~frequen_1; //invert the signal
}
}
else
{
TR0=0; // two extra microseconds
TR0=0;
counter=1;
frequen_1=~frequen_1;
}
//in case on the two first teeth I don't invert my signal
TL0 = new_timer_reload; //load the new values
TH0 = new_timer_reload_high;
TR0=1; //start the timer
EAL=1;
}

  • I just did a quick experiment with my Keil C version 6.14. The following statement:

    	counter=counter++;
    
    was optimised out!

    This is not a very conventional C statement, but it looks as if it should be legal. So, this looks like a compiler bug to me! It might be interesting to try compiling with lower levels of optimisation.

    With counter stuck at 0, I don't think your code will be doing anything.

    Try:
    	counter++;
    

  • "counter=counter++;"
    Under Keil C51 5.50 this statement don't work
    but under Keil C51 V7.0 seems it works OK.

      
        counter++; 
    is a better 'C' notation then    
        counter=counter++;
    It looks unnatural. 
    
    Vaclav

  • Thank's a lot guys.

    You got the right answer. Now it's working

    Jorge Cases

  • I just did a quick experiment with my Keil C version 6.14. The following statement:


    counter=counter++;

    was optimised out!


    In the C Programming Language, a C compiler is supposed to evaluate everything on the RIGHT HAND SIDE of an assignment BEFORE it changes the thing on the LEFT HAND SIDE.

    So, with that in mind,

    counter=counter++;
    

    is evaluated as follows:

    1. Obtain the value of counter.
    2. Increment the value of counter.
    3. Assign the value obtained in step 1 to counter.

    In effect, this statement does nothing. So, the compiler optimized it out. It is NOT a bug. It is how the C Language works.

    Jon

  • Sorry for my late reply. I am very busy, but I'd like to do reply for Jon's contribution.

    "counter=counter++;

    is evaluated as follows:

    1. Obtain the value of counter.
    2. Increment the value of counter.
    3. Assign the value obtained in step 1 to
    counter.
    ...
    It is NOT a bug. It is how the C Language works."


    Jon,
    It's very thin ice you walking about in this case I think.
    It's right you wrote about r-value and l-value evaluation but why is made increment for the "counter=counter++;" statement in C51 V7.0 for all optimize levels?

    There are two variants of explanation by my opinion:
    A. The "++" operator in "counter++;" statement is postfix operator which means use variable and then increment.

     1. Obtain the value of counter. 
     2. Assign the value obtained in step 1 to counter.
     3. Increment the value of counter.
    

    B. Statement "counter++;" is same as "counter=counter+1;" or "counter+=1;". If I use it instead of original statement:
      counter=counter++;  // original
      counter=(counter+=1);
    
    Both statements are optimized and evaluated in C51 V7.0 as increment while C51 V5.50 don't evaluate them and optimize out both above mentioned statements.

    My question is:
    What do you think about V7.0 and V5.50a compiler version differnces?
    Is there some other explanation for V7.0 operation and what is right?

    Regards,
    Vaclav