Cantafford
Member
Hey,
I want to perform a CRC function on some data I recieve from the UART module of PIC18f2450.
I have written 2 functions to test it's implementation. One which will recieve data that I want to send. It will calculate CRC with the specific polynomial(named pattern in code). Then will return a value which is equal to the initial message to which I append the result of the CRC calculation.
The second function recieves this value and performs the check(by xor'ing it with the polynomial) prints a message in which it lets the user know whether the data was altered or not.(by checking if the final result of 2nd xor is 0)
My polynomial is 0x000B.
This is the first function(one that calculates the CRC pattern and returns it):
And this is the second function. The one that checks the result of the xor'ing and prints a message telling us if the data was altered or not depending if the result is 0 or not. It should also print the CRC result from the first function.(AKA the last 3 bits of value returned by first function.
Then in my main function I create a variable then equal it to the return of first function to which I give a value.
Then when calling the second function I'm expecting to see the result of the CRC(something different than 0) and the message that data is ok and it's value.
However when I run this the second function prints this:
It should print a value different than 0 for CRC result then the message that the data was not altered and it's value.
Please help me correct this issue if possible. Thank you.
I want to perform a CRC function on some data I recieve from the UART module of PIC18f2450.
I have written 2 functions to test it's implementation. One which will recieve data that I want to send. It will calculate CRC with the specific polynomial(named pattern in code). Then will return a value which is equal to the initial message to which I append the result of the CRC calculation.
The second function recieves this value and performs the check(by xor'ing it with the polynomial) prints a message in which it lets the user know whether the data was altered or not.(by checking if the final result of 2nd xor is 0)
My polynomial is 0x000B.
This is the first function(one that calculates the CRC pattern and returns it):
Code:
long long int sendMessageAndCreateCRC(long long int message)
{
long long int dataToSend = 0;
long long int pattern = 13; // 0b1101
message = message << 3; // append 3 zeroes at the end(CRC is 0b1011)
long long int aux = message;
int i;
for( i = sizeof(message)*8-1 ; i=0; i--)
{
if( message&(1<<i) ) // if bit from left-right set do xor
{
message = message ^ (pattern<<(i-3)); // ??????????????
}
}
dataToSend = aux | message; // append reminder at the end of data frame
return dataToSend;
}
Code:
void recieveAndCheckRecievedData(long long int recievedData)
{
printf("Result of CRC is: %lld\n", recievedData & 0x0007 ); // printf("%0x", recievedData & 7 );
long long aux = recievedData;
long long int pattern = 13; // 1101
int i;
for( i = sizeof(recievedData)*8-1; i>0; i--)
{
if( recievedData&(1<<i) )
{
recievedData = recievedData ^ (pattern<<(i-3) ); // ?????????????????
}
}
if(recievedData!=0)
{
printf("Data was altered! Resend!\n");
}
else
{
printf("Data ok!\n");
printf("Data is: %lld\n", aux >> 3);
printf("Press any key to continue\n");
}
}
Then when calling the second function I'm expecting to see the result of the CRC(something different than 0) and the message that data is ok and it's value.
Code:
long long int sentMessage;
sentMessage = sendMessageAndCreateCRC(256); // get the value from first function which performs XOR on argument and polynom
recieveAndCheckRecievedData(sentMessage); // and send it to second function for verification
Code:
Result of CRC is: 7
Data was altered. Resend!
Please help me correct this issue if possible. Thank you.
Last edited: