Passing a byte and its bits to a function
Is there a way to pass a union member into a function? For instance, I have a function prototype here:
Code:
Void function(unsigned char input);
Now, I want to pass an 8-bit variable to the function, but I want access to each individual bit within the function. Normally, I can make a union in my main function loop for a variable like so:
Code:
union variable {
unsigned char byte;
struct {
unsigned b0:1;
unsigned b1:1;
unsigned b2:1;
unsigned b3:1;
unsigned b4:1;
unsigned b5:1;
unsigned b6:1;
unsigned b7:1;
} bits;
};
volatile union variable var1; //the actual union name for this specific instance is var1
I have made a union for my unsigned char because in some operations, I want to operate on the 8 bit variable (for instance, subtract 15 from var1: var1.byte - 15), while in other operations, I want to operate on individual bits of the variable (for instance, is bit 3 of var1 equal to 0: var1.bits.b3 == 0).
Now, concerning the definition for my function, it might look something like this:
Code:
Void function(unsigned char input)
{
Input.b2 = 1;
}
I am passing one entire 8-bit variable to the function, but inside the function, I want to operate on the individual bits of this variable. How would I do this? I think I need a union with a structure somewhere (like I have included above), but I am not sure if this union is placed in the main loop where the passing variable is, or inside the function definition where the temporary variable to the function is, or should the variable union be declared as a global (outside of any function)?