?? crc_table_code.m
字號(hào):
% Author:lidongshi
% input must be integer multiples of 8
% CRC-CCITT encode in table
% 07.11.30
function [crc_coded_sequence] = crc_table_code(uncode_sequence)
% uint16 bitshift bitand dec2hex
% crctable = [ '0000'; '1189'; '2312'; '329b'; '4624'; '57ad'; '6536'; '74bf']
%%%%%%%%%%%%%%%%%%%%%%%% MSB first %%%%%%%%%%%%%%%%%%%%%%%%
index = 0;
to_xor = 0;
i = 0;
table = zeros(1,256);
% make CRC table
while 1
tmp = bitshift(index,8);
to_xor = uint16(tmp);
for i = 1:8 % (i = 0; i < 8; i++)
if bitand(to_xor,hex2dec('8000')) % to_xor < 0 % bitand(to_xor,hex2dec('0001'))
to_xor = bitshift(to_xor,1);
% to_xor = bitshift(to_xor,-1);
to_xor = bitxor(to_xor,hex2dec('1021'));
% to_xor = bitxor(to_xor,hex2dec('8408'));
else
% to_xor = bitshift(to_xor,-1);
to_xor = bitshift(to_xor,1);
table(index+1) = to_xor;
end
end
if index == 255
break;
else
index = index + 1;
end
end
% breakpoint = 1; % just for test, set breakpoint
crctable = table;
table_hex = dec2hex(table);
%%%%%%%%%%%%%%%%%%%%%%%%% table of CRC-CCITT %%%%%%%%%%%%%%%%%%%%%%%%%%
% table_hex = [ '0000';'8000';'2042';'C000';'4084';'A042';'60C6';'E000';
% '8108';'C084';'A14A';'E042';'C18C';'E0C6';'E1CE';'F000';
% '8108';'0210';'C084';'2252';'A14A';'4294';'E042';'62D6';
% 'C18C';'8318';'E0C6';'A35A';'E1CE';'C39C';'F000';'E3DE';
% '2462';'C108';'0420';'8210';'64E6';'E084';'44A4';'A252';
% 'A56A';'E14A';'8528';'C294';'E5EE';'F042';'C5AC';'E2D6';
% 'C18C';'2672';'8318';'0630';'E0C6';'66F6';'A35A';'46B4';
% 'E1CE';'A77A';'C39C';'8738';'F000';'E7FE';'E3DE';'C7BC';
% '48C4';'A462';'6886';'E108';'0840';'8420';'2802';'C210';
% 'C9CC';'E4E6';'E98E';'F084';'8948';'C4A4';'A90A';'E252';
% 'A56A';'4AD4';'E14A';'6A96';'8528';'0A50';'C294';'2A12';
% 'E5EE';'CBDC';'F042';'EB9E';'C5AC';'8B58';'E2D6';'AB1A';
% '6CA6';'E18C';'4CE4';'A672';'2C22';'C318';'0C60';'8630';
% 'EDAE';'F0C6';'CDEC';'E6F6';'AD2A';'E35A';'8D68';'C6B4';
% 'E1CE';'6EB6';'A77A';'4EF4';'C39C';'2E32';'8738';'0E70';
% 'F000';'EFBE';'E7FE';'CFFC';'E3DE';'AF3A';'C7BC';'8F78';
% '9188';'C8C4';'B1CA';'E462';'D10C';'E886';'F14E';'F108';
% '1080';'8840';'30C2';'C420';'5004';'A802';'7046';'E210';
% 'C9CC';'9398';'E4E6';'B3DA';'E98E';'D31C';'F084';'F35E';
% '8948';'1290';'C4A4';'32D2';'A90A';'5214';'E252';'7256';
% 'B5EA';'E56A';'95A8';'CAD4';'F56E';'F14A';'D52C';'EA96';
% '34E2';'C528';'14A0';'8A50';'7466';'E294';'5424';'AA12';
% 'E5EE';'B7FA';'CBDC';'97B8';'F042';'F77E';'EB9E';'D73C';
% 'C5AC';'36F2';'8B58';'16B0';'E2D6';'7676';'AB1A';'5634';
% 'D94C';'ECA6';'F90E';'F18C';'99C8';'CCE4';'B98A';'E672';
% '5844';'AC22';'7806';'E318';'18C0';'8C60';'3882';'C630';
% 'EDAE';'DB5C';'F0C6';'FB1E';'CDEC';'9BD8';'E6F6';'BB9A';
% 'AD2A';'5A54';'E35A';'7A16';'8D68';'1AD0';'C6B4';'3A92';
% 'FD2E';'F1CE';'DD6C';'EEB6';'BDAA';'E77A';'9DE8';'CEF4';
% '7C26';'E39C';'5C64';'AE32';'3CA2';'C738';'1CE0';'8E70';
% '0000';'FF3E';'EFBE';'DF7C';'E7FE';'BFBA';'CFFC';'9FF8';
% 'E3DE';'7E36';'AF3A';'5E74';'C7BC';'3EB2';'8F78';'1EF0'
% ];
%%%%%%%%%%%%%%%%%%%%%%%% CRC encode %%%%%%%%%%%%%%%%%%%%%%%%%%%
uncode_sequence = [0 1 0 1 0 0 1 1]; % input bits,just for test
% uncode_sequence = [0 0 0 0 0 0 0 0];
% uncode_sequence = [1 1 1 1 1 1 1 1];
% make input bits into bytes, MSB first
len_tmp = size(uncode_sequence);
len = len_tmp(2);
len = len/8;
uncode_sequence_byte = zeros(1,len); % make bits into bytes
for i = 1:len
for j = 8:-1:1
uncode_sequence_byte(i) = uncode_sequence_byte(i) + uncode_sequence((i-1)*8+(8-j)+1) * 2^(j-1);
end
end
crc_reg = 0;
num = 1;
% CRC encode
while len > 0
len = len - 1;
crc_reg_tmp1 = bitxor(crc_reg,uncode_sequence_byte(num));
num = num + 1;
crc_reg_tmp1 = uint16(crc_reg_tmp1);
crc_reg_tmp1 = bitand(crc_reg_tmp1,hex2dec('ff'));
crc_reg_tmp1 = double(crc_reg_tmp1);
crc_reg_tmp1 = crc_reg_tmp1 + 1;
crc_reg = bitshift(crc_reg,-8);
crc_reg = bitxor(crc_reg,crctable(crc_reg_tmp1));
end
% make CRC bytes into 16bits
for i = 16:-1:1
crc_reg_bit(17-i) = fix(crc_reg/2^(i-1));
crc_reg = mod(crc_reg,2^(i-1));
end
% output bits
crc_coded_sequence = [uncode_sequence crc_reg_bit];
breakpoint = 1;% just for test, set breakpoint
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%% LSB first %%%%%%%%%%%%%%%%%%%%%%%%
% crctable = [ '0000'; '1189'; '2312'; '329b'; '4624'; '57ad'; '6536'; '74bf'];
% crctable = ['0000'; '1189'; '2312'; '329b'; '4624'; '57ad'; '6536'; '74bf';
% '8c48'; '9dc1'; 'af5a'; 'bed3'; 'ca6c'; 'dbe5'; 'e97e'; 'f8f7';
% '1081'; '0108'; '3393'; '221a'; '56a5'; '472c'; '75b7'; '643e';
% '9cc9'; '8d40'; 'bfdb'; 'ae52'; 'daed'; 'cb64'; 'f9ff'; 'e876';
% '2102'; '308b'; '0210'; '1399'; '6726'; '76af'; '4434'; '55bd';
% 'ad4a'; 'bcc3'; '8e58'; '9fd1'; 'eb6e'; 'fae7'; 'c87c'; 'd9f5';
% '3183'; '200a'; '1291'; '0318'; '77a7'; '662e'; '54b5'; '453c';
% 'bdcb'; 'ac42'; '9ed9'; '8f50'; 'fbef'; 'ea66'; 'd8fd'; 'c974';
% '4204'; '538d'; '6116'; '709f'; '0420'; '15a9'; '2732'; '36bb';
% 'ce4c'; 'dfc5'; 'ed5e'; 'fcd7'; '8868'; '99e1'; 'ab7a'; 'baf3';
% '5285'; '430c'; '7197'; '601e'; '14a1'; '0528'; '37b3'; '263a';
% 'decd'; 'cf44'; 'fddf'; 'ec56'; '98e9'; '8960'; 'bbfb'; 'aa72';
% '6306'; '728f'; '4014'; '519d'; '2522'; '34ab'; '0630'; '17b9';
% 'ef4e'; 'fec7'; 'cc5c'; 'ddd5'; 'a96a'; 'b8e3'; '8a78'; '9bf1';
% '7387'; '620e'; '5095'; '411c'; '35a3'; '242a'; '16b1'; '0738';
% 'ffcf'; 'ee46'; 'dcdd'; 'cd54'; 'b9eb'; 'a862'; '9af9'; '8b70';
% '8408'; '9581'; 'a71a'; 'b693'; 'c22c'; 'd3a5'; 'e13e'; 'f0b7';
% '0840'; '19c9'; '2b52'; '3adb'; '4e64'; '5fed'; '6d76'; '7cff';
% '9489'; '8500'; 'b79b'; 'a612'; 'd2ad'; 'c324'; 'f1bf'; 'e036';
% '18c1'; '0948'; '3bd3'; '2a5a'; '5ee5'; '4f6c'; '7df7'; '6c7e';
% 'a50a'; 'b483'; '8618'; '9791'; 'e32e'; 'f2a7'; 'c03c'; 'd1b5';
% '2942'; '38cb'; '0a50'; '1bd9'; '6f66'; '7eef'; '4c74'; '5dfd';
% 'b58b'; 'a402'; '9699'; '8710'; 'f3af'; 'e226'; 'd0bd'; 'c134';
% '39c3'; '284a'; '1ad1'; '0b58'; '7fe7'; '6e6e'; '5cf5'; '4d7c';
% 'c60c'; 'd785'; 'e51e'; 'f497'; '8028'; '91a1'; 'a33a'; 'b2b3';
% '4a44'; '5bcd'; '6956'; '78df'; '0c60'; '1de9'; '2f72'; '3efb';
% 'd68d'; 'c704'; 'f59f'; 'e416'; '90a9'; '8120'; 'b3bb'; 'a232';
% '5ac5'; '4b4c'; '79d7'; '685e'; '1ce1'; '0d68'; '3ff3'; '2e7a';
% 'e70e'; 'f687'; 'c41c'; 'd595'; 'a12a'; 'b0a3'; '8238'; '93b1';
% '6b46'; '7acf'; '4854'; '59dd'; '2d62'; '3ceb'; '0e70'; '1ff9';
% 'f78f'; 'e606'; 'd49d'; 'c514'; 'b1ab'; 'a022'; '92b9'; '8330';
% '7bc7'; '6a4e'; '58d5'; '495c'; '3de3'; '2c6a'; '1ef1'; '0f78'
% ];
%
% crctable = hex2dec(crctable);
% uncode_sequence = [0 1 0 1 0 0 1 1];
% uncode_sequence = [0 0 0 0 0 0 0 0];
% uncode_sequence = [1 1 1 1 1 1 1 1];
% uncode_sequence = fliplr(uncode_sequence);% LSB first
% len_tmp = size(uncode_sequence);
% len = len_tmp(2);
% crc_reg = 0;
% num = 1;
%
% while len > 0
% len = len - 1;
% % crc_reg = bitshift(crc_reg,-8);
% crc_reg_tmp1 = bitxor(crc_reg,uncode_sequence(num));
% num = num + 1;
% crc_reg_tmp1 = uint16(crc_reg_tmp1);
% crc_reg_tmp1 = bitand(crc_reg_tmp1,hex2dec('ff'));
% crc_reg_tmp1 = double(crc_reg_tmp1);
% crc_reg_tmp1 = crc_reg_tmp1 + 1;
% crc_reg = bitshift(crc_reg,-8);
% crc_reg = bitxor(crc_reg,crctable(crc_reg_tmp1));
% end
%
% for i = 16:-1:1
% crc_reg_bit(17-i) = fix(crc_reg/2^(i-1));
% crc_reg = mod(crc_reg,2^(i-1));
% end
%
% crc_coded_sequence = [uncode_sequence crc_reg_bit];
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -