CRC-8 with DT's routine ?
Looking for some confirmation that DT's CRC calculation routine will work with CRC-8.
This is two typical samples of what I have incomming from a Fine Offset WH2081 weather station transmitter.
FF AD 42 CB 16 05 06 00 6C 0E 87
FF AD 42 CC 16 05 08 00 6C 0C 0D
FF is a preamble, and the last byte is CRC-8 using the polynomial x^8 + x^5 + x^4 + 1 (which I think becomes the constant $131 for calculations).
The middle 9 bytes are data, and used for the CRC byte sent by the TX.
Do I have it right that DT's code could be used directly if the CRC_Len and CRC_poly constants are changed as below ?
CRC VAR WORD
CRC_IN VAR BYTE
Idx VAR BYTE
CRC_Len CON 8
CRC_Poly CON $131 ; x^8 + x^5 + x^4 + 1
;-----CRC-16 -----------------------------------------------------
CRC16:
CRC.HighByte = CRC.HighByte ^ CRC_IN
FOR Idx = 0 to 7
IF CRC.0(CRC_Len-1) THEN
CRC = (CRC << 1) ^ CRC_Poly
ELSE
CRC = CRC << 1
ENDIF
NEXT Idx
RETURN
If so, is it just a matter of setting the CRC_IN byte to each value of the nine data bytes in turn while looping through CRC16: each time, the wanted result being the value of CRC after the ninth loop ?
Or is there stuff to be done after each result of subroutine CRC16 ?
Thanks if anyone can confirm that for me before I try to hack my receiver code to pieces.
Martin
Re: CRC-8 with DT's routine ?
Confirmation denied!
The 8-bit polynomial for x^8 + x^5 + x^4 + 1 would be $31.
And the routine for 8-bit CRC's is different.
Code:
CRC VAR BYTE
CRC_IDX VAR BYTE
CRC_IN VAR BYTE
CRC_Poly CON $31
CRC8:
FOR CRC_IDX = 0 to 7
IF (CRC.7 ^ CRC_IN.7) THEN
CRC = (CRC << 1) ^ CRC_Poly
ELSE
CRC = CRC << 1
ENDIF
CRC_IN = CRC_IN << 1
NEXT CRC_IDX
RETURN
This test code ...
Code:
X VAR BYTE
CRC = 0
FOR X = 0 TO 8
LOOKUP X,[$AD, $42, $CB, $16, $05, $06, $00, $6C, $0E], CRC_IN
HSEROUT [HEX2 CRC_IN," "]
GOSUB CRC8
NEXT X
HSEROUT ["= ",HEX2 CRC,13,10,13,10]
CRC = 0
FOR X = 0 TO 8
LOOKUP X,[$AD, $42, $CC, $16, $05, $08, $00, $6C, $0C], CRC_IN
HSEROUT [HEX2 CRC_IN," "]
GOSUB CRC8
NEXT X
HSEROUT ["= ",HEX2 CRC,13,10,13,10]
Returns these results ...
Code:
AD 42 CB 16 05 06 00 6C 0E = 87
AD 42 CC 16 05 08 00 6C 0C = 0D
Re: CRC Calculator Routine
denied :eek:
Oh no not again.
I took my best shot at it based on looking at some online CRC calculators and the CRC Wiki and thought it might work for what I wanted.
Now that I've been corrected I'll test the CRC routine when I get back to my coding shortly.
Thanks Darrel,
Martin
Re: CRC Calculator Routine
Hey Darrel,
If you have time, could you show me how you went from a polynomial to the constant used in the code ?
I was using this web based calculator to try to understand it, but I'm missing something, as I can't match your $31.
http://ghsi.de/CRC/index.php?Polynom...06006C0E%0D%0A
Re: CRC Calculator Routine
The highest bit of a polynomial is assumed to be 1, and is not included in the value used in the formula.
It's presence in the polynomial indicates the length of the final CRC result.
With x^8 + x^5 + x^4 + 1 ...
The x^8 indicates that it is an 8-bit CRC, and it's bit position is not used.
From there, you just stick the bits in a value to be used in the formula.
Code:
x^8 + x^5 + x^4 + 1
7 6 5 4 3 2 1 0
------------------------
0 0 1 1 0 0 0 1
-------------------------
$ 3 1
The polynomial for plain CRC16 is x^16 + x^15 + x^2 + x^0
So it's a 16-bit CRC, the x^16 is not used, and the poly value is $8005.
Note that + x^0 is the same as + 1.
The calculator you pointed to forces you to put the highest bit in the constant so it knows the length of the CRC.
Otherwise, it's the same value ... $31.
HTH
Re: CRC Calculator Routine
Hi Darrel,
I can now also confirm your CRC calculation code works well. Finally got back to my weather station receiver coding and added the CRC routines. No problems at all, works as advertised.
Part of the delay was obtaining a bigger PIC to expand my code. Eventually got some PIC16F1509's.
Regards,
Martin
PS. I saw your 'lost youth' sig message above. I think I did see your youth recently, but it was moving farther away from you at the time, and doesn't look like it's going to be coming back any time soon :P
Re: CRC Calculator Routine
Great!
I'm glad it you got it working.
And that's also great you saw my Youth. That means it's still around somewhere.
Maybe I should offer a reward?
http://www.pbpgroup.com/files/SIGIMG/lostyouth.gif