im using 8051 and had a problem writing a code to interface the LED using serial port interface.Im using the I/O controller.the problem is when i ON the LED it light all the 7 leds.There is 7 output port. i try to use a bitwise as to ON the LED of bit 0: if((c&0x01)=='0'){ OPORT=0x00;}
else if(c=='1') { OPORT=0xff; }
thanks
I'm not going to rewrite your program, but I am going to tell you to think a bit more about what you are trying to do!
Exactly how do you think:
if (c=='1') { if ((c&0x01)==0) { OPORT=0x00; } else { OPORT=0xff; } }
will work?
If you have already checked if c equals '1', how do you then think that a second if statement would be able to find c to be sometimes odd and sometimes even? Where would the variable c manage to change it's value between the outer and inner if?
Another thing: you want to change one single LED? How many bits should you then toggle so as not to change the value of the other 7 LED?
Turning on a single bit can be done by: x |= (1 << bitpos); Turning off a single bit can be done by: x &= ~(1 << bitpos); where bitpos is a value between 0 and 7 (if x is a byte).
"Turning on a single bit can be done by: x |= (1 << bitpos); Turning off a single bit can be done by: x &= ~(1 << bitpos);"
This is standard 'C' - general and applicable to any compiler and any target; it is Portable
The 8051 has a particular feature that some addresses are bit-addressable; ie, individual bits can be addressed directly.
Keil C51 provides some specific, proprietary extensions to the 'C' standard to allow you to take advantage of this feature; of course, this is non-Portable
Ports P0-P3 are bit addressable...
Yes, I'm aware of the bit-addressing in the 8051.
Note the specific sentence in the first post: "the problem is when i ON the LED it light all the 7 leds.There is 7 output port."
Then note that the OP used the declaration:
char xdata OPORT _at_ 0x0000;
and then performed assigns:
OPORT = 0xff;
or
OPORT = 0x00;
To my eyes, that doesn't look like an attempt at using bit-addressable variables - hence my comment about the general method to set or clear a bit.
I think that the OP code will need a number of refining steps, and I don't think it is a good idea to just post a complete rewrite of the code. Each single change to the code must be followed my an understanding why.
I feel that switching to bdata, sbit, overloading of the ^ operator etc is a separate step.