CRC for DS1820

Post your requests and ideas on the future development of mikroBasic.
Post Reply
Author
Message
eludar
Posts: 280
Joined: 24 Nov 2005 01:40

CRC for DS1820

#1 Post by eludar » 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 :cry:

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#2 Post by Dany » 17 Jul 2008 19:51

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:

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
C:

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);
}
Pascal (derived from the basic one above):

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;
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.
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)

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#3 Post by yo2lio » 18 Jul 2008 19:37

Thank you !
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

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#4 Post by Dany » 02 Oct 2008 11:40

Hi,
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;
Reason: previous version did no longer work with mP v8.3 bèta.
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)

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#5 Post by yo2lio » 02 Oct 2008 11:56

I have a questions :

Is this line :

Code: Select all

TmpBit := ((Result.0 > 0) <> (Buff[I].J > 0));
a standard PASCAL syntax ????
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

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

#6 Post by janni » 02 Oct 2008 12:39

Inventive :lol: but may cause trouble with next versions of mP.

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.

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#7 Post by Dany » 02 Oct 2008 16:21

yo2lio wrote:I have a questions :
Is this line :

Code: Select all

TmpBit := ((Result.0 > 0) <> (Buff[I].J > 0));
a standard PASCAL syntax ????
Yes, standard pascal syntax.

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.
Can be no more standard. :D .(TmpBit holds now the exclusive "or" of the two tested bits.)
What I actually wanted to write was:

Code: Select all

TmpBit := Result.0 xor Buff[I].J;
but due to the "bit" handling methods of mP (Buff.J does not give always a "1" for a set bit), i had to use the alternative method.
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. :)
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)

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#8 Post by Dany » 02 Oct 2008 16:24

janni wrote:Inventive :lol: 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: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. :D
True. I will have a look into it. Thanks. :D
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)

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

#9 Post by janni » 02 Oct 2008 17:51

Dany wrote:
janni wrote:Inventive :lol: 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).
Well, judging from newest version of mP for 8051 (command-line compiler), there may be no boolean type (although there are boolean operators there :shock: ). On the other hand there'll be bit types and then it'll be more efficient to change your approach to bit operations.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

#10 Post by janni » 02 Oct 2008 18:00

On second thought, I don't think it will come to that. It would cause too many protests. Maybe this approach for 8051 processors was simply due to RAM savings resulting from these processors architecture.

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#11 Post by Dany » 02 Oct 2008 19:05

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 :shock: ).
That would be indeed a schock. The boolean type is a standard type in Pascal. I hope they do not get rid of it! :cry:
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.
True. We wait and see. :D
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)

datik
Posts: 2
Joined: 27 May 2011 12:07

Re: CRC for DS1820

#12 Post by datik » 27 May 2011 12:16

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)

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: CRC for DS1820

#13 Post by janni » 30 May 2011 12:37

datik 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
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). Use

Code: Select all

if PORTB.temp7 <> p4int.temp7 then ... 
or, better yet,

Code: Select all

temp=PORTB xor p4int
if temp.temp7 then ...
resulting in half the final code.

datik
Posts: 2
Joined: 27 May 2011 12:07

Re: CRC for DS1820

#14 Post by datik » 31 May 2011 08:57

janni wrote:
datik 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
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). Use

Code: Select all

if PORTB.temp7 <> p4int.temp7 then ... 
or, better yet,

Code: Select all

temp=PORTB xor p4int
if temp.temp7 then ...
resulting in half the final code.
Thanks - that helped me to understand it, and get it to work :D

Post Reply

Return to “mikroBasic Wish List”