Shop OBEX P1 Docs P2 Docs Learn Events
CRC and the stamp — Parallax Forums

CRC and the stamp

ArchiverArchiver Posts: 46,084
edited 2004-07-21 22:31 in General Discussion
Just wondering if anyone has tried to implement a CRC using the
stamp? I am using a 2sx. Is it capable of the math? I have N&V's
article on it but would like to find an example.
I have a wireless application working (remote relay actuator) but
would like to improve error correction/rejection.
Thanks
Ian

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2004-07-21 14:35
    This subject has come up a couple times, and was recently on the Javelin
    Stamp list as well. Here's a bit of code I used in a Stamp-to-PC
    program:

    Calc_CRC:
    crc = (crc & $FF00) | (aByte ^ (crc & $FF))
    FOR idx = 0 TO 7
    IF (crc.BIT0 = 0) THEN
    crc = crc >> 1
    ELSE
    crc = (crc >> 1) ^ $A001
    ENDIF
    NEXT
    RETURN


    The crc value is initialized to $0000 before looping through the bytes
    in the package, sending them one at a time to the Calc_CRC routine.

    I'll post the other demo in the file section as that algorithm was
    described and provided with a small packet and expected CRC (and yes,
    the PBASIC implementation works).

    -- Jon Williams
    -- Applications Engineer, Parallax
    -- Dallas Office




    Original Message
    From: iphillipsca [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=2xvQia5ksFZn_Br3ocil3WSH-q4p1VHwjXDNuenCDwPNLE4aaG__7A868EyXykaQBw0kdfqDh7regO7Ncg]iphillips@s...[/url
    Sent: Wednesday, July 21, 2004 8:05 AM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] CRC and the stamp


    Just wondering if anyone has tried to implement a CRC using the
    stamp? I am using a 2sx. Is it capable of the math? I have N&V's
    article on it but would like to find an example.
    I have a wireless application working (remote relay actuator) but
    would like to improve error correction/rejection.
    Thanks
    Ian



    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject
    and Body of the message will be ignored.

    Yahoo! Groups Links







    This message has been scanned by WebShield. Please report SPAM to
    abuse@p....
  • ArchiverArchiver Posts: 46,084
    edited 2004-07-21 14:48
    Thank you, much appreciated. Once again the support and talent of
    Parallax shines through.

    Ian
  • ArchiverArchiver Posts: 46,084
    edited 2004-07-21 19:47
    Thanks again Jon for the info. I have been studying the code you posted
    and now a couple of questions.
    Will this work the same if I just replace the TstStream data with my
    byte to be sent? The code you posted would be just used to decode a sent
    byte correct? To encode the crc I would have to alter the Subroutine, I
    think.
    I am using a 165 shift register to read switches and send to another 2sx
    and if the byte is true, output a HIGH to a pin.
    Sorry to drag on about this. I am starting to get the programming stuff
    down, but I was never that good at math.

    Ian
  • ArchiverArchiver Posts: 46,084
    edited 2004-07-21 20:19
    I can't really answer details about the algorithm, I just implemented
    the "rules" as were presented by a customer. The input he provided
    resulted in the proper output, which should prove the CRC generation
    subroutine.

    You do not need to alter the subroutine.

    What you will do is initialize the CRC, then, one byte at a time, send
    your "packet" to the subroutine. At the end you'll have a two-byte CRC
    value that you can append to your packet to be sent. You receiver Stamp
    will take the packet and run all but the last two bytes (appended CRC)
    through the same subroutine. It will then compare its local CRC value
    with the one appended to the packet. If the values match then there was
    no problem. If they don't match, your protocol needs some method of
    alerting the transmitter to resend the packet.

    And I don't quite understand your application. If you're only sending
    one byte from master to slave, you don't need this much complication.
    You could do something as simple as this and probably live happily with
    it.


    GOSUB Get_165 ' returns myByte
    checkByte = ~myByte
    SEROUT pin, baud, [noparse][[/noparse]myByte, checkByte]

    Then on the slave side:

    SERIN pin, baud, [noparse][[/noparse]theByte, checkByte]
    myCheck = ~theByte
    IF (myCheck = checkByte) THEN
    ' put theByte on P0 - P7
    OUTL = theByte
    ELSE
    ' deal with transmission error
    END


    I hope this helps.

    -- Jon Williams
    -- Applications Engineer, Parallax
    -- Dallas Office



    Original Message
    From: Ian Phillips [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=xPVTCmyqZDLJe9VnaOojCxJnWDgy96DmCcHYsDPjDhCsOEPEKM1c38DnL6P3cyWZTEY_g6qtsyOS8Q6JDSM]iphillips@s...[/url
    Sent: Wednesday, July 21, 2004 1:48 PM
    To: basicstamps@yahoogroups.com
    Subject: RE: [noparse][[/noparse]basicstamps] CRC and the stamp


    Thanks again Jon for the info. I have been studying the code you posted
    and now a couple of questions. Will this work the same if I just replace
    the TstStream data with my byte to be sent? The code you posted would be
    just used to decode a sent byte correct? To encode the crc I would have
    to alter the Subroutine, I think. I am using a 165 shift register to
    read switches and send to another 2sx and if the byte is true, output a
    HIGH to a pin. Sorry to drag on about this. I am starting to get the
    programming stuff down, but I was never that good at math.

    Ian
  • ArchiverArchiver Posts: 46,084
    edited 2004-07-21 20:54
    You're right! There I go again making things too complicated.
    The application is a bomb bay door type of newspaper dropper.
    The unit is on chain motors and they (proverbial) don't want any wires
    running to it. Thus the wireless and then error concerns.
    I'll try the simpler version but keep the crc for future projects.
    Thanks again.

    Ian

    Original Message
    From: Jon Williams [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=vHosg8AddGYOmbxjuChWy_8HrqCXWq_giNVYTKtrHaEUJrocsG13z_WuEACMfjzobqbkfKgtHSLH6ImfBK3b]jwilliams@p...[/url
    Sent: July 21, 2004 3:19 PM
    To: basicstamps@yahoogroups.com
    Subject: RE: [noparse][[/noparse]basicstamps] CRC and the stamp

    I can't really answer details about the algorithm, I just implemented
    the "rules" as were presented by a customer. The input he provided
    resulted in the proper output, which should prove the CRC generation
    subroutine.

    You do not need to alter the subroutine.

    What you will do is initialize the CRC, then, one byte at a time, send
    your "packet" to the subroutine. At the end you'll have a two-byte CRC
    value that you can append to your packet to be sent. You receiver Stamp
    will take the packet and run all but the last two bytes (appended CRC)
    through the same subroutine. It will then compare its local CRC value
    with the one appended to the packet. If the values match then there was
    no problem. If they don't match, your protocol needs some method of
    alerting the transmitter to resend the packet.

    And I don't quite understand your application. If you're only sending
    one byte from master to slave, you don't need this much complication.
    You could do something as simple as this and probably live happily with
    it.


    GOSUB Get_165 ' returns myByte
    checkByte = ~myByte
    SEROUT pin, baud, [noparse][[/noparse]myByte, checkByte]

    Then on the slave side:

    SERIN pin, baud, [noparse][[/noparse]theByte, checkByte]
    myCheck = ~theByte
    IF (myCheck = checkByte) THEN
    ' put theByte on P0 - P7
    OUTL = theByte
    ELSE
    ' deal with transmission error
    END


    I hope this helps.

    -- Jon Williams
    -- Applications Engineer, Parallax
    -- Dallas Office
  • ArchiverArchiver Posts: 46,084
    edited 2004-07-21 22:31
    --- In basicstamps@yahoogroups.com, "Jon Williams" <jwilliams@p...>
    wrote:
    > Here's a bit of code I used in a Stamp-to-PC program...

    For what it's worth, it appears to me that this is an implementation
    of CRC-16.
Sign In or Register to comment.