资源预览内容
第1页 / 共1页
亲,该文档总共1页全部预览完了,如果喜欢就下载吧!
资源描述
matlab实现CRC适于任何于等于32位的CRC校验function crcout = crcX(datain,tempstr,initdata,refin,refout,xorout)%cala crc%author *%parameter: initdata -xor the fisrt reg; refin - the datain need reflect%refout: out crc need reflect; xorout- crc reg xor xoroutmcrctemp = uint32(bin2dec(tempstr);len = length(tempstr);size = length(datain);for i = 0:(fix(len/8)+1) datain(end+1) = 0;endif refin = 0 datain = reflect(datain , 8);end%andvalue = num2str(uint8(zeros(1,len+1);%andvalue(len+1) = 1;andvalue = bitshift(1,len);midout = uint32(0);for i = 0:(size*8)-1)+len) midout = bitshift(midout,1); if bitand(datain(fix(i/8)+1),bitshift(1,7-rem(i,8) = 0 midout = bitor(midout,1); end if i = len-1 midout = bitxor(midout,initdata); end if bitand(midout,andvalue)=0 midout = bitxor(midout,mcrctemp); end% dec2bin(midout)endif refout = 0 midout = reflect(midout,len);endmidout = bitxor(midout,xorout);andvalue = bitshift(4294967295,len-32);crcout = bitand(midout,andvalue);end function outdata=reflect(data,bitnum)%reflect this data%paramter explain: bitnum reflect bit numbersize=length(data);outdata = zeros(1,size);mvalue = bitshift(4294967295,bitnum);for i = 1:size outdata(i) = data(i); outdata(i) = bitand(outdata(i),mvalue); for j=0:(bitnum-1) if bitand(data(i),bitshift(1,j) = 0 outdata(i) = bitor(outdata(i),bitshift(1,bitnum -1 - j); end endendend
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号