Helpful ReplyHot!CRC calculation problem

Author
KMDN
Super Member
  • Total Posts : 258
  • Reward points : 0
  • Joined: 2014/03/08 03:40:44
  • Location: 0
  • Status: offline
2017/12/23 10:14:47 (permalink)
0

CRC calculation problem

I am trying to calculate CRC of farm {1,3,4,0,0,0,0,0,0} using give code
 
unsigned int CRC16_2(unsigned char *buf, int len)
{
  int i,pos;
  unsigned int crc = 0xFFFF;
  for (pos = 0; pos < len; pos++)
  {
  crc ^= (unsigned int)buf[pos]; // XOR byte into least sig. byte of crc

  for ( i = 8; i != 0; i--) { // Loop over each bit
    if ((crc & 0x0001) != 0) { // If the LSB is set
      crc >>= 1; // Shift right and XOR 0xA001
      crc ^= 0xA001;
    }
    else // Else LSB is not set
      crc >>= 1; // Just shift right
    }
  }

  return crc;
}
 
 
When I try to insert CRC value in last two byte of array first byte I got correct but last one always zero.
 
CRC16 = CRC16_2(fram,7);
fram[7] = CRC16;
fram[8] = CRC16<<8;
PUT_serial_data_RS(fram);
LATDbits.LATD15 = 0;

 
Can any one help me find out what is wrong in code 
 
CRC16 is unsigned int
#1
NKurzman
A Guy on the Net
  • Total Posts : 15839
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: CRC calculation problem 2017/12/23 10:49:51 (permalink)
0
You are shifting the byte fore fram[8] the wrong way.
#2
aschen0866
Super Member
  • Total Posts : 4290
  • Reward points : 0
  • Joined: 2006/01/08 22:18:32
  • Location: San Diego
  • Status: offline
Re: CRC calculation problem 2017/12/23 10:58:45 (permalink) ☄ Helpfulby KMDN 2017/12/25 05:19:14
0
kushalnandanwar
 
 
fram[8] = CRC16<<8;


I think what you want is CRC16 >> 8.
#3
KMDN
Super Member
  • Total Posts : 258
  • Reward points : 0
  • Joined: 2014/03/08 03:40:44
  • Location: 0
  • Status: offline
Re: CRC calculation problem 2017/12/23 18:33:11 (permalink)
0
unsigned char fram[8] ={1,3,4,0,0,0,0,0,0} ; 

#4
simong123
Lab Member No. 003
  • Total Posts : 1236
  • Reward points : 0
  • Joined: 2012/02/07 18:21:03
  • Location: Future Gadget Lab (UK Branch)
  • Status: offline
Re: CRC calculation problem 2017/12/23 21:14:28 (permalink)
0
kushalnandanwar
unsigned char fram[8] ={1,3,4,0,0,0,0,0,0} ; 


Should be
unsigned char fram[9] ={1,3,4,0,0,0,0,0,0} ; 

#5
KMDN
Super Member
  • Total Posts : 258
  • Reward points : 0
  • Joined: 2014/03/08 03:40:44
  • Location: 0
  • Status: offline
Re: CRC calculation problem 2017/12/24 09:04:39 (permalink)
0
Array start with zero
#6
NKurzman
A Guy on the Net
  • Total Posts : 15839
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: CRC calculation problem 2017/12/24 09:15:44 (permalink)
0
Yes arrays start with 0, and 0 to 8 is 9 members.
#7
qɥb
Monolothic Member
  • Total Posts : 2510
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: CRC calculation problem 2017/12/24 15:53:56 (permalink)
0
kushalnandanwar
Array start with zero

When you create an array, you must specify the number of elements required, not the highest index.
 

PicForum "it just works"
#8
KMDN
Super Member
  • Total Posts : 258
  • Reward points : 0
  • Joined: 2014/03/08 03:40:44
  • Location: 0
  • Status: offline
Re: CRC calculation problem 2017/12/24 20:50:50 (permalink)
0
simong123
kushalnandanwar
unsigned char fram[8] ={1,3,4,0,0,0,0,0,0} ; 


Should be
unsigned char fram[9] ={1,3,4,0,0,0,0,0,0} ; 





 
I am asking why I should be
 
unsigned char fram[9] ={1,3,4,0,0,0,0,0,0} ; 

 
if I need only 9 elements, 0 to 8
post edited by KMDN - 2017/12/24 21:02:13
#9
simong123
Lab Member No. 003
  • Total Posts : 1236
  • Reward points : 0
  • Joined: 2012/02/07 18:21:03
  • Location: Future Gadget Lab (UK Branch)
  • Status: offline
Re: CRC calculation problem 2017/12/24 21:08:08 (permalink)
4 (1)
I refer you to qɥb's previous post.
#10
Jump to:
© 2018 APG vNext Trial Version 4.5