We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
hello every one,
I am trying to use bitfield in my project. i want to assign the port address to the byte which i have created using bitfield.
/* following is the code i have tried */ typedef union { byte Byte; struct { byte Pb0 :1; byte Pb1 :1; byte Pb2 :1; byte Pb3 :1; } Bits; struct { byte Pbyte :8; } MergedBits; } Ppins; extern volatile Ppins _PTAD @0x00000000; #define FByte _PTAD.Byte #define Bit1 _PTAD.Bits.Pb0 #define Bit2 _PTAD.Bits.Pb1 #define Bit3 _PTAD.Bits.Pb2 #define Bit4 _PTAD.Bits.Pb3
can i give the address in this way or is there any other way to do this. Please help me out.
Embedded programming means that you sometimes "cuts a corner" and does something a little bit non-standard just because your program is specifically climbing all over the hardware.
However, just because the code is mapped to specific hardware doesn't mean that you should ignore all rules about writing portable code.
The C standard doesn't specify how bit fields should be allocated. This is an okay decision for a normal software that doesn't need to write down the structure as a binary block to file - OR MAP THE BITS TO A HW REGISTER.
You can't know where your four bits Pb0 to Pb3 will be placed in the byte. Based on that, you have to decide for yourself if it really is a good idea to base your code on such an assumption. Moving from one compiler to another (or a newer version of your current compiler) may break the code...
Another thing: Why having a union with _three_ alternatives? The third alternative is a struct containing just a single 8-bit field...
A third thing: It is a lot easier to read the code if it has any line breaks. When you post messaes, there are good instructions about what to do with source code, to make it readable.
Thank you people for reply message.
My intention in this part of program is to use only 4-bits of Port register for one function and other 4-bits of same Port register for other function, with out disturbing each other nibbles.
And also to access the same Port register as a byte of data for another function.
So, if there is any other method to do this please suggest me.