I wrote a code for to toggle the port that is given below #include<reg51.h> sbit CLOCK = P0^2; void main() { while(1) { CLOCK = CLOCK^1; } } its not toggling in the IDE please guide me Mahesh
Better u try this.
#include<reg51.h> sbit CLOCK = P0^2; void main() { while(1) { if(CLOCK) { CLOCK = 0; } else { CLOCK = 1; } } }
Why do you say that is "better"? Certainly more verbose! It seems very cumbersome to test the value, then assign the complement, rather than just use the complement operator directly! You could also try
COUNT = COUNT ? 0 : 1;
May be. But I have used like this and it is error free.
but have you tried
CLOCK = ~CLOCK;
CLOCK = !CLOCK;
You please compile these two codes in Keil and watch the opcode generated thro disassembly window. You can fine which is better.
#include<reg51.h> sbit CLOCK = P0^2; void main() { if(CLOCK) { CLOCK = 0; } else { CLOCK = 1; } }
void main() { CLOCK = CLOCK ? 0 : 1; }
The code size for yours is 25 and mine is 24. In the best case, that is if the clock is high, my code will work 2 cycles faster. You can place breakpoints in both the cases, if you want.
View all questions in Keil forum