smbc_pec

文章提供了一个用于计算CRC8校验和的静态函数,该函数接受一个数据指针和数据长度作为参数,通过对每个字节进行位操作来计算CRC值。在主函数中,有两个示例数据数组,用于测试CRC8函数,并打印出相应的校验结果。
摘要由CSDN通过智能技术生成


#include <stdio.h>

typedef unsigned char uint8_t ;
typedef unsigned int uint32_t  ;
typedef unsigned short uint16_t  ;


static uint8_t CRC8(uint8_t *data, uint8_t datalen)
{
    uint8_t crc = 0xff;
    uint8_t i = 0;

    if(datalen <= 0)
    {
        return crc;
    }
    while(datalen--)
    {
        for(i = 0x80; i != 0; i /=2)
        {
            if((crc & 0x80) != 0)
            {
                crc *= 2;
                crc ^= 0x07;
            }
            else     
            {
                crc *=2;
            }
            if((*data & i) != 0)
            {
                crc ^= 0x07;
            }
        }
        data++;
    }
    return crc;
}

int main(void) {

    uint8_t example_1[] = {0xd4, 0x00, 0xd5, 0x06, 0xbf, 0xff, 0x1e, 0x01, 0x3c, 0x08};
  
    uint8_t example4[] = {0xf0,0x00,0xf1,0x06,0xe7,0x00,0xe1,0x5c,0x5e,0xf7};


    uint8_t crc;
    uint8_t len = 0xa;
    crc = CRC8(example4, len);

    printf("Hello World crc:%x\n",crc);
    return 0;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值