我目前的变送器中是用的软件CRC校验,模仿台达PLC的MODBUS RTU定义方式,代码如下,哪位用过PSOC中硬件CRC16实现MODBUS RTU可否交流一下
#include <m8c.h> // part specific constants and macros #include "PSoCAPI.h" // PSoC API definitions for all User Modules #include "stdlib.h" extern unsigned char CRC_buf[10];
unsigned int CRC_Check(unsigned char *CRC_buf1,unsigned char BuffLen) { unsigned int CRC_ReturnValue = 0xFFFF; unsigned char i=0,j; while ( BuffLen -- ) { CRC_ReturnValue ^= CRC_buf1[i++]; j = 8; do { if ( CRC_ReturnValue & 0x01 ) { CRC_ReturnValue = ( CRC_ReturnValue >> 1 ) ^ 0xA001; } else { CRC_ReturnValue = CRC_ReturnValue >> 1; } -- j; } while ( j ); } return CRC_ReturnValue; // } |