SHT CRC 算法

SHT CRC 算法,第1张

1、循环校验码(CRC码):是数据通信领域中最常用的一种差错校验码,其特征是信息字段和校验字段的长度可以任意选定。

2、生成CRC码的基本原理:任意一个由二进制位串组成的代码都可以和一个系数仅为‘0’和‘1’取值的多项式一一对应。例如:代码对应的多项式为x6+x4+x2+x+1,而多项式为x5+x3+x2+x+1对应的代码。

3、CRC码集选择的原则:若设码字长度为N,信息字段为K位,校验字段为R位(N=K+R),则对于CRC码集中的任一码字,存在且仅存在一个R次多项式g(x),使得

V(x)=A(x)g(x)=xRm(x)+r(x);

其中: m(x)为K次信息多项式, r(x)为R-1次校验多项式,

g(x)称为生成多项式:

g(x)=g0+g1x+ g2x2++g(R-1)x(R-1)+gRxR

发送方通过指定的g(x)产生CRC码字,接收方则通过该g(x)来验证收到的CRC码字。

标准CRC生成多项式如下表:

名称 生成多项式 简记式 标准引用

CRC-4 x4+x+1 3 ITU G704

CRC-8 x8+x5+x4+1 0x31

CRC-8 x8+x2+x1+1 0x07

CRC-8 x8+x6+x4+x3+x2+x1 0x5E

CRC-12 x12+x11+x3+x+1 80F

CRC-16 x16+x15+x2+1 8005 IBM SDLC

CRC16-CCITT x16+x12+x5+1 1021 ISO HDLC, ITU X25, V34/V41/V42, PPP-FCS

CRC-32 x32+x26+x23++x2+x+1 04C11DB7 ZIP, RAR, IEEE 802 LAN/FDDI, IEEE 1394, PPP-FCS

CRC-32c x32+x28+x27++x8+x6+1 1EDC6F41 SCTP

基本算法(人工笔算):

以CRC16-CCITT为例进行说明,CRC校验码为16位,生成多项式17位。假如数据流为4字节:BYTE[3]、BYTE[2]、BYTE[1]、BYTE[0];

数据流左移16位,相当于扩大256×256倍,再除以生成多项式0x11021,做不借位的除法运算(相当于按位异或),所得的余数就是CRC校验码。

发送时的数据流为6字节:BYTE[3]、BYTE[2]、BYTE[1]、BYTE[0]、CRC[1]、CRC[0];

举例:

信息字段代码为: m(x)=x6+x4+x3+1 代码为:

生成多项式: g(x)=x4+x3+1 代码为:11001

m(x)x4=x10+x8+x7+x4 对应的代码记为: 即 左移4位

m(x)x4 在与 g(x)进行 模2的除法运算,相当于按位异或,计算过程如下:

1 0 1 1 0 0 1 0 0 0 0

1 1 0 0 1

-----------------------------

0 1 1 1 1 0 1 0 0 0 0

1 1 0 0 1

-----------------------------

0 0 0 1 1 1 1 0 0 0 0

1 1 0 0 1

-----------------------------

0 0 1 1 1 0 0 0

1 1 0 0 1

-----------------------------

0 0 1 0 1 0 --------------> 余数 即 校验码

应该不是判断忙碌或者不只是判断忙碌,这个语句应该是送了一串命令进去

//HD44780 LCD DRIVER

#include <AT89X52H>

#include <ctypeh>

//LCD Commands

#define LCD_CLS 1 //Clears entire display and sets DDRAM address 0

#define LCD_HOME 2 //Sets DDRAM address 0 in address counter

#define LCD_SETMODE 4 //Sets cursor move direction and specifies display shift

#define LCD_SETVISIBLE 8 //Sets entire display (D) on/off,cursor on/off (C), and blinking of cursor position character (B)

#define LCD_SHIFT 16 //Moves cursor and shifts display without changing DDRAM contents

#define LCD_SETFUNCTION 32 //Sets interface data length (DL), number of display lines (N), and character font (F)

#define LCD_SETCGADDR 64 //Sets CGRAM addressCGRAM data is sent and received after this setting

#define LCD_SETDDADDR 128 //Sets DDRAM address DDRAM data is sent and received after this setting

#define TURE 1

#define FORSE 0

#define TMH 0xf8 //delay timeh 2ms

#define TML 0xCD //delay timel

//------------------------------------------

