Shop OBEX P1 Docs P2 Docs Learn Events
CRC code for DS1820 — Parallax Forums

CRC code for DS1820

ArchiverArchiver Posts: 46,084
edited 2004-01-02 18:05 in General Discussion
Does anyone have a PBASIC routine to check/verify the scratch pad ram
CRC code for the DS1820?
Thanx,
-Barry

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2004-01-02 18:05
    Barry-

    The program below will read the Serial ID and scratchpad for DS18S20,
    plus calculate and verify the CRC. The CRC code implements the
    standard Dallas Semi 8-bit CRC algorithm. Should work fine for the
    '1820 as well.

    Regards,

    Steve

    '{$STAMP BS2p}

    id VAR BYTE(9)
    i VAR BYTE
    j VAR BYTE
    k VAR BYTE
    crc VAR BYTE
    feedback VAR BIT

    ' display serial ID, CRC results
    OWOUT 8,1,[noparse][[/noparse]$33]
    OWIN 8,0,[noparse][[/noparse]STR id\8]
    DEBUG "Serial ID: "
    FOR i = 0 TO 7
    DEBUG HEX2 id(i), " "
    NEXT

    crc = 0
    FOR i = 0 TO 7
    j = id(i): GOSUB addToCRC
    NEXT
    DEBUG CR,CR

    ' display scratchpad contents, CRC results
    OWOUT 8,1,[noparse][[/noparse]$CC,$BE]
    OWIN 8,0,[noparse][[/noparse]STR id\9]
    DEBUG "Scratchpad: "
    FOR i = 0 TO 8
    DEBUG HEX2 id(i)," "
    NEXT

    crc = 0
    FOR i = 0 TO 8
    j = id(i): GOSUB addToCRC
    NEXT
    DEBUG CR,CR

    STOP

    addToCRC:
    DEBUG CR,HEX2 j," "
    FOR k = 0 TO 7
    feedback = crc.BIT0 ^ j.BIT0
    crc = crc >> 1 + (feedback*$80) ^ (feedback*$0C)
    j = j >> 1
    NEXT
    DEBUG HEX2 crc," "
    RETURN


    On 2 Jan 04 at 17:20, prius_lover2002 wrote:

    > Does anyone have a PBASIC routine to check/verify the scratch pad
    > ram CRC code for the DS1820? Thanx, -Barry
Sign In or Register to comment.