Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 17:11 11 May 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : CRC16 Checksum MMBasic

Author Message
edu001
Regular Member

Joined: 14/07/2014
Location: United Kingdom
Posts: 82
Posted: 11:51pm 22 Jul 2014
Copy link to clipboard 
Print this post

Hi

I need to to a CRC16 Checksum calculation on my output string in MMBasic

Can someone point me in the right direction, I have searched and cannot find any reference to this at all

Thanks
 
halldave

Senior Member

Joined: 04/05/2014
Location: Australia
Posts: 121
Posted: 12:28am 23 Jul 2014
Copy link to clipboard 
Print this post

ok found some code needs to be converted



DEFINT A-Z
DECLARE SUB CRC16 (Byte%)
DIM SHARED TableLow(0 TO 255)
DIM SHARED TableHi(0 TO 255)
CLS
FOR I = 0 TO 255 ' Initialize the lookup tables
READ TableLow(I)
NEXT I
FOR I = 0 TO 255
READ TableHi(I)
NEXT I
' ***************************
' just a few lines for test
' ***************************
Lo = 0: Hi = 0 'Initialize CRC to 00 00 (start new)
T! = TIMER
FOR Byte = 1 TO 30000 'Calc CRC for 30000 characters
Char = Byte MOD 256 'Get character
CRC16 Char 'Calc into CRC
NEXT Byte
PRINT TIMER - T! 'Print how long it took
' Were done Hi & Lo contain the result
' Hi = High byte of 16 bit CRC
' Lo = Low byte of 16 bit CRC
END
DATA &H00,&HC1,&H81,&H40,&H01,&HC0,&H80,&H41
DATA &H01,&HC0,&H80,&H41,&H00,&HC1,&H81,&H40
DATA &H01,&HC0,&H80,&H41,&H00,&HC1,&H81,&H40
DATA &H00,&HC1,&H81,&H40,&H01,&HC0,&H80,&H41
DATA &H01,&HC0,&H80,&H41,&H00,&HC1,&H81,&H40
DATA &H00,&HC1,&H81,&H40,&H01,&HC0,&H80,&H41
DATA &H00,&HC1,&H81,&H40,&H01,&HC0,&H80,&H41
DATA &H01,&HC0,&H80,&H41,&H00,&HC1,&H81,&H40
DATA &H01,&HC0,&H80,&H41,&H00,&HC1,&H81,&H40
DATA &H00,&HC1,&H81,&H40,&H01,&HC0,&H80,&H41
DATA &H00,&HC1,&H81,&H40,&H01,&HC0,&H80,&H41
DATA &H01,&HC0,&H80,&H41,&H00,&HC1,&H81,&H40
DATA &H00,&HC1,&H81,&H40,&H01,&HC0,&H80,&H41
DATA &H01,&HC0,&H80,&H41,&H00,&HC1,&H81,&H40
DATA &H01,&HC0,&H80,&H41,&H00,&HC1,&H81,&H40
DATA &H00,&HC1,&H81,&H40,&H01,&HC0,&H80,&H41
DATA &H01,&HC0,&H80,&H41,&H00,&HC1,&H81,&H40
DATA &H00,&HC1,&H81,&H40,&H01,&HC0,&H80,&H41
DATA &H00,&HC1,&H81,&H40,&H01,&HC0,&H80,&H41
DATA &H01,&HC0,&H80,&H41,&H00,&HC1,&H81,&H40
DATA &H00,&HC1,&H81,&H40,&H01,&HC0,&H80,&H41
DATA &H01,&HC0,&H80,&H41,&H00,&HC1,&H81,&H40
DATA &H01,&HC0,&H80,&H41,&H00,&HC1,&H81,&H40
DATA &H00,&HC1,&H81,&H40,&H01,&HC0,&H80,&H41
DATA &H00,&HC1,&H81,&H40,&H01,&HC0,&H80,&H41
DATA &H01,&HC0,&H80,&H41,&H00,&HC1,&H81,&H40
DATA &H01,&HC0,&H80,&H41,&H00,&HC1,&H81,&H40
DATA &H00,&HC1,&H81,&H40,&H01,&HC0,&H80,&H41
DATA &H01,&HC0,&H80,&H41,&H00,&HC1,&H81,&H40
DATA &H00,&HC1,&H81,&H40,&H01,&HC0,&H80,&H41
DATA &H00,&HC1,&H81,&H40,&H01,&HC0,&H80,&H41
DATA &H01,&HC0,&H80,&H41,&H00,&HC1,&H81,&H40
'
DATA &H00,&HC0,&HC1,&H01,&HC3,&H03,&H02,&HC2
DATA &HC6,&H06,&H07,&HC7,&H05,&HC5,&HC4,&H04
DATA &HCC,&H0C,&H0D,&HCD,&H0F,&HCF,&HCE,&H0E
DATA &H0A,&HCA,&HCB,&H0B,&HC9,&H09,&H08,&HC8
DATA &HD8,&H18,&H19,&HD9,&H1B,&HDB,&HDA,&H1A
DATA &H1E,&HDE,&HDF,&H1F,&HDD,&H1D,&H1C,&HDC
DATA &H14,&HD4,&HD5,&H15,&HD7,&H17,&H16,&HD6
DATA &HD2,&H12,&H13,&HD3,&H11,&HD1,&HD0,&H10
DATA &HF0,&H30,&H31,&HF1,&H33,&HF3,&HF2,&H32
DATA &H36,&HF6,&HF7,&H37,&HF5,&H35,&H34,&HF4
DATA &H3C,&HFC,&HFD,&H3D,&HFF,&H3F,&H3E,&HFE
DATA &HFA,&H3A,&H3B,&HFB,&H39,&HF9,&HF8,&H38
DATA &H28,&HE8,&HE9,&H29,&HEB,&H2B,&H2A,&HEA
DATA &HEE,&H2E,&H2F,&HEF,&H2D,&HED,&HEC,&H2C
DATA &HE4,&H24,&H25,&HE5,&H27,&HE7,&HE6,&H26
DATA &H22,&HE2,&HE3,&H23,&HE1,&H21,&H20,&HE0
DATA &HA0,&H60,&H61,&HA1,&H63,&HA3,&HA2,&H62
DATA &H66,&HA6,&HA7,&H67,&HA5,&H65,&H64,&HA4
DATA &H6C,&HAC,&HAD,&H6D,&HAF,&H6F,&H6E,&HAE
DATA &HAA,&H6A,&H6B,&HAB,&H69,&HA9,&HA8,&H68
DATA &H78,&HB8,&HB9,&H79,&HBB,&H7B,&H7A,&HBA
DATA &HBE,&H7E,&H7F,&HBF,&H7D,&HBD,&HBC,&H7C
DATA &HB4,&H74,&H75,&HB5,&H77,&HB7,&HB6,&H76
DATA &H72,&HB2,&HB3,&H73,&HB1,&H71,&H70,&HB0
DATA &H50,&H90,&H91,&H51,&H93,&H53,&H52,&H92
DATA &H96,&H56,&H57,&H97,&H55,&H95,&H94,&H54
DATA &H9C,&H5C,&H5D,&H9D,&H5F,&H9F,&H9E,&H5E
DATA &H5A,&H9A,&H9B,&H5B,&H99,&H59,&H58,&H98
DATA &H88,&H48,&H49,&H89,&H4B,&H8B,&H8A,&H4A
DATA &H4E,&H8E,&H8F,&H4F,&H8D,&H4D,&H4C,&H8C
DATA &H44,&H84,&H85,&H45,&H87,&H47,&H46,&H86
DATA &H82,&H42,&H43,&H83,&H41,&H81,&H80,&H40
'
SUB CRC16 (Byte)
SHARED Hi, Lo
Temp = Byte XOR Lo
Lo = TableLow(Temp) XOR Hi
Hi = TableHi(Temp)
END SUB