#define MAX_DISPLAY_CHAR 2

//-----------------------------------------

unsigned char code text[6]="hellow";

static unsigned char data counter,a;

void wcchar(unsigned char d); //write a command char

void wdchar(unsigned char i); //write a data char

char wtbusy(); //wait busy sign

void clrscr(void); //clear screen

void initime0(void); //initial time0

void delay(unsigned char i); //delay

//-------------------------------

unsigned char crc8(unsigned char pData,unsigned char count);

unsigned char pData[]={0x33,0x12,0x34,0x56,0x78,0x33,0x12,0x23,0x45,0x56};

unsigned char data crc;

static char outputbuffer[MAX_DISPLAY_CHAR];

char calc_decascii(char num);

//-------------------------------

void ISR_Timer0(void) interrupt 1

{

TL0=TML;

TH0=TMH;

counter--;

}

void ISR_Int0(void) interrupt 0

{

wdchar(text[a++]); //display "hellow"

if (a>=6)

{a=0;wdchar(' ');}

}

//main program display "hellow"

main(void)

{

unsigned char data k;

initime0();

wcchar(0x38); //initial lcd

wcchar(LCD_SETVISIBLE+6); //set lcd visible

clrscr(); //clear lcd

while(1)

{

crc8(&pData,10);

calc_decascii(crc);

wdchar (outputbuffer[1]);

wdchar (outputbuffer[2]);

wdchar (' ');

for(k=0;k<=6;k++)

{

// wdchar(text[k]); //display "hellow"

P1_2=0;

delay(55); //delay

P1_2=1;

delay(55); //delay

}

}

}

//sub function || display a char

void wdchar(unsigned char i) //write a char

{

unsigned char xdata j;

P1_0=1;

P1_1=0;

j=0x8000;

j=i;

while (wtbusy())

{}

}

void wcchar(unsigned char d) //write a command char

{

unsigned char xdata j;

P1_0=0;

P1_1=0;

j=0x8000;

j=d;

while (wtbusy())

{}

}

char wtbusy() //wait busy sign

{

unsigned char xdata j;

P1_0=0;

P1_1=1;

j=0x8000;

if(j&0x80)

{

return TURE;

}

else

{

return FORSE;

}

}

//clear screen

void clrscr()

{

wcchar(LCD_CLS);

}

//initial time0

void initime0()

{

TL0=TML;

TH0=TMH;

TR0=TURE;

IE=0x93; //Enable T0 and Serial Port

}

//sub function time delay

void delay(unsigned char i)

{

counter=i;

while (counter)

{}

}

unsigned char crc8(unsigned char pData,unsigned char count)

{

// unsigned char crc;

crc = 0;

while (count-- > 0)

{

crc ^= pData++;

}

return crc;

}

char calc_decascii(char num)

// A rather messy function to convert a floating

// point number into an ASCII string

{ long data temp = num;

char data arrayptr = &outputbuffer[MAX_DISPLAY_CHAR];

long data divisor = 10;

long data result;

char data remainder,asciival;

int data i;

// If the result of the calculation is zero

// insert a zero in the buffer and finish

if (!temp)

{ arrayptr = 48;

goto done;

}

// Handle Negative Numbers

if (temp < 0)

{ outputbuffer[0] = '-';

temp -= 2temp;

}

for (i=0 ; i < sizeof(outputbuffer) ; i++)

{ remainder = temp % divisor;

result = temp / divisor;

// If we run off the end of the number insert a space into

// the buffer

if ((!remainder) && (!result))

{ arrayptr = ' ';}

// We're in business - store the digit offsetting

// by 48 decimal to account for the ascii value

else

{ asciival = remainder + 48;

arrayptr = asciival;

}

temp /= 10;

// Save a place for a negative sign

if (arrayptr != &outputbuffer[1]) arrayptr--;

}

done: return outputbuffer;

}

生成多项式:G(X)=X^8+X^2+X+1转化为100000111 =0x1070

#define POLY (0x1070U << 3)

u8 crc8(u16 data)

{

int i;

for(i = 0; i < 8; i++) {

if (data & 0x8000)

data = data ^ POLY;

data = data << 1;

}

return (u8)(data >> 8);

}

以上就是关于SHT CRC 算法全部的内容,包括:SHT CRC 算法、关于单片机液晶1602的程序片段问题、查表法计算CRC码的C程序设计,生成多项式为x8+x2+x+1等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9443953.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存