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.
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
The expression (c & 0x01) can only yield the value 0 or 1, and since the value of the ASCII character '0' is 0x30, neither 0 nor 1 will ever equal '0'.
this is my program code.im still unable to on only one led.
#include <putl.h> char xdata OPORT _at_ 0x0000; char xdata IPORT _at_ 0x0001; void sInit(void) { PCON=0x80; TMOD=0x20; SCON=0x50; TH1=243; TR1=1; } void sputCh(char c) { while(T1==0); SBUF =c; TI=0; } unsigned char sgetCh(void) { unsigned char c; while(RI==0); c=SBUF; RI=0; return c; } void main(void) { unsigned char c; //unsigned char r; sInit(); while(1) { c=sgetCh(); OPORT=c; { if(c=='1') { if((c&0x01)==0) { OPORT=0x00; } else { OPORT=0xff; } } else if(c=='2') { if((c&0x02) { OPORT=0x00; } else { OPORT=0xff; } } } } }
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.