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!
Setting Register Value in C Code
-
- Posts: 238
- http://meble-kuchenne.info.pl
- Joined: Sun Dec 20, 2020 6:06 pm
- Has thanked: 96 times
- Been thanked: 67 times
Re: Setting Register Value in C Code
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.
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.
-
- Matrix Staff
- Posts: 1587
- Joined: Sat Dec 05, 2020 10:32 am
- Has thanked: 222 times
- Been thanked: 372 times
Re: Setting Register Value in C Code
Yes, that should work fine. It will obviously not simulate though.jay_dee wrote: ↑Mon Mar 31, 2025 9:26 amWhen 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?
jay_dee wrote: ↑Mon Mar 31, 2025 9:32 amIn 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
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.
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.
-
- Matrix Staff
- Posts: 1587
- Joined: Sat Dec 05, 2020 10:32 am
- Has thanked: 222 times
- Been thanked: 372 times
Re: Setting Register Value in C Code
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.
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.
Re: Setting Register Value in C Code
Hi,
this method of setting values in registers work well for me.
REGISTERNAMEbits.BITNAME
for example,
BRGCON2bits.SAM = 1; // Set the SAM bit to 1 in Register BRGCON2
However if a bit field is longer than one bit, how do I refer to the BitFields name?
Do I just refer to the lowest value bit, bit0? even though the bit field may be 2, 3, 6... bits in length?
i.e
REGISTERNAMEbits.BITNAME0
for example,
BRGCON1bits.BRP0 = 2 // Set the BRP bits to 2 in Register BRGCON1
Is this approach valid, it compiles but I'm debugging my own CAN INIT and just want to make sure this shortcut notation is not part of the problem.
Thanks, J.
this method of setting values in registers work well for me.
REGISTERNAMEbits.BITNAME
for example,
BRGCON2bits.SAM = 1; // Set the SAM bit to 1 in Register BRGCON2
However if a bit field is longer than one bit, how do I refer to the BitFields name?
Do I just refer to the lowest value bit, bit0? even though the bit field may be 2, 3, 6... bits in length?
i.e
REGISTERNAMEbits.BITNAME0
for example,
BRGCON1bits.BRP0 = 2 // Set the BRP bits to 2 in Register BRGCON1
Is this approach valid, it compiles but I'm debugging my own CAN INIT and just want to make sure this shortcut notation is not part of the problem.
Thanks, J.
-
- Valued Contributor
- Posts: 1684
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 145 times
- Been thanked: 784 times
Re: Setting Register Value in C Code
The 'bits' refers to a bitfield
So for example OSCCONbits.ICRF = 0b101 would set three bits (ICRF<0..2> - on a sample PIC18F) would be my understanding - whereas OSCCONbits.ICRF0 would refer to a single bit.
Martin
So for example OSCCONbits.ICRF = 0b101 would set three bits (ICRF<0..2> - on a sample PIC18F) would be my understanding - whereas OSCCONbits.ICRF0 would refer to a single bit.
Martin
Re: Setting Register Value in C Code
Intersting and make sense but 
if I use the Bitfield name the compiler throws an error. But 'BRGCON1bits.BRP0 = 1' will pass

if I use the Bitfield name the compiler throws an error. But 'BRGCON1bits.BRP0 = 1' will pass
3698: BRGCON1bits.BRP = 1;
^ (255) not a member of the struct/union ""
^ (182) illegal conversion between types
int -> volatile union S783
-
- Valued Contributor
- Posts: 1684
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 145 times
- Been thanked: 784 times
Re: Setting Register Value in C Code
Odd - I've just tested with a PIC18F46K80 as the target and:
OSCCONbits.INTRF = 0b101;
BRGCON1bits.BRP = 0b10001;
Compiles okay - maybe been missed from the include for the MCU you are using?
The alternative is something like (assuming BRP is bits 0..4)
BRGCON1 = (BRGCON1 & 0b11100000) | DESIRED_BRP_VALUE; // Clear BRP then OR in new bits
Martin
OSCCONbits.INTRF = 0b101;
BRGCON1bits.BRP = 0b10001;
Compiles okay - maybe been missed from the include for the MCU you are using?
The alternative is something like (assuming BRP is bits 0..4)
BRGCON1 = (BRGCON1 & 0b11100000) | DESIRED_BRP_VALUE; // Clear BRP then OR in new bits
Martin