Continue to Site

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.

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

Crc16

Status
Not open for further replies.

SwingeyP

Member
Hello again.

Does anyone have any routines for crc16 checksum?

CRC-CCITT 0x1021 x16 + x12 + x5 + 1

I have no idea where to even start.

Regards - Paul


Code:
Dim ch(45) As Byte
Dim chr As Byte
Dim i As Byte
Hseropen 9600

For i = 0 To 41
	ch(i) = LookUp("M0TVU,n,192633.0,5233.736,00153.799,00181"), i
Next i

'check we have the string

For chr = 0 To i
	Hserout ch(chr)
Next chr

'now create a value which is the crc_16 checksum


'now add this value to the end of the string


'now output the whole string.
 
Last edited:
There are plenty of implementations, here's a couple I have used (the first updates calculates the crc from a character array, the second updates an existing crc with a single character):

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

Code:
void cPacketSerial::calcCRC16(char data, short &crc){
   *(((char*)&crc)+1) ^= data;

   for(char i = 0; i < 8; i++)
      if(crc & 0x8000)
         crc = (crc << 1) ^ 0x1021;
      else
         crc <<= 1;
   }

You could also look here: https://www.ccsinfo.com/forum/viewtopic.php?t=24977

Most algorithms you will find will likely be in C or C++; you'll have to translate to your BASIC variant.
 
I don't understand the C syntax. I found lots of c implementations but I don't understand ^= *buff++ or (crc >> 1) ^ 0xa001; - what is this doing? Can you comment the code or represent it in oshon?
 
The statement: "crc ^= *buff++" is saying something like:
crc = crc XOR buff
i = i + 1

The statement "crc = (crc >> 1) ^ 0xa001" is saying
crc = ShiftRight(crc, 1) XOR &HA001
where &H#### is a hexadecimal number
 
ok so something like

Code:
Dim ch(45) As Byte
Dim chr As Byte
Dim i As Byte
Hseropen 9600
 
For i = 0 To 41
	ch(i) = LookUp("M0TVU,n,192633.0,5233.736,00153.799,00181"), i
Next i
 
'check we have the string
 
For chr = 0 To i
	Hserout ch(chr)
Next chr
 
'now create a value which is the crc_16 checksum
 for j= 0 to i ' loop around the array length is as was stored earlier
crc=crc xor ch(j)
if(crc & 1) then ' i'll need to oshon this
    crc = ShiftRight(crc, 1) XOR &HA001 'what number is it xor'd with? should this be crc=crc + .......
      else
       crc = ShiftRight(crc, 1) 'should this be crc = crc + .......
next i

 
'now add this value to the end of the string
 
 
'now output the whole string.

Im not infront of the oshon compiler at present so just typed what looked right.

I'll have a play again later have to pop out now...
 
Ok I now have this.

I'm not exactly sure what the code is doing

Can anyone help me out please?

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

OSHON version


Code:
Dim ch(45) As Byte
Dim crc As Word
Dim bt As Byte


Hseropen 9600


ch = "M"


'-----------------------------------------
'This would be a function
'-----------------------------------------
crc = 0xffff
Hserout #ch, CrLf

crc = crc Xor ch  'XOR crc with ch - 1111111111111111 XOR 0100110100000000
Hserout #crc, CrLf
	
For bt = 0 To 8
	crc = crc And 1  'if(crc & 1)
	If crc.0 = 1 Then  'is this right?
		crc = ShiftRight(crc, 1)  'shift to the right
		crc = crc Xor 0xa001
		Hserout #crc, CrLf

	Else
		crc = ShiftRight(crc, 1)
	Endif
Next bt

Hserout #crc

End

As you can imagine it doesn't seem to do anything. Can anyone explain what is supposed to be going on please?

Regards - Paul
 
This is close - I think


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 "AND = ", #crc, CrLf
	If tmp = 1 Then  'is this right?
		crc = ShiftRight(crc, 1)  'shift to the right
		crc = crc Xor polynomial
	Else
		crc = ShiftRight(crc, 1)
	Endif
Next bt

Hserout "CRC = ", #crc  'M = 7899

End
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top