CRC and Decimal, ASCII

Graham O

Member
Hello,
I'm trying to use a bit of code which generates a CRC and it is working correctly provided I input the data in ASCII format, i.e. "A","B","C","1", "2" etc or use the number equivalents, i.e. decimal 1 = 49, 2 = 50 etc.

The main part of the program generates a data string in the scratchpad of type:

$$Daed,100,12:46:20,53.25,-3.25,XXX where XXX is the CRC value.

How do I get the CRC calculation to recognise the text and decimal values and treat them correctly please? I've searched the forums, but I think I've been staring at the problem so long, I'm just going round in circles.

Code:
SYMBOL crc = w0 ' b0/b1
SYMBOL k = w1 ' b2/b3
SYMBOL byte_ = b4
SYMBOL bit_ = b5
symbol word2 = w12

SYMBOL POLYNOMIAL = $1021

crc = $FFFF

put 1,49 'decimal 1 in ASCII
put 2,50
put 3,51

for b14 = 1 to 3

get b14,b15

byte_ = b15 : GOSUB Crc16Add2:

next


crc = crc ^ $0000


END

Crc16Add2:

word2 = byte_ << 8
crc = crc ^ word2

for byte_ = 0 to 7

word2 = crc & 0x8000
if word2 > 0 then
crc = crc << 1
crc = crc ^ 0x1021
else
crc = crc << 1
endif


next


return

I am comparing the result with http://www.lammertbies.nl/comm/info/crc-calculation.html for the 0xFFFF option.

Thanks for any help.
 

westaust55

Moderator
The .bas file in post 41 here contains some CRC16 code I wrote for the maxim 1-Wire DS2406 EPROM memory.
http://www.picaxeforum.co.uk/showthread.php?t=15306&page=5
That used a “polynomial” value of $A001 which is based upon the IBM CRC-16.

The polynomial value of $1021 suggests that your code is based upon the CRC-16 CCITT as used in XModem and Bluetooth applications.

Notwithstanding the difference, the same basic code can be used. I recall that somewhere in the same thread as linked above, hippy provided an alternative piece of PICAXE code. May have been for Maxim CRC-8 but again the concepts are the same.
 
Top