CRC校验码编码实验.doc_第1页
CRC校验码编码实验.doc_第2页
CRC校验码编码实验.doc_第3页
CRC校验码编码实验.doc_第4页
CRC校验码编码实验.doc_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

试验四 CRC校验码编码实验 试验报告实验四 CRC校验码编码实验班级:电子C073 姓名:赵宣 学号:075584一、实验目的 1、复习C+语言基本编写方法,熟悉面向对象编程方法。2、学习CRC编码基本流程, 学会调试循环冗余校验码编码程序。3、根据给出资料,掌握CRC校验码的编码原理,重点掌握按字节(Byte)编码方法二、实验内容与原理(一)实验原理:1 CRC 校验码介绍CRC 校验的基本思想是利用线性编码理论,在发送端根据要传送的k 位二进制码序列,以一定的规则产生一个校验用的监督码(CRC 码)r 位,并附在信息后边,构成一个新的二进制码序列数共 (k+r) 位,最后发送出去。在接收端,则根据信息码和CRC 码之间所遵循的规则进行检验,以确定传送中是否出错。16 位的CRC 码产生的规则是先将要发送的二进制序列数左移16 位(乘以216)后,再除以一个多项式,最后所得到的余数既是CRC 码。求CRC 码所采用模2 加减运算法则,既是不带进位和借位的按位加减,这种加减运算实际上就是逻辑上的异或运算,加法和减法等价,乘法和除法运算与普通代数式的乘除法运算是一样,符合同样的规律。接收方将接收到的二进制序列数(包括信息码和CRC 码)除以多项式,如果余数为0,则说明传输中无错误发生,否则说明传输有误。2按位计算CRC一个二进制序列数可以表示为求此二进制序列数的CRC 码时,先乘以216后(左移16位),再除以多项式G(X) ,所得的余数就是所要求的CRC 码。可以设:其中Q n (X) 为整数, R n (X) 为16位二进制余数,将上式代入前式得:再设:其中Qn-1(X) 为整数, Rn-1(X) 为16位二进制余数,继续代入前式,多次迭代得到:根据CRC 的定义,很显然,十六位二进制数R0(X) 即是要求的CRC 码。3按字节计算CRC对于一个二进制序列数可以按字节表示为下式,其中Bn(X) 为一个字节(共8位):求此二进制序列数的CRC码时,先乘以216后(左移16位),再除以多项式G(X),所得的余数即是所要求的CRC 码。可以设:其中Qn(X) 为整数, Rn(X) 为16位二进制余数,将上式代入前式得:由于:其中RnH8(X) 是Rn(X)的高八位, RnL8(X)是Rn(X)的低八位,代入前式得到:再设:其中Qn-1(X ) 为整数, Rn-1(X ) 为16位二进制余数,继续代入前式,多次迭代得到:显然,十六位二进制数R0(X)即是要求的CRC码。(二)实验内容:1根据实验原理掌握CRC 校验码编码/解码基本流程。2在C+编译器下能够调试编码算法每一个步骤,重点掌握按字节编码的过程。三、实验仪器、设备1计算机系统最低配置 256M 内存、P4 CPU。2C+ 编程软件 Visual C+ 7.0 (Microsoft Visual Studio 2003) Visual C+ 8.0 (Microsoft Visual Studio 2005) 四、实验步骤项目文件建立步骤同实验二,下面列出对给定字符串CRC 校验主要步骤:步骤1:从主函数入口输入一个字符串,并且确定按字节32 位CRC 校验编码,编码多项式采用CCITT 标准形式多项式。步骤2:调用编码函数,依次读入字符串每个自己,进行模2 除法运算。步骤3:将原来字符串左移32 位,将除法最后的余式追加到字符串的后32 位中去,得到该字符串CRC 校验编码结果。步骤4:如果要解码,首先确认编码多项式,然后将接收字符串除以编码多项式。如果能够整除,说明字符串在传输或存储中没有发生错误;否则,表明字符串在传输或存储中产生错误,导致CRC 校验失败。五、实验数据及结果分析:第 6 页 共 6 页#include stdafx.h#include CRC32.hvoid SlowCrc:PutByte (unsigned char byte)unsigned char mask = 0x80; / leftmost bitfor (int j = 0; j = 1;void SlowCrc:PutBit (bool bit)std:cout bit? 1: 0;bool topBit = (_register & 0x80000000) != 0;/ shift bit into register from the right_register = 1;_register = (bit? 0x1: 0x0); / OR or XOR, same resultif (topBit)/ XOR the 32-bits of the key./ The implicit high bit of the 33-bit key conceptually/ clears the topBit shifted out of the register_register = _key;int _tmain(int argc, _TCHAR* argv)Crc:Type const ethernetKey = 0x04c11db7;SlowCrc slowCrc (ethernetKey);/ calculate R in: M (x) * x32 = Q (x) * K (x) + R (x)std:string msg (Harry had a little lamp);size_t origLen = msg.length ();/ Multiply message by x32msg.resize (origLen + sizeof (Crc:Type);for (size_t i = 0; i msg.length (); +i)std:cout ;slowCrc.PutByte (msg i);std:cout n- ;Crc:Type crc = slowCrc.Done ();std:cout n0x std:hex crc std:endl;/ Now test consistency/ M (x) * x32 + R (x)int shift = 32;for (int j = 0; j sizeof (SlowCrc:Type); +j)shift -= 8;msg origLen + j = static_cast (crc shift);/ Divide it by K (x) - should be divisiblefor (i = 0; i msg.length (); +i)std:cout ;slowCrc.PutByte (msg i);std:cout n- ;crc = slowCrc.Done ();std:cout n0x std:hex crc std:endl;assert (crc = 0);return 0;六、思考题1掌握按字节编码的过程,试着写出CRC 校验编码的Matlab 程序?%main programclear all;input=1 1 0 0CRC_Number=3 8 12 16;for crc_index = 1:size(CRC_Number,2)crc_no = CRC_Number(crc_index)output = crc_add(input, crc_no)output_after_check, indicate =crc_check(output,crc_no)end function output, indicate = crc_check( input, crc_no )% the function is proposed for deleting crc bits from the input sequencen = size(input,2);generator = zeros(1,crc_no+1);output = zeros(1,n-crc_no);switch crc_nocase 3generator = 1 0 1 1;case 8generator = 1 1 0 0 1 1 0 1 1; %D8+D7+D4+D3+D+1case 12generator = 1 1 0 0 0 0 0 0 0 1 1 1 1; %D12+D11+D3+D2+D+1case 16generator = 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1; %D16+D12+D5+1case 24generator = 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1; %D24+D23+d6+D5+D+1otherwisefprintf(nPlease the number of crc bits should be 8 12 16 24n);endoutput = input(1:n-crc_no);for ii = 1:n-crc_noif(input(1) = 1)input(1:crc_no+1) = mod(input(1:crc_no+1)+generator),2);endinput = input(2:end) input(1);endif sum(input) = 0indicate = 0;elseindicate = 1;endfunction output = crc_add( input, crc_no )% the function is proposed for adding crc bits to the input sequencek = size(input,2);generator = zeros(1,crc_no+1);output = zeros(1,k+crc_no);switch crc_nocase 3generator = 1 0 1 1;case 8generator = 1 1 0 0 1 1 0 1 1; %D8+D7+D4+D3+D+1case 12generator = 1 1 0 0 0 0 0 0 0 1 1 1 1; %D12+D11+D3+D2+D+1case 16generator = 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1; %D16+D12+D5+1case 24generator = 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1; %D24+D23+d6+D5+D+1otherwisefprintf(nPlease the number of crc bits should be 8 12 16 24n);endoutput(1:k)=input;for ii = 1:kif(output(1) = 1)output(1:crc_no+1) = mod(output(1:crc_no+1)+generator),2);endoutput = output(2:end) output(1);endoutput = input output(1:crc_no);2如何设计FPGA 实现CRC 校验计算?32位并行数据CRC - 16校验码的FPGA实现表32位CRC - 16编码器的端口说明clk input 系统时钟crc_rset input CRC生成器复位sdata input 输入数据crc_out output 输出CRC校验码其中VHDL代码如下:L IBRARY IEEE;USE IEEE. STD_LOGIC_1164. ALL;ENTITY CRC16 ISPORT ( sdata: IN STD_LOGIC_VECTOR (31 DOWNTO 0) ;clk: IN STD_LOGIC;crc_rset: IN STD_LOGIC;crc_out: OUT STD_LOGIC_VECTOR (15 DOWNTO 0) ) ;END CRC16;ARCH ITECTURE th OF CRC16 ISSIGNAL D: STD_LOGIC_VECTOR (31 DOWNTO 0) ;SIGNAL R, crc_temp: STD_LOGIC_VECTOR (15 DOWNTO 0) ;BEGIND = sdata;PROCESS( clk, crc_in)BEGINIF crc_in =1THEN R 0) ;ELSIF rising_edge ( clk) THENcrc_temp (0) =D (31) XOR D (30) XOR D (27) XOR D (26) XOR D ( 25) XOR D ( 24) XOR D ( 23) XORD (22) XOR D (21) XOR D (20) XOR D (19) XOR D (18) XOR D ( 17) XOR D ( 16) XORD (15) XOR D (13) XOR D ( 12) XOR D ( 11) XOR D ( 10 ) XOR D ( 9) XOR D ( 8) XORD (7) XOR D (6) XOR D (5) XOR D (4) XOR D (3) XOR D (2) XOR D (1) XOR D (0) XORR (0) XOR R (1) XOR R (2) XOR R (3) XOR R (4) XOR R (5) XOR R (6) XOR R (7) XORR (8) XOR R (9) XOR R (10) XOR R (11) XOR R (14) XOR R (15) ;crc_temp (1) =D (31) XOR D (28) XOR D (27) XOR D ( 26) XOR D ( 25) XOR D ( 24) XOR D ( 23) XORD (22) XOR D (21) XOR D (20) XOR D (19) XOR D (18) XOR D ( 17) XOR D ( 16) XORD (14) XOR D (13) XOR D ( 12) XOR D ( 11) XOR D ( 10 ) XOR D ( 9) XOR D ( 8) XORD (7) XOR D (6) XOR D (5) XOR D (4) XOR D (3) XOR D (2) XOR D (1) XOR R (0) XORR (1) XOR R (2) XOR R (3) XOR R (4) XOR R (5) XOR R (6) XOR R (7) XOR R (8) XORR (9) XOR R (10) XOR R (11) XOR R (12) XOR R (15) ;crc_temp (2) =D (31) XOR D ( 30) XOR D ( 29) XOR D ( 28) XOR D ( 16) XOR D ( 14) XOR D ( 1) XORD (0) XOR R (0) XOR R (12) XOR R (13) XOR R (14) XOR R (15) ;crc_temp (3) =D (31) XOR D ( 30) XOR D ( 29) XOR D (17) XOR D (15) XOR D ( 2) XOR D (1) XORR (1) XOR R (13) XOR R (14) XOR R (15) ;crc_temp (4) =D (31) XOR D (30) XOR D (18) XOR D (16) XOR D (3) XOR D (2) XOR R (0) XOR R (2)XOR R (14) XOR R (15) ;crc_temp (5) = D ( 31) XOR D ( 19) XOR D ( 17 ) XOR D ( 4 ) XOR D ( 3 ) XOR R ( 1 ) XOR R ( 3 ) XORR (15) ;crc_temp (6) =D (20) XOR D (18) XOR D (5) XOR D (4) XOR R (2) XOR R (4) ;crc_temp (7) =D (21) XOR D (19) XOR D (6) XOR D (5) XOR R (3) XOR R (5) ;crc_temp (8) =D (22) XOR D (20) XOR D (7) XOR D (6) XOR R (4) XOR R (6) ;crc_temp (9) =D (23) XOR D (21) XOR D (8) XOR D (7) XOR R (5) XOR R (7) ;crc_temp (10) =D (24) XOR D (22) XOR D (9) XOR D (8) XOR R (6) XOR R (8) ;crc_temp (11) =D (25) XOR D (23) XOR D (10) XOR D (9) XOR R (7) XOR R (9) ;crc_temp (12) =D (26) XOR D (24) XOR D (11) XOR D (10) XOR R (8) XOR R (10) ;crc_temp

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论