I want to create a fctn which can set or clear a bit based on the value of a variable. I attempted to incorporate the provided macro fctns; it didn't work. It compiled, but didn't change port state. However, I don't understand the syntax of the macro. Q: why doesn't my fctn work? Q: how can the macro fctn work: using a keyword 'bit' for a variable name, and not defining a variable 'var'? Example code snippets below: ... in includes.h #define setbit(var,bit) (var |= (0x01 << (bit))) #define clrbit(var,bit) (var &= (~(0x01 << (bit)))) ... in main.c void changebit( UINT16 var, UINT8 bitn, bit bitstate) { if (bitstate) setbit(var,bitn); else clrbit(var,bitn); } ... in main() changebit(P7,4,bitstate); Thank you. David
Reading your msg more carefully,
//--- A --- UINT16 changebit( UINT16 var, UINT8 bitnum, bit bitstate) { if (bitstate) { var = var |(0x01 << bitnum); } else { var &= ~(0x01 << bitnum); } return(var); } P7 = changebit(P7,RED_LED,1); // LED OFF
//--- B --- void changebit( UINT16 *var, UINT8 bitnum, bit bitstate) { if (bitstate) { *var |= (0x01 << bitnum); // set } else { *var &= ~(0x01 << bitnum); // clr } } changebit(&P7, RED_LED, 1); // LED OFF