This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

toggling port pins through an element in a structure

Here is what I am trying to do:

sbit PORT_1_0 = P1^0;

void main ()
{ PORT_1_0 = 1; while(1) { PORT_1_0 =~ PORT_1_0; }
}

Now this is as simple as it gets.
The question is, is it possible to do the same thing using an element of a struct?

Something like:

struct abc
{ unsigned char bit : 1;
};

struct bit bit_array[24];

bit_array[0].bit = P1^0; //Assign the port pin to be toggled

//and then the toggling here.

Basically, how to assign a port pin to the bit in a struct? I am doing this because based on the address of a frame, I need to decide which pin is to be set.
I hope you get the question.

Parents
  • cant this be implemented using bit padding,
    You mean bit fields. There is no such thing as bit padding.

    instead of declaring an array of 24 bit
    Not really. Because what you propose loses a major property of what the OP was trying to do. Array elements are addressed by number, struct elements (including bit fields) are addressed by name. The OP wanted an array, so a struct won't work for him. Nor would that struct really offer any benefit over a "bit array" technique, where you use set/clear/query macros to get the values of individual bits out of an array of bytes, as in

    #define BIT_ARRAY_DEFINE(array,nbits) uint8_t array[(nbits + 7)/8];
    #define BIT_ARRAY_SET_BIT(array,n) ((array)[(n)/8] |= (1 << ((n) & 7)))
    

    SFRs (boths as bytes and as bits) in an '51 controller are rather weird by modern standards. You cannot construct a pointer to an SFR, because they do not allow indirect addressing. So no arrays of SFRs or SFR bits, no constructing a pointer to an SFR, ...

Reply
  • cant this be implemented using bit padding,
    You mean bit fields. There is no such thing as bit padding.

    instead of declaring an array of 24 bit
    Not really. Because what you propose loses a major property of what the OP was trying to do. Array elements are addressed by number, struct elements (including bit fields) are addressed by name. The OP wanted an array, so a struct won't work for him. Nor would that struct really offer any benefit over a "bit array" technique, where you use set/clear/query macros to get the values of individual bits out of an array of bytes, as in

    #define BIT_ARRAY_DEFINE(array,nbits) uint8_t array[(nbits + 7)/8];
    #define BIT_ARRAY_SET_BIT(array,n) ((array)[(n)/8] |= (1 << ((n) & 7)))
    

    SFRs (boths as bytes and as bits) in an '51 controller are rather weird by modern standards. You cannot construct a pointer to an SFR, because they do not allow indirect addressing. So no arrays of SFRs or SFR bits, no constructing a pointer to an SFR, ...

Children
No data