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.
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
Thanks... I was thinking the same thing; avoid the macro and just write the C. However, the following generates syntax errors beginning with '|=' . I'm missing something.
void changebit( UINT16 *var, UINT8 bitnum, bit bitstate) { if (bitstate) { *var |= (0x01 << bitnum); // set } else { *var &= ~(0x01 << bitnum); // clr } } ... in main() changebit(&P7, RED_LED, 1); // LED OFF