I am writing c code in xc8 compiler for PIC micro-controller. This code will control a fan's(motor) on/off and speed using CCP module.
To turn on/off the CCP module and consequently the fan, I am toggling the CCP1CON bits like so
ccp1conValue = CCP1CON;
ccp1conValue = ~ccp1conValue & 0x0F;
ccp1conValue = CCP1CON;
My concern is that is this syntax correct and acceptable in XC8? Can I use invert and AND operation in the same statement or I need to do them separately? Or is there a better way to do it altogether?
It's valid C syntax, and any slightly compliant compiler should handle it with grace. While people often push what is reasonable to do in a single line of C code, I would personally judge what you've done to be perfectly acceptable.
One thing to watch out for whenever you combine multiple operations is operator precedence.
Your statement:
~ccp1conValue & 0x0F
is equivalent to:
(~ccp1conValue) & 0x0F
This is likely what you were trying to do, but it's good to keep in mind.
Also, that last line:
ccp1conValue = CCP1CON;
should probably be:
CCP1CON = ccp1conValue;
instead.
Depending on if you need to keep the value, you may be able to replace those three lines with:
CCP1CON = ~CCP1CON & 0x0F;