As a follow-up to the script for the 16 bit crc, here is a script for the Dallas/Maxim 8 bit crc. The sample data & crc comes from a DS1822 1-wire digital thermometer. I use the code in a slightly modified format as a function in a larger program, with data and transmitted crc passed to the function, which returns 'OK' or 'Error' back to the main program.

#!/usr/bin/perl # # Script to calculate the Dallas/Maxim 8 bit crc # The data block is 8 bytes and the crc is 1 byte # Sample data is from a DS1822 scratchpad read # # The script compares a calculated 8 bit crc with the # transmitted crc # # LS = least significant # MS = most significant # XOR = bitwise exclusive OR # use strict; # # variables my ($data, $crc8); # input data & input crc my $crc; # calculated crc my $test; # initial XOR of bit 1 of data & crc my ($byte, $bit); # each byte of data & each bit of byte my ($i, $n); # loop counters # ########################################################### # Sample data (8 bytes in hex) $data = "4E014B467FFF0210"; # Sample crc (correct crc for the sample data above) $crc8 = "D9"; ########################################################### # # set initial crc for calculation (8 bits all zero) $crc = 0b00000000; # # loop through data bytes for ($i = 0; $i <= 14; $i+=2) { #convert pairs of hex digits into number $byte = hex(substr $data, $i, 2); # sub-loop to read 8 bits from byte for ($n = 0; $n <= 7; $n++) { # # get LSbit of byte by applying mask $bit = $byte & 0b00000001; # # XOR the LS bit of the crc with # the current data bit $test = $crc ^ $bit; # apply mask to get LS bit only $test = $test & 0b00000001; # # test if result was 1 or 0 if ($test) { # result was 1 so need to XOR, right shift # & make MS bit 1 # # XOR crc with 0b00011000 $crc = $crc ^ 0b00011000; # right shift $crc = $crc >> 1; # make MS bit 1 by OR'ing with 0b10000000 $crc = $crc | 0b10000000; } else { # result was zero, so just right shift crc # (MS bit will be 0) $crc = $crc >> 1; } # shift data byte to right so next bit # is in LS bit position $byte = ( $byte >> 1 ); } } # # compare calculated crc to transmitted crc if ($crc == hex($crc8)) { print "Result ". sprintf("%X",$crc) . " is correct\n"; } else { print "Result " . sprintf("%X",$crc) . " does not match the trans +mitted crc:" . sprintf("%02X",$crc8) . "\n"; } # exit 0;

In reply to Re^2: Dallas/Maxim crc by anita2R
in thread Dallas/Maxim crc by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.