专栏中心

EEPW首页 > 专栏 > msp430CRC校验

msp430CRC校验

发布人:chen3bing 时间:2024-09-10 来源:工程师 发布文章
#include "io430.h"
#include<stdio.h>
#include<stdlib.h>
typedef  unsigned int uint16;
typedef  unsigned char uint8;

uint8 data1[2] = { 0x35,0x34};

void InvertUint8(unsigned char *dBuf,unsigned char *srcBuf)  
{  
    int i;  
    unsigned char tmp[4];  
    tmp[0] = 0;  
    for(i=0;i< 8;i++)  
    {  
      if(srcBuf[0]& (1 << i))  
        tmp[0]|=1<<(7-i);  
    }  
    dBuf[0] = tmp[0];  
      
}  
void InvertUint16(unsigned short *dBuf,unsigned short *srcBuf)  
{  
    int i;  
    unsigned short tmp[4];  
    tmp[0] = 0;  
    for(i=0;i< 16;i++)  
    {  
      if(srcBuf[0]& (1 << i))  
        tmp[0]|=1<<(15 - i);  
    }  
    dBuf[0] = tmp[0];  
}  

unsigned int CRC16_MODBUS(unsigned char *puchMsg, unsigned int usDataLen)  
{  
  unsigned short wCRCin = 0xFFFF;  
  unsigned short wCPoly = 0x8005;  
  unsigned char wChar = 0;  
    
  while (usDataLen--)     
  {  
        wChar = *(puchMsg++);  
        InvertUint8(&wChar,&wChar);  
        wCRCin ^= (wChar << 8);  
        for(int i = 0;i < 8;i++)  
        {  
          if(wCRCin & 0x8000)  
            wCRCin = (wCRCin << 1) ^ wCPoly;  
          else  
            wCRCin = wCRCin << 1;  
        }  
  }  
  InvertUint16(&wCRCin,&wCRCin);  
  return (wCRCin) ;  
} 

int main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
  unsigned int data;
    data = CRC16_MODBUS(data1,2);
    //data = CRC16_USB(data1,2);
    printf("0x%x",data);//0xc9cb
  return 0;
}

运行调试

1.png

专栏文章内容及配图由作者撰写发布,仅供工程师学习之用,如有侵权或者其他违规问题,请联系本站处理。 联系我们

关键词: CRC16

相关推荐

crc16

资源下载 2007-04-19

DS2432

MODBUS协议CRC16校验工具

资源下载 2012-05-31

CRC16校验的程序

crc16 another one

资源下载 2007-02-16
更多 培训课堂
更多 焦点
更多 视频

技术专区