visiontec
Newbie level 4
how to assign port in c
Im trying to get values out of an array and assign them to a P2 pins.
this is my code:
but is there any way of doin this more effeciently?
I tried doing this:
but this didnt really work out.
guys can someone tell me how i should do this?
Im trying to get values out of an array and assign them to a P2 pins.
this is my code:
Code:
unsigned char xdata Relay_state[8];
void main (void)
{
.......
update_relay();
}
void update_relay(void)
{
int port_status = P2;
if(port_status & 2^0 !=Relay_state[0]) P2_0 = !Relay_state[0];
if(port_status & 2^1 !=Relay_state[1]) P2_1 = !Relay_state[1];
if(port_status & 2^2 !=Relay_state[2]) P2_2 = !Relay_state[2];
if(port_status & 2^3 !=Relay_state[3]) P2_3 = !Relay_state[3];
if(port_status & 2^4 !=Relay_state[4]) P2_4 = !Relay_state[4];
if(port_status & 2^5 !=Relay_state[5]) P2_5 = !Relay_state[5];
if(port_status & 2^6 !=Relay_state[6]) P2_6 = !Relay_state[6];
if(port_status & 2^7 !=Relay_state[7]) P2_7 = !Relay_state[7];
}
but is there any way of doin this more effeciently?
I tried doing this:
Code:
int shifter = 0x01;
int c = 0;
while(c<8)
{
if((port_status & shifter) != Relay_state[c])
{
P2 |= shifter;
}
c++;
shifter = shifter << c;
}
but this didnt really work out.
guys can someone tell me how i should do this?