Page 1 of 1

Setting Register Value in C Code

Posted: Mon Mar 31, 2025 9:26 am
by jay_dee
Hi,
in the past I have manually set register bits by bit shifting in a C code component.
for example,
PIR1 = PIR1 & ~(1<<2); // Clear CCP1IF
PIE1 = PIE1 | 0x04; // Set CCP1IE

When robbing, bodging and checking against with C code examples from Microchip application notes, I see they use an easier to read method.
PIR1bits.CCP1IF = 0; // Clear CCP1 interrupt flag
PIE1bits.CCP1IE = 1; // Enable the CCP1 int

Can this method be used without issue in Flowcode C code components?
Obviously your register names have to be correct to avoid throwing a compile error!

Re: Setting Register Value in C Code

Posted: Mon Mar 31, 2025 9:32 am
by jay_dee
In another project I also See, they use something similar in IF statements.
if (SSPSTATbits.D_nA) // If D_nA = 1 then TRUE
if (!SSPSTATbits.R_nW) //If R_nW = 0 then TRUE

Can something similar be used in a Normal FC IF Statement? Would it need just need FCV_ adding to it?
FlowCode IF componet = "FCV_SSPSTATbits.D_nA" // If D_nA = 1 then TRUE

or is there a similar method that works?
thanks, J.

Re: Setting Register Value in C Code

Posted: Mon Mar 31, 2025 9:58 am
by Steve-Matrix
jay_dee wrote:
Mon Mar 31, 2025 9:26 am
When robbing, bodging and checking against with C code examples from Microchip application notes, I see they use an easier to read method.
PIR1bits.CCP1IF = 0; // Clear CCP1 interrupt flag
PIE1bits.CCP1IE = 1; // Enable the CCP1 int

Can this method be used without issue in Flowcode C code components?
Yes, that should work fine. It will obviously not simulate though.

jay_dee wrote:
Mon Mar 31, 2025 9:32 am
In another project I also See, they use something similar in IF statements.
if (SSPSTATbits.D_nA) // If D_nA = 1 then TRUE
if (!SSPSTATbits.R_nW) //If R_nW = 0 then TRUE

Can something similar be used in a Normal FC IF Statement? Would it need just need FCV_ adding to it?

No, not directly. You could first read the bit into a Flowcode variable in a C code icon and then use a decision icon on that variable:

FCV_MYVAR = SSPSTATbits.D_nA;

...then "if MyVar" in the decision.

Or you could customise the code of the decision icon and use C there directly.

Re: Setting Register Value in C Code

Posted: Mon Mar 31, 2025 12:58 pm
by jay_dee
Hi Steve,
If I have a FC LOCAL variable, inside a user macro. How would I reference this in C ?
Assuming "MyMacro" and Local Variable ".MyVar"

would something like FCV_MYVAR = SSPSTATbits.D_nA;
become
FCV_MYMACRO.MYVAR = SSPSTATbits.D_nA;
?? thanks, John.

Re: Setting Register Value in C Code

Posted: Mon Mar 31, 2025 2:13 pm
by Steve-Matrix
The short answer is FCL_MYVAR. The suffix for globals is "FCV_" and the suffix for locals is "FCL_".

In the C-code icons (which includes any other icons that have been customised), you can drag variables from the lists on the right onto the C code. When dropped, they will be added to the C code text with the appropriate C reference.