PDA

View Full Version : Problems with CRC8 Calc in 1Wire



JohnB
- 14th March 2007, 10:33
I thought I had this all cracked a while ago but on returning to it now I don't seem to be the right answer.

I am using the routine posted by Tom Estes http://www.picbasic.co.uk/forum/showthread.php?t=1672&highlight=1Wire
but when I test it against what I understand to be valid Device IDs I get a CRC failure.

This is my code: Can anyone suggest what I am doing wrong?

I Var Byte
CRC Var Byte
CRCData Var Byte
TestBit Var Bit
OW_Byte_Ptr Var Byte

'' PBP Version not using lookup table
'' Expects CRC starting value and CRCData
'' CRC will be cumulative unless reset externally
'' Gives ending CRC value and destroys CRCData
'' Uses I-Byte, TestBit-Bit, CRC-Byte, CRCData-Byte

CRC8:
For I = 0 To 7 ' Do for all 8 bits in data byte
TestBit = CRC.0 ^ CRCData.0 ' XOR bit0 of data byte and crc
CRCData = CRCData >> 1 ' Position data byte for next bit test
If TestBit = 0 Then Shift ' If test bit not set, just shift CRC
CRC = CRC ^ $18 ' If set, account for EXOR feedback
Shift: ' Shift right the CRC byte
CRC = CRC >> 1 ' CRC bit 0 to bit bucket
CRC.7 = TestBit ' Test bit rotates into CRC bit 7
Next I
Return

'TestCode------------------------------------------------

TestData VAR Byte[8]

' Load up Data array

TestData[0] = $05 '$28 'Family Code
TestData[1] = $C5 '$B1 'Serial Number
TestData[2] = $C3 '$FE
TestData[3] = $08 '$22
TestData[4] = $00 '$00
TestData[5] = $00 '$00
TestData[6] = $00 '$00
TestData[7] = $CD '$5D 'CRC

Main:
For OW_Byte_Ptr = 0 To 7
CRCData = TestData[OW_Byte_Ptr]
GoSub CRC8
HSerOut [HEX2 CRC, 10, 13]
Next

If CRC = 0 Then
HSerOut ["CRC OK",10,13]
Else
HSerOut ["CRC Error ", HEX2 CRC,10,13]
EndIf

Stop

What I am actually getting is
35
FF
4C
7E
D8
3E
8D
B1
CRC Error B1

Where I should be getting 0

Am I missing something?

JohnB

Tom Estes
- 14th March 2007, 15:06
JohnB,

I'm away from my hardware so I can't test your code. However it appears you have not initialized CRC before you test the first byte. Add CRC=0 right after your Main: label before you start to submit bytes to the routine. If that doesn't fix it let me know and when I get home I'll test your software.

Tom Estes

JohnB
- 14th March 2007, 15:34
Tried that - this is the result:

A8
13
06
46
A4
65
76
E5
CRC Error E5


JohnB

Tom Estes
- 14th March 2007, 16:28
You are jumping around the subroutine upon start up right? If your software is verbatum to what you posted then you need to add Goto Start just before the CRC8: label and place a Start: label before your testcode comment. I'm not sure what PBP will do with variables declared after coding starts but I would move the TestData VAR Byte(8) up to before the subroutine where the other declarations are.

Again, I'm away from home for a couple of days so I can't test the code.....

JohnB
- 15th March 2007, 09:48
Can't believe how stupid I have been, indeed it was an extract from a much larger program which I was having a problem with. In the extract I was skipping over loading the array so it was never getting to operate on correct data.

I will now have to go back to my original code and see where it is going wrong as I still have a problem with the real code, it just not with the CRC calc as I h ad originally thought.

Sorry to have bothered you.

JohnB

BigWumpus
- 16th March 2007, 22:01
Yesterday I have implemented the CRC-8-calculating from 1-wire with a short routine without tables from:

http://www.dattalo.com/technical/software/pic/crc.php