Edited by halldave 2014-07-24
 
edu001
Regular Member

Joined: 14/07/2014
Location: United Kingdom
Posts: 82
Posted: 07:24pm 23 Jul 2014
Copy link to clipboard 
Print this post

Thanks for the reply I really did not want to use tables that method is quite slow, Im sure someone must have done this
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6220
Posted: 08:56pm 23 Jul 2014
Copy link to clipboard 
Print this post

The table method is ofter a lot quicker than calculations.

This is a method I have used (but not in MM Basic).

Procedure.l calcCRC(*LineBuffer, PktSize.l)
Protected i.l, j.l, CRC.l
CRC = 0
For i = 0 To PktSize-1
CRC= CRC ! (PeekA(*LineBuffer+i) << 8)
For j = 1 To 8
If (CRC & $8000)
CRC = (CRC << 1) ! $1021
Else
CRC = CRC << 1
EndIf
CRC = CRC & $FFFF
Next j
Next i
ProcedureReturn CRC
EndProcedure


One big problem is there are a number of CRC16 methods to chose from. You do need an example string and CRC that your device needs to make sure that you are using the correct one.

Jim

VK7JH
MMedit
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3998
Posted: 09:24pm 23 Jul 2014
Copy link to clipboard 
Print this post

A table is usually just to speed things up - typical trade-off speed vs space.

