Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Using bits from multiple ports as a byte in C

Status
Not open for further replies.

Gobbledok

Active Member
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 :)
 
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:
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:
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.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top