CRC16 CCITT - A plea for help ...

Status
Not open for further replies.

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

Code:
Dim data As Word
Dim crc As Word
Dim bt As Byte
Dim tmp As Word
Dim polynomial As Word

polynomial = 0x1021  '0x1021 -

Hseropen 9600
data = 0x4d  'M 77 1001101

'-----------------------------------------
'This would be a function
'-----------------------------------------
crc = 0xffff

data = ShiftLeft(data, 8)  'shift the data left 8
crc = crc Xor data  'XOR the data with the CRC
For bt = 0 To 7
	tmp = crc And 0x8000  'and the CRC with 0x8000
	If tmp <> 0 Then  'if this vale <> 0 then will be either 8000 or 0
		crc = ShiftLeft(crc, 1)  'shift the CRC left 1
		crc = crc Xor polynomial  'XOR the CRC with polynomial
	Else
		crc = ShiftLeft(crc, 1)  'else just shift the crc left 1
	Endif
Next bt

Hserout "CRC = ", #crc, CrLf  'M = 0x7899 -  or maybe 0x55f5 -

End
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…