CRC for DS1820
CRC for DS1820
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
Hi,
I also had the same wish, but then for MicroPascal. I assume mE did not implement this in their libraries. Finally I found (on the web, in this forum or others) a routine for MicroC, for MicroBasic and I derived one myself for MicroPascal.
To help those struggling (as I did) with CRC computation, here they are:
Basic:
C:
Pascal (derived from the basic one above):
p.s. The Pascal code assumes the read in data of the DS1820 to be present in global variable Buff[0..8]. You can of course make a parameter for this.
I also had the same wish, but then for MicroPascal. I assume mE did not implement this in their libraries. Finally I found (on the web, in this forum or others) a routine for MicroC, for MicroBasic and I derived one myself for MicroPascal.
To help those struggling (as I did) with CRC computation, here they are:
Basic:
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;
Last edited by Dany on 30 May 2011 10:06, edited 1 time in total.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)
Hi,
The Pascal version has become the following:
Reason: previous version did no longer work with mP v8.3 bèta.
The Pascal version has become the following:
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;
Last edited by Dany on 30 May 2011 10:06, edited 1 time in total.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)
I have a questions :
Is this line :
a standard PASCAL syntax ????
Is this line :
Code: Select all
TmpBit := ((Result.0 > 0) <> (Buff[I].J > 0));
Best regards, Florin Andrei Medrea.
http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/
mail : florin@microelemente.ro
http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/
mail : florin@microelemente.ro
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.
What I actually wanted to write was:
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
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)
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.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)
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.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)
Re: CRC for DS1820
beeing aware this is an old line i try to ask here, because its the only thing i found regarding my trouble
I can't get the following to work :
temp7 := 4 ;
if (testbit(PORTB, temp7) <> (p4int.temp7 > 0)) then // If this port changed, start counting
p4int is a declared varible of byte.
What i want is making an XOR of the bit of PORTB and of p4int which is pointed at, by temp7
(testing for what bit is causing a port change interrupt on port B 4..7)
I can't get the following to work :
temp7 := 4 ;
if (testbit(PORTB, temp7) <> (p4int.temp7 > 0)) then // If this port changed, start counting
p4int is a declared varible of byte.
What i want is making an XOR of the bit of PORTB and of p4int which is pointed at, by temp7
(testing for what bit is causing a port change interrupt on port B 4..7)
Re: CRC for DS1820
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 ...
Re: CRC for DS1820
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 ...