G Gobbledok Active Member Apr 21, 2011 #1 Hi I need to define a byte as (for example): The first bits need to be PORTE<5:1> Then followed by PORTD<3:2> Then followed by PORTE<8> I need to give it a name as a variable so I can address it as BYTE = 0xAA. I hope the description isn't too vague I don't know the terminology yet Thanks in advance
Hi I need to define a byte as (for example): The first bits need to be PORTE<5:1> Then followed by PORTD<3:2> Then followed by PORTE<8> I need to give it a name as a variable so I can address it as BYTE = 0xAA. I hope the description isn't too vague I don't know the terminology yet Thanks in advance
P Pommie Well-Known Member Most Helpful Member Apr 21, 2011 #2 The easiest way is to make it a function, Code: void WritePort(unsigned char dat){ porte &= 0b01000001; porte |= ((dat & 0b11111000)>>2)|((dat & 1)<<7); portd &= 0b11110011; portd |= (dat & 0b00000110)<<1; } And then you can just do WritePort(0x55); Whoa, just realized you name the bits 1-8 and not the conventional 0-7. The above will need slight adjustment if the 8 wasn't a typo. Mike. Last edited: Apr 21, 2011
The easiest way is to make it a function, Code: void WritePort(unsigned char dat){ porte &= 0b01000001; porte |= ((dat & 0b11111000)>>2)|((dat & 1)<<7); portd &= 0b11110011; portd |= (dat & 0b00000110)<<1; } And then you can just do WritePort(0x55); Whoa, just realized you name the bits 1-8 and not the conventional 0-7. The above will need slight adjustment if the 8 wasn't a typo. Mike.
G Gobbledok Active Member Apr 21, 2011 #3 Yeh wasn't a typo, I need PORTE<0> for something else. It is on a 16 bit PIC. Sweet though, I can get it from there. Wasn't sure if there was a better way to do it though. Thanks ! Last edited: Apr 21, 2011
Yeh wasn't a typo, I need PORTE<0> for something else. It is on a 16 bit PIC. Sweet though, I can get it from there. Wasn't sure if there was a better way to do it though. Thanks !
P Pommie Well-Known Member Most Helpful Member Apr 21, 2011 #4 In that case do it all in 16 bit, Code: void WritePort(unsigned int dat){ porte &= 0xfec1; porte |= ((dat & 0x00f8)>>2)|(dat & 1)<<8; portd &= 0xfff3; portd |= (dat & 0x0006)<<1; } Can't be doing with 16 bit binary so switched to hex. Mike.
In that case do it all in 16 bit, Code: void WritePort(unsigned int dat){ porte &= 0xfec1; porte |= ((dat & 0x00f8)>>2)|(dat & 1)<<8; portd &= 0xfff3; portd |= (dat & 0x0006)<<1; } Can't be doing with 16 bit binary so switched to hex. Mike.