So, the thing is I am trying to transfer a byte to a 8-pin parallel port on a LCD. Due to some space issue, The bit 0~bit 7 is plugged in digital port 28,30,32,...,42 on a Arduino Mega2560. So what I did in code is:
void LCD(byte cmd){
byte i;
byte ptr;
ptr=1;
for(i=28;i<=42;i+=2)
{
if (cmd & ptr == ptr)
digitalWrite(i,HIGH);
else
digitalWrite(i,LOW);
ptr << 1;
}
}
So the idea is, if the lowest bit (bit 0) of cmd is 1, then (cmd & ptr) will equal to 0b00000001, (which is equal to ptr itself) so the pin 28 (bit 0) will be high. and vise versa, if the bit 0 is 0, (cmd & ptr) will not equal to ptr and the pin 28 is low. After the lowest bit is done, ptr do a left-shift by 1 bit (which is 0b00000010), then cycle on to check bit 1, 2, ... bit7, hence the whole byte is converted to a series of low and high.
The problem is, it doesn't work as expected, and once there is a bit 1 in the cmd, no matter how many and where it is, multimeter shows all pins are high. Is there anything wrong with my code or digitalWrite shouldn't be used like this ?
P.S. hardware is checked and made sure no short-circuit is happening.