EmbeddedRelated.com
Forums
Memfault Beyond the Launch

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

Started by Unknown February 29, 2008

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?

"Tom&#4294967295;s &#4294967295; h&#4294967295;ilidhe" <toe@lavabit.com> wrote in message 
news:eafe2d01-a21d-4f30-9a9d-563f96cfe6eb@e10g2000prf.googlegroups.com...
> > > 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&#4294967295;V as meeting the requirements for safety related systems.
On 2008-02-29, Tom&#4294967295;s &#4294967295; h&#4294967295;ilidhe <toe@lavabit.com> wrote:
> 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);
> Can anyone shed any light on their usage of bitwise AND for setting > config bits?
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
On Feb 29, 9:30 pm, John Temples <use...@xargs-spam.com> wrote:

> 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. >
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.
On Mar 3, 10:34 am, "M. Hamed" <mhs...@gmail.com> wrote:
> On Feb 29, 9:30 pm, John Temples <use...@xargs-spam.com> wrote: > > > 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. > > 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.

Memfault Beyond the Launch