8bit Register Containing I/O bits from different PORTS( cutom port)

Status
Not open for further replies.

Mehmood Ahmed

New Member
Hi All,

I want to pack I/O bits from different ports into one 8bit variable making it easy for putting data on those pins simultaneously is there possibility. The compiler I am using is hitech C. I want to pack( RB2...RB7 , RC2 , RC3) into one 8bit register or variable so that I can use it as an 8bit data port.

Regards
 
Last edited:
For output..
PORTB = (data << 2) & 0xFC;
PORTC = (data >> 6) & 0x0C;

You may have to OR the ports if they are used for something else

Input
data = ( PORTB >> 2 ) & 0x3F;
data += (PORTC << 6 ) & 0xC0;
 
thanks for your reply...my requirement is not this rather I want someway like struct or union or something else that may help me pack those I/O bits into one single register...I want to avoid this method you have just told...please guide me if that is possible the method I want

Regards
 
You are still going to have to shift.. even using a union.. If you set up an integer and a bit-field in a union, you still need to get the bits into the right order. What kind of interface are you doing?
 
I think imaging structures and unions is only confusing him, all he needs to do is write a function/procedure to do what he wants.

Something like (not in C):

PortWrite = VariableA

VariableB = PortRead

Trivial to do, and easy to use.
 
Last edited:
Nigel ! I agree the way I proposed IS the best way.. I think union's will over complicate things
 
The union is a great tool... you can declare an integer (16 bits) and two bytes ( 8 obviously ) and 16 bit_fields.. in one variable .. Very useful for serialization.. The integer and two byte array occupy the same ram space... Quite easily obtained in asm as well.
 

And would that allow you to access bits in different ports?.
 
No... That's why it wouldn't be useful here. I don't think SFR's can be accessed this way in C ( someone may prove me wrong )
 
Ahmed - why do you want to avoid rotate, mask and inclusive-or? From your description (RB2...RB7, RC2 , RC3) I don't think there's any way around it.

Also, you seem to have 1 too many bits there.
 
Last edited:
Thanks to both of you seniors.Both of you gave great attention to my problem. On the basis of arguments both of you have given
(also 3v0 said the same thing yesterday when I was on chat) conclusion comes out to be the solution Ian has given and, I am going to use that one. Thank you.
Regards
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…