找回密码
 注册会员
img_loading
智能检测中
更新自动建库工具PCB Footprint Expert 2024.04 Pro / Library Expert 破解版

51单片机的CRC程序通过查表的办法进行计算对于51单片机相当适用

[复制链接]
admin 发表于 2010-5-19 01:47:30 | 显示全部楼层 |阅读模式

本文包含原理图、PCB、源代码、封装库、中英文PDF等资源

您需要 登录 才可以下载或查看,没有账号?注册会员

×
51单片机的CRC程序,此程序是通过查表的办法进行计算,对于51单片机相当适用


  1. #define BYTE_BITS 8 // Number of bits in byte<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
  2. #define TABLE_SIZE  1<<BYTE_BITS // Size of table
  3. #define WIDTH 16 // width of poly(number of most significnt bit)
  4. #define MSB_MASK    1<<(WIDTH-1) // Mask for high order bit in word
  5. #define POLY 0x1021 // Ploy. Bit #16 is set and hidde
  6. typedef unsigned short Crc16;
  7. Crc16 table[TABLE_SIZE];
  8. void crc16init()
  9. {
  10. Crc16 i, val,t1,t2;
  11. int j;
  12. for(i=0;i<TABLE_SIZE;++i)
  13. {
  14. val=i<<(WIDTH-BYTE_BITS);
  15. for(j=0;j<BYTE_BITS;++j)
  16. {
  17. t1= val<<1;
  18. t2=((val&MSB_MASK) ? POLY : 0);
  19. val=t1^t2;
  20. }
  21. table[i]=val;
  22. }
  23. }

  24. Crc16 crc16(Crc16 crc, void const* src, int cnt)
  25. {
  26. unsigned char const* s=(unsigned char const*)src;
  27. while(cnt--)
  28. crc=(crc<<BYTE_BITS)^table[(crc>>(WIDTH - BYTE_BITS)) ^ *s++];
  29. return crc;
  30. }

  31. void main()
  32. {
  33. unsigned char data[12]={0x54, 0xD3, 0x07, 0x0C, 0x17, 0x45, 0x0B,0x0E ,0x19,0,0};
  34. Crc16 result;
  35. crc16init();
  36. result=crc16(0,&data[0],9);
  37. data[9]=(result>>8)&0xff;
  38. data[10]=result&0xff;
  39. result=crc16(0,&data[0],11);

  40. }<!--EndFragment-->
复制代码
wuyao 发表于 2010-9-18 07:32:05 | 显示全部楼层
学习学习111111111111
您需要登录后才可以回帖 登录 | 注册会员

*滑块验证:
img_loading
智能检测中
本版积分规则

QQ|手机版|MCU资讯论坛 ( 京ICP备18035221号-2 )|网站地图

GMT+8, 2025-6-6 20:48 , Processed in 0.051540 second(s), 9 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表