Im trying to communicate my PIC 16F873 to a motor speed variator that use the RS485 interface and a MODBUS protocol. This protocol generate a CRC cyclic redundancy check to detect errors. This CRC value is two bytes of 8 bits, CRChigh and CRC low. To generate this two bytes, the routine use all bytes that that are included in the message. The two bytes CRC then are included in the frame message that form the MODBUS protocol.
Now, I have to make the CRC routine. I have seen the application note 730 (CRC generatig and checking), where use two implementations. I want to know if this application note generate the CRC that I need for the MODBUS protocol, because I have seen some difference.
The Modbus use this procedure:
1. Load a 16 bit CRC register with FFFF hex
2. Exclusive OR the first byte ( 8 bit )of the message with the low order byte of the 16 bit register CRC, putting the result in the CRC register
3. Shift the CRC register one bit to the right (toward the LSB), zero-filling the MSB. Extract and examine the LSB.
4. If LSB was 0 : repeat step 3 ( another shift)
If LSB was 1 : Exclusive OR the CRC register with the polynomial value A001hex (1010 0000 0000 0001 )
5. Repeat steps 3 and 4 until 8 shifts have been performed. When this is done , a complete data byte will have been processed.
6. Repeat steps 2 through 5 for the next byte of the message. Continue doing this until all bytes have been processed.
7. The final content of the CRC register is the CRC value.
8. When the CRC is placed into the message , its upper byte and lower bytes must be swapped
Anybody know something about that ? Or have a routine that can help me ?
If someone have used this protocol with PIC, please tell me if it has worked well.
Thanks for your help......
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.572 / Virus Database: 362 - Release Date: 27/01/04
CRC checking error in MODBUS protocol
I have used the following for several years on a variety of Modbus systems. It works fine on 20mhz xtal for 9600. Bear in mind going any faster might need table lookups and not calcs. Modbus use the TTY version of CRC checking.
Initialise with this before use
add to the checksum with this
obviously the results in CRC_High and CRC_Low that is calculated on the fly as it comes in is then compared with the last two bytes.
; ****************************** CRC16 routine ******************************************