A Checksum Algorithm


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2006
    Location
    Iran
    Posts
    94

    Default A Checksum Algorithm

    Hi,i want to connect two pic microcontrollers (PIC16F873A and PIC16F877A) with serial communication (with serin and serout instructions).there is a long distance between them (40 meter at least).i want to connect them with twist pair shield cable as a connecting cable. maybe we have some errors in transmission,which checksum Algorithm you suggest? what about CRC?
    Last edited by amindzo; - 6th December 2008 at 21:05.

  2. #2
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by amindzo View Post
    Hi,i want to connect two pic microcontrollers (PIC16F873A and PIC16F877A) with serial communication (with serin and serout instructions).there is a long distance between them (40 meter at least).i want to connect them with twist pair shield cable as a connecting cable. maybe we have some errors in transmission,which checksum Algorithm you suggest? what about CRC?
    Here are some common methods. Easiest is to sum all of the data into a byte, ignoring the overflow. Another is to xor all of the data. If it is important, you can do both.

  3. #3


    Did you find this post helpful? Yes | No

    Default Get the hardware right first

    I assume that the hardware at each end will have their own power supplies. Sometimes that means each end will be driven from a different phase of the mains and this can lead to significant earth potential differences. I have measured over 40 volts between the earth pins on two adjacent power outlets in a factory where one power outlet was on one phase and the other on a separate phase. The power outlets were only a metre apart but that was where the electrician split the feeds. The potential difference varied with factory electrical loading and it took days to track down the intermittent errors.

    If you try to connect TTL over 40 metres you can only tolerate about one volt of earth difference before the system stops working. No amount of checksum, CRC or retransmission scheme will save you if the earth potential difference is higher than that.

    I suggest you use an opto coupler at each end and two twisted pair wires for transmit and receive. Ethernet UTP is high grade cable and the different twist rates for each pair minimise crosstalk and noise pickup. Optocouplers are generally not as good as transformers at all terminations but the transformer approach needs a proper modulation scheme to eliminate any DC component (Google Bi-Phase modulation)which is another layer of complexity best avoided unless you are in a very high electical noise or lightning prone environment.

    At one end the transmitter is direct connected to the line with a current limiting resistor. The corresponding receiver at the other end is driven by an optocoupler. The system is then a current loop and the maximum distances can be several hundred metres.

    Only AFTER you have reliable communications do you think about adding a checksum or CRC. The end application will dictate how robust the error checking needs to be. If human life is at stake a 32 bit CRC might be wise. If you are just turning a lamp on or off simply sending the command information repeatedly, with no error checking will be fine. I personally use an 8 or 16 bit simple addition checksum for scientific instruments and data loggers and this makes the error rate vanishingly small for a tiny code overhead.

    HTH

    BrianT
    Last edited by BrianT; - 8th December 2008 at 00:25. Reason: expanded reply

  4. #4
    timmers's Avatar
    timmers Guest


    Did you find this post helpful? Yes | No

    Default

    A simple and reliable method we use is:-

    1) convert RS232c (TTL) to RS485 using MAX3082
    2) Use a checksum.

    Example. Our data packets are preceeded with a D* preamble and terminated with a checksum.

    Byte 1 "D"
    Byte 2 "*"
    Byte 3
    Byte 4
    Byte 5
    Byte 6
    Byte 7
    Byte 8
    Byte 9 Checksum

    To calculate the checksum
    WORD = BYTE3 + BYTE4 +BYTE5 +BYTE6 +BYTE7 +BYTE8
    CHECKSUM=WORD.BYTE0 ^ $FF

    VOILA.

Similar Threads

  1. NMEA CheckSum Routine
    By Tom Gonser in forum Code Examples
    Replies: 2
    Last Post: - 6th June 2015, 22:24
  2. 4 Bytes one button press
    By Dennis in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 15th January 2010, 22:36
  3. Checksum
    By retepsnikrep in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 24th October 2009, 04:09
  4. Creating Checksum in PBP
    By Tom Gonser in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 16th March 2005, 04:49
  5. Checksum problem!
    By atomski in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 3rd November 2004, 07:21

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts