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; }
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);