SwingeyP
Member
Hello. I am trying to write a CRC16 in OSHON
I was given this code in C (I don't understand c syntax)
Original c code
I have come up with this but it's not working can anyone see what i'm doing wrong please?
I have gone through copying the bits on paper and the code seems to do what it should but the checksum for M according to this site - https://www.lammertbies.nl/comm/info/crc-calculation.html
should be 0x7899.
My RTTY project can not continue without this CRC :-(
Regards - Paul
I was given this code in C (I don't understand c syntax)
Original c code
Code:
unsigned short crc16(unsigned char *buff, int len){
unsigned short crc = 0xFFFF;
while(len){
crc ^= *buff++;
for(int i = 0; i < 8; i++){
if(crc & 1)
crc = (crc >> 1) ^ 0xA001;
else
crc = crc >> 1;
}
len--;
}
return crc;
}
I have come up with this but it's not working can anyone see what i'm doing wrong please?
Code:
Dim ch As Byte
Dim crc As Word
Dim bt As Byte
Dim tmp As Byte
Dim polynomial As Word
polynomial = 0x1021
Hseropen 9600
ch = "M"
'-----------------------------------------
'This would be a function
'-----------------------------------------
crc = 0xffff
'Hserout "ch = ", #ch, CrLf
crc = crc Xor ch 'XOR crc with ch -
'hserout "initial XOR = " #crc, CrLf
For bt = 0 To 8
tmp = crc And 1 'if(crc & 1)
'Hserout "tmp = ", #tmp, CrLf
If tmp = 1 Then 'is this right?
crc = ShiftRight(crc, 1) 'shift to the right
'Hserout "After Shift = ", #crc, CrLf
crc = crc Xor polynomial
'Hserout "After XOR = ", #crc, CrLf, CrLf
Else
crc = ShiftRight(crc, 1)
'Hserout "Just Shift crc = ", #crc, CrLf, CrLf
Endif
Next bt
Hserout "CRC = ", #crc, CrLf 'M = 7899
End
I have gone through copying the bits on paper and the code seems to do what it should but the checksum for M according to this site - https://www.lammertbies.nl/comm/info/crc-calculation.html
should be 0x7899.
My RTTY project can not continue without this CRC :-(
Regards - Paul