We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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++;
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.
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,
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.
counter=counter++; // original counter=(counter+=1);