It is currently 19 Jul 2019 02:49

All times are UTC + 1 hour




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: CRC for DS1820
PostPosted: 30 Jan 2006 00:41 
Offline

Joined: 24 Nov 2005 01:40
Posts: 280
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:


Top
 Profile  
 
 Post subject:
PostPosted: 17 Jul 2008 19:51 
Offline

Joined: 18 Jun 2008 11:43
Posts: 3779
Location: Nieuwpoort, Belgium
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:
'=======================================================
'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:
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):
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.

_________________
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)


Last edited by Dany on 30 May 2011 10:06, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: 18 Jul 2008 19:37 
Offline

Joined: 19 Sep 2006 12:57
Posts: 1868
Location: Romania, Arad City
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


Top
 Profile  
 
 Post subject:
PostPosted: 02 Oct 2008 11:40 
Offline

Joined: 18 Jun 2008 11:43
Posts: 3779
Location: Nieuwpoort, Belgium
Hi,
The Pascal version has become the following:

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.

_________________
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)


Last edited by Dany on 30 May 2011 10:06, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: 02 Oct 2008 11:56 
Offline

Joined: 19 Sep 2006 12:57
Posts: 1868
Location: Romania, Arad City
I have a questions :

Is this line :

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


Top
 Profile  
 
 Post subject:
PostPosted: 02 Oct 2008 12:39 
Offline

Joined: 18 Feb 2006 13:17
Posts: 5122
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.


Top
 Profile  
 
 Post subject:
PostPosted: 02 Oct 2008 16:21 
Offline

Joined: 18 Jun 2008 11:43
Posts: 3779
Location: Nieuwpoort, Belgium
yo2lio wrote:
I have a questions :
Is this line :
TmpBit := ((Result.0 > 0) <> (Buff[I].J > 0));

a standard PASCAL syntax ????

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


Top
 Profile  
 
 Post subject:
PostPosted: 02 Oct 2008 16:24 
Offline

Joined: 18 Jun 2008 11:43
Posts: 3779
Location: Nieuwpoort, Belgium
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)


Top
 Profile  
 
 Post subject:
PostPosted: 02 Oct 2008 17:51 
Offline

Joined: 18 Feb 2006 13:17
Posts: 5122
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.


Top
 Profile  
 
 Post subject:
PostPosted: 02 Oct 2008 18:00 
Offline

Joined: 18 Feb 2006 13:17
Posts: 5122
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.


Top
 Profile  
 
 Post subject:
PostPosted: 02 Oct 2008 19:05 
Offline

Joined: 18 Jun 2008 11:43
Posts: 3779
Location: Nieuwpoort, Belgium
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)


Top
 Profile  
 
 Post subject: Re: CRC for DS1820
PostPosted: 27 May 2011 12:16 
Offline

Joined: 27 May 2011 12:07
Posts: 2
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)


Top
 Profile  
 
 Post subject: Re: CRC for DS1820
PostPosted: 30 May 2011 12:37 
Offline

Joined: 18 Feb 2006 13:17
Posts: 5122
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
if PORTB.temp7 <> p4int.temp7 then ...
or, better yet,
temp=PORTB xor p4int
if temp.temp7 then ...
resulting in half the final code.

_________________
Replacement libraries for mP/mB PRO, mP PRO tips & trics


Top
 Profile  
 
 Post subject: Re: CRC for DS1820
PostPosted: 31 May 2011 08:57 
Offline

Joined: 27 May 2011 12:07
Posts: 2
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
if PORTB.temp7 <> p4int.temp7 then ...
or, better yet,
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: