Using bitwise AND for setting configuration bits? What's all this about

Feb 29, 2008 4 Replies

I've been programming the PIC16F684 for a few weeks now, and only today did I notice the peculiarity of how the configuration bits were being set:



__CONFIG(FCMDIS & IESODIS & BORDIS & UNPROTECT & MCLRDIS & PWRTEN & WDTDIS & INTIO);



I would have expected bitwise OR to be used in conjunction with macros that equate to an exact power of 2, like as follows:



#define FCMDIS 1 #define IESODIS 2 #define BORDIS 4 #define UNPROTECT 8 #define MCLRDIS 16 #define PWRTEN 32



__CONFIG(FCMDIS | IESODIS | BORDIS | UNPROTECT | MCLRDIS | PWRTEN | WDTDIS | INITO);



Can anyone shed any light on their usage of bitwise AND for setting config bits?



If you look at the definitions for the constants that are &'ed together you will see that they define which bit should be cleared, not which bit should be set. Hence the & rather than the |. I would hazard a guess it is done this way for simplicity - to minimise the number of definitions you need to change what would otherwise be the default behaviour.

Regards, Richard. + http://www.FreeRTOS.org & http://www.FreeRTOS.org/shop 16 official architecture ports, 5000 downloads per month. + http://www.SafeRTOS.com Certified by TÜV as meeting the requirements for safety related systems.

It's because configuration bits default to '1'. Using OR would mean every bit would have to be specified in the CONFIG directive, and any unintentional omission would set the bit to its non-default state.

John W. Temples, III

That would make sense though if the defines were 0xFE, 0xFD etc, but with these defines, ANDing the first two terms would render the result

0 and hence all subsequent ANDs zero, and so all bits would be cleared. So I still don't get it either.

Which they probably are. Sorry I misread the OP.

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required