发表于:2011-08-14 11:12:33
楼主
输入参数:
iARRAY ARRAY【0..7】 OF INT /输入整型数组
iNum INT /CRC校验字节数
输出参数:
iCRC INT /CRC结果
iERR INT /错误码
专用参数:
略
IF iNum=0 THEN
iERR:=16#FF;
RETURN;
ELSE
wCRCRegister:=16#FFFF;
DIVMOD (IN1 := iNum,IN2 := 2,DV => iQuotient,MD => iRemainder);
IF iRemainder=1 THEN
iLoop:=iQuotient+1;
ELSE
iLoop:=iQuotient;
END_IF;
FOR iCtrlVar1:=0 TO iLoop-1 DO
iData:=iARRAY【iCtrlVar1】;
WORD_AS_BYTE (IN := INT_TO_WORD (IN := iData),LOW => bLSB,HIGH => bMSB);
wData:= BYTE_TO_WORD (IN := bLSB);
BNext:=FALSE;
NEXT: wCRCRegister:= XOR_WORD (IN1 := wCRCRegister,IN2 := wData);
FOR iCtrlVar2:=1 TO 8 DO
IF (AND_WORD (IN1 := wCRCRegister,IN2 := 16#0001)=1) THEN
wCRCRegister := SHR_WORD (IN := wCRCRegister,N := 1);
wCRCRegister := XOR_WORD (IN1 := wCRCRegister,IN2 := 16#A001);
ELSE
wCRCRegister := SHR_WORD (IN := wCRCRegister,N := 1);
END_IF;
END_FOR;
IF (iCtrlVar1*2+2<=iNum&NOT BNext) THEN
wData:= BYTE_TO_WORD (IN := bMSB);
BNext:=TRUE;
JMP NEXT;
END_IF;
END_FOR;
iCRC:= WORD_TO_INT (IN := wCRCRegister);
END_IF;
经测试,上述程序段可以使用!