Bit order using PortIn? Mystery!

16 Nov 2016

Weird! Using

PortIn(Port0,0x7878000)

, which is

 (p18,p17,p16,p15,p11,p12,p14,p13)

, I read the portIn value. I am extremely confused about the bit order of the received byte. Using this code to print every bit:

pc.printf(..., s,s&0x80,s&0x40,s&0x20,s&0x10,s&0x8,s&0x4,s&0x2,s&0x1)

, I see that, when pin input is HIGH, the value of the bits is:

 (8,4,2,1,256,512,1024,2048)

What is the correct order to read one byte from PortIn?

PS I' ve found a function to read a byte from PortIn, but seems not to work and is even more confusing:

 int top = s >> 19;         // Isolate the top nibble
        int middle = s >> 2;       // Isolate bits 2 & 3
        s = s & 0x00000003;   // Isolate bits 0 & 1      
        s += middle;
        s += top;

So what the hell is going on here? THANKS everybody!!

17 Nov 2016

usually a port is only 8 bits wide...

you have eight bits jumbled up..

char PortValue = 0;

PortValue +=  1* (s & 0x8);  // bit 0 is 1 when p11 is high
PortValue +=  2* (s & 0x4);  // bit 1 is 1 when p12 is high
PortValue +=  4* (s & 0x1);  // bit 2 is 1 when p13 is high
PortValue +=  8* (s & 0x2);  // bit 3 is 1 when p14 is high
PortValue +=  (s & 0xF0);    // bits 7:4 are 1 when pins p18:15 are high

printf("Portin s= %02X  P18:P11 is %02X",s,PortValue );

I am nu-bee too :(