Electronic – pass a bit register as a function argument in Hi-Tech C compiler for PIC16

ccompilerhi-tech-compilerpicregister

Is there a way to pass a bit from a PIC's register as a function parameter?

Taking, for example, the PIC16F887, its registers (SFRs) and individual bits are defined as fallows in the corresponding header file (...\HI-TECH Software\PICC\9.80\include\pic16f887.h):

volatile       unsigned char    PORTB       @ 0x006;

volatile       bit  RB3     @ ((unsigned)&PORTB*8)+3;

How can I pass the RB3 bit as a parameter in a function so that it will allow me to call the function in this way:

setBitHigh (RB3);

What would be the function's prototype?

I'm using the HI-TECH C Compiler for PIC10/12/16 MCUs (PRO Mode) V9.80.

EDIT: The setBitHigh function is just a simplified example. I actually want to pass bits as parameters to more complex functions and to be able to choose at runtime which bits should the function use.

Best Answer

Maybe this would work, but it is not standard C:

void setBitHigh( bit *b ){
   b = 1;
}
setBitHigh( & RB3 );

If not, then there is no direct way to do what you want. You could of course return the value from the function and then assign it:

RB3 = newBitValue();

or you could pass the function both the byte address and the offset, and leave it to the function to do the dirty work:

void setBitHigh( unsigned char *a, bit_offset b ){
   *a |= ( 1 << b );
}
setBitHigh( & PORTB, 3 );