• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Performing CRC function. Help needed

Status
Not open for further replies.

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):



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;
}
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.

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 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.

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
However when I run this the second function prints this:

Code:
Result of CRC is: 7
Data was altered. Resend!
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.
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
Your two CRC routines should be identical and the calling code should either add it to the message to send or compare it to the message received.

Mike.
 

DirtyLude

Well-Known Member
^ I would agree. You want one function that calculates your 3 bit CRC and then you can append it to your message or check it against your message. You are giving up readability and modularity of the code to be fancy.

Regardless, if you don't have debug access to check variables and step through this code, you at least have terminal access. Simply put in more printf's to find out what it's calculating and what the CRC check is expecting. It should not be difficult to track back and find what the issue is.

I would suggest moving to stdint data types and removing the sizeof() function. ( int64_t instead of long long int)
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top