If speed doesn't matter then a short loop is usually the alternative (but may be too slow in MMBasic).

IIRC, there are multiple 16-bit CRCs but only one known as CRC-16.
(edit: haha, not as such! see wikipedia etc)

Be sure to get the initial value right (often all 1s or 0s) as all others flow from it.

JohnEdited by JohnS 2014-07-25
 
edu001
Regular Member

Joined: 14/07/2014
Location: United Kingdom
Posts: 82
Posted: 10:12pm 23 Jul 2014
Copy link to clipboard 
Print this post

Thanks for the reply's the one I am after is CRC16_CCITT

TassyJim what did you use that in
 
Frank N. Furter
Guru

Joined: 28/05/2012
Location: Germany
Posts: 936
Posted: 11:56pm 23 Jul 2014
Copy link to clipboard 
Print this post

I need a CRC-Check too!

I found this code in an old thread:

Function CalcolaCRC (buf() As Byte, Nbyte As Integer) As Integer
Dim i As Integer
Dim j As Integer
Dim wCrc As Integer
Const CRC_PRESET = &HFFFF
Const CRC_POLYNOM = &H8408

wCrc = CRC_PRESET
For i = 0 To Nbyte - 1
wCrc = wCrc Xor buf(i)
For j = 0 To 7
If wCrc And 1 Then
wCrc = ((Int(wCrc / 2) And &H7FFF) Xor CRC_POLYNOM)
Else
wCrc = Int(wCrc / 2) And &H7FFF
End If
Next
Next
CalcolaCRC = wCrc
End Function


Can anybody test if this code works on Maximite/Micromite? I think it needs only a small adaption. (I am not able to check if this code works correct...)

Thanks!

Frank
 
memberx
Newbie

Joined: 20/04/2012
Location: Australia
Posts: 24
Posted: 12:47am 24 Jul 2014
Copy link to clipboard 
Print this post

I have a SUB code for 16 Bit CRC calculation that returns CRC as Low and High Byte.
Tested on Maximite for specific application.

What is your application?
 
Frank N. Furter
Guru

Joined: 28/05/2012
Location: Germany
Posts: 936
Posted: 03:17am 24 Jul 2014
Copy link to clipboard 
Print this post

I want to use the µMite as I2C-slave and must guarantee that the transmitted data is not corrupt.

Frank
 
edu001
Regular Member

Joined: 14/07/2014
Location: United Kingdom
Posts: 82
Posted: 04:46am 25 Jul 2014
Copy link to clipboard 
Print this post

memberx

Could you could pm me regarding your code

Thanks
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025