CRC for DS1820
Posted: 30 Jan 2006 00:41
CRC recalculation for DS1820 would be nice, to we know it is information valid. I was try to understand how to do this,
but it is to complicated for me
but it is to complicated for me
Code: Select all
'=======================================================
'CRC8 without table
'All you have to do is to initialise the crc variable (to zero or $FFFF)
' and then call the procedure for every byte of your data.
'=======================================================
sub procedure ByteCRC(dim byref CRC as byte,dim CRCData as byte)
dim i,TstBit as byte
For i = 0 to 7 ' Do for all 8 bits in data byte
TstBit = CRC xor CRCData 'CRC.0 xor CRCData.0 ' XOR bit0 of data byte and crc
TstBit = TestBit(TstBit,0)
CRCData = CRCData >> 1 ' Position data byte for next bit test
If TstBit = 1 then ' If test bit not set, just shift CRC
CRC = CRC xor $18 ' If set, account for EXOR feedback
end if ' Shift right the CRC byte
CRC = CRC >> 1 ' CRC bit 0 to bit bucket
CRC.7 = TstBit ' Test bit rotates into CRC bit 7
Next i
end sub
Code: Select all
byte calc_crc(byte buff[], byte num_vals)
{
byte shift_reg=0, data_bit, sr_lsb, fb_bit, i, j;
for (i=0; i<num_vals; i++) /* for each byte */
{
for(j=0; j<8; j++) /* for each bit */
{
data_bit = (buff[i]>>j)&0x01;
sr_lsb = shift_reg & 0x01;
fb_bit = (data_bit ^ sr_lsb) & 0x01;
shift_reg = shift_reg >> 1;
if (fb_bit)
{
shift_reg = shift_reg ^ 0x8c;
}
}
}
return(shift_reg);
}
Code: Select all
function CalcCrc: byte;
var I, J: byte;
TmpBit: boolean;
begin
Result := 0; // temporary CRC
for I := 0 to 8 do // for each byte
begin
for J := 0 to 7 do // for each bit
begin
TmpBit := Result.0 xor Buff[I].J; // XOR bit0 of data byte and temporary crc
Result := Result shr 1; // Shift right the temporary CRC byte
if TmpBit then Result := Result xor $8c; // If set, account for EXOR feedback
end;
end;
end;
Code: Select all
function CalcCrc: byte;
var I, J: byte;
TmpBit: boolean;
begin
Result := 0; // temporary CRC
for I := 0 to 8 do // for each byte
begin
for J := 0 to 7 do // for each bit
begin
TmpBit := ((Result.0 > 0) <> (Buff[I].J > 0)); // XOR bit0 of data byte and temporary crc
Result := Result shr 1; // Shift right the temporary CRC byte
if TmpBit then Result := Result xor $8c; // If set, account for EXOR feedback
end;
end;
end;
Code: Select all
TmpBit := ((Result.0 > 0) <> (Buff[I].J > 0));
Yes, standard pascal syntax.yo2lio wrote:I have a questions :
Is this line :a standard PASCAL syntax ????Code: Select all
TmpBit := ((Result.0 > 0) <> (Buff[I].J > 0));
Code: Select all
"(Result.0 > 0)" gives a boolean as is "(Buff[I].J > 0)",
"boolean <> boolean" gives again a boolean, and finally
"TmpBit := boolean" places the result in a boolean variable.
Code: Select all
TmpBit := Result.0 xor Buff[I].J;
I do not think so, even if bittests start to return "1" and "0" it should still work.but may cause trouble with next versions of mP
I do not think so, even if bittests start to return "1" and "0" it should still work (see previous post with some explanation about the code used).janni wrote:Inventive but may cause trouble with next versions of mP.
True. I will have a look into it. Thanks.janni wrote:The DS sensors have two structures for which CRC is generated, so keeping the structure-size parameter from C example would make the procedure more universal.
Well, judging from newest version of mP for 8051 (command-line compiler), there may be no boolean type (although there are boolean operators there ). On the other hand there'll be bit types and then it'll be more efficient to change your approach to bit operations.Dany wrote:I do not think so, even if bittests start to return "1" and "0" it should still work (see previous post with some explanation about the code used).janni wrote:Inventive but may cause trouble with next versions of mP.
That would be indeed a schock. The boolean type is a standard type in Pascal. I hope they do not get rid of it!janni wrote:Well, judging from newest version of mP for 8051 (command-line compiler), there may be no boolean type (although there are boolean operators there ).
True. We wait and see.janni wrote:On the other hand there'll be bit types and then it'll be more efficient to change your approach to bit operations.
You're mixing bit logic with booleans - first part of the expression resuts in 0 or 1, while the second in 0 and 255 (boolean false and true). Usedatik wrote:I can't get the following to work :
temp7 := 4 ;
if (testbit(PORTB, temp7) <> (p4int.temp7 > 0)) then // If this port changed, start counting
Code: Select all
if PORTB.temp7 <> p4int.temp7 then ...
Code: Select all
temp=PORTB xor p4int
if temp.temp7 then ...
Thanks - that helped me to understand it, and get it to workjanni wrote:You're mixing bit logic with booleans - first part of the expression resuts in 0 or 1, while the second in 0 and 255 (boolean false and true). Usedatik wrote:I can't get the following to work :
temp7 := 4 ;
if (testbit(PORTB, temp7) <> (p4int.temp7 > 0)) then // If this port changed, start countingor, better yet,Code: Select all
if PORTB.temp7 <> p4int.temp7 then ...
resulting in half the final code.Code: Select all
temp=PORTB xor p4int if temp.temp7 then ...