CRC calculation inside C-Box

For general Flowcode discussion that does not belong in the other sections.
Post Reply
dvcam99
Posts: 89
http://meble-kuchenne.info.pl
Joined: Fri Dec 04, 2020 11:03 am
Has thanked: 6 times
Been thanked: 12 times

CRC calculation inside C-Box

Post by dvcam99 »

Hello Matrix Team,
question ist it possible to use the follwing C-code inside FC in a C-Box



/*
Enter C code below this comment
*/
/* 8-bit CRC polynomial X^8 + X^2 + X + 1 */.
#define FCV_POLY 0x07
unsigned char update_crc (unsigned char FCV_CRC, unsigned char FCV_CRC_SEED)
{
unsigned char FCV_CRC_U;
unsigned char FCV_I;

FCV_CRC_U = FCV_CRC;
FCV_CRC_U ^= FCV_CRC_SEED;

for (FCV_I=0; FCV_I<8; FCV_I++)
{
FCV_CRC_U = ( FCV_CRC_U & 0x80 ) ? FCV_POLY ^ ( FCV_CRC_U << 1 ) : ( FCV_CRC_U << 1 );
}
return FCV_CRC_U;
}
unsigned char crc8 (unsigned char *FCV_CRC, unsigned char FCV_CRC_LENGTH)
{
unsigned char FCV_CRC_UP = 0;
unsigned char FCV_C;

for(FCV_C=0;FCV_C < FCV_CRC_LENGTH; FCV_C++) {
FCV_CRC_UP = update_crc (FCV_CRC[FCV_C], FCV_CRC_UP);
}

return FCV_CRC_UP;
}


Attached the FC file too.


THX in advance

Dirk
Attachments
CRC8_c_code_V_1_0.fcfx
(10.77 KiB) Downloaded 52 times
Happy FC9, FC-8 and FC-6 professional user ;)

kersing
Valued Contributor
Posts: 161
Joined: Wed Dec 02, 2020 7:28 pm
Has thanked: 66 times
Been thanked: 58 times

Re: CRC calculation inside C-Box

Post by kersing »

That won’t work because you can not define new functions within a macro (which is a function). It should not be difficult to rewrite the C code as macro. Or you can add the C code in the supplementary code.

mnfisher
Valued Contributor
Posts: 969
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 514 times

Re: CRC calculation inside C-Box

Post by mnfisher »

Note that you could create a second function (crc8). You can call FC macros from within C using FCM_macroname(args) - the easiest way to see the arguments needed is to look at the C code for a macro, and this is one instance where names do not get capitalised.

As pointed out - it would probably be more sensible to convert to a couple of FC macros in this instance.

Martin

Steve-Matrix
Matrix Staff
Posts: 1262
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 168 times
Been thanked: 279 times

Re: CRC calculation inside C-Box

Post by Steve-Matrix »

It is possible, but C code icons within the flowcharts in Flowcode are not ideally suited to contain full C functions. A better place to store functions within "supplementary code" (under Build->Project Options). You can then call these functions from your flowchart using a C code icon.

I also notice you are using variable names within your C code that start with "FCV_". I would avoid this because they could clash or cause confusion with variables created within Flowcode. In your own C functions you should use 'normal' variable names rather than the names of Flowcode variables. The exception is when you want to refer to Flowcode variables within your C code (e.g. when passing Flowcode variables to your functions).

I have attached a simple example project of using Supplementary Code within Flowcode. And here are some images from the example.
Supplementary Code Example.png
Supplementary Code Example.png (23.63 KiB) Viewed 1125 times
Calling Supplementary Code.png
Calling Supplementary Code.png (8.81 KiB) Viewed 1125 times
Attachments
Supplementary Code example.fcfx
(7.27 KiB) Downloaded 57 times

dvcam99
Posts: 89
Joined: Fri Dec 04, 2020 11:03 am
Has thanked: 6 times
Been thanked: 12 times

Re: CRC calculation inside C-Box

Post by dvcam99 »

Hello together,

mayn thank for your help. I could sove my problem with your help.

Attached my FC solution:
CRC8_c_code_V_1_2.fcfx
(12.19 KiB) Downloaded 86 times

BR

Dirk
Happy FC9, FC-8 and FC-6 professional user ;)

Landlweg
Posts: 7
Joined: Fri Jun 11, 2021 6:44 pm

Re: CRC calculation inside C-Box

Post by Landlweg »

Good afternoon,

i am new
work with Pic16F18877, BL0080, Uart, Flowcode V10

i have to transmit 9 byte data and a 2 byte checksum with CRC-CCITT16-polynom 0x8408

i have found a nice example
CRC8_c_code_V_1_2.fcfx
do you have also a example with CCITT16-polynom 0x8408
or can you help ?

best regards
Hermann Pirklbauer - Austria

Post Reply