PDA

View Full Version : A Checksum Algorithm



amindzo
- 6th December 2008, 20:54
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?

tenaja
- 6th December 2008, 22:54
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.

BrianT
- 8th December 2008, 00:19
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

timmers
- 8th December 2008, 22:33
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.