Page 1 of 1

CRC calculation inside C-Box

Posted: Fri Jun 16, 2023 11:57 am
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

Re: CRC calculation inside C-Box

Posted: Fri Jun 16, 2023 12:06 pm
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.

Re: CRC calculation inside C-Box

Posted: Fri Jun 16, 2023 12:15 pm
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

Re: CRC calculation inside C-Box

Posted: Fri Jun 16, 2023 12:27 pm
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 1173 times
Calling Supplementary Code.png
Calling Supplementary Code.png (8.81 KiB) Viewed 1173 times

Re: CRC calculation inside C-Box

Posted: Wed Jun 21, 2023 9:08 pm
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 90 times

BR

Dirk

Re: CRC calculation inside C-Box

Posted: Thu Aug 03, 2023 6:40 pm
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