格雷码计数器
《格雷码计数器》由会员分享,可在线阅读,更多相关《格雷码计数器(8页珍藏版)》请在装配图网上搜索。
1、格雷码计数器的Verilog描述一、格雷码介绍(转载)在数字系统中只能识别0和1,各种数据要转换为二进制代码才能进行处理, 格雷码是一种无权码,采用绝对编码方式,典型格雷码是一种具有反射特性和循 环特性的单步自补码,它的循环、单步特性消除了随机取数时出现重大误差的可 能,它的反射、自补特性使得求反非常方便。格雷码属于可靠性编码,是一种错 误最小化的编码方式,因为,自然二进制码可以直接由数/模转换器转换成模拟 信号,但某些情况,例如从十进制的3转换成4时二进制码的每一位都要变,使 数字电路产生很大的尖峰电流脉冲。而格雷码则没有这一缺点,它是一种数字排 序系统,其中的所有相邻整数在它们的数字表示中
2、只有一个数字不同。它在任意 两个相邻的数之间转换时,只有一个数位发生变化。它大大地减少了由一个状态 到下一个状态时逻辑的混淆。另外由于最大数与最小数之间也仅一个数不同,故 通常又叫格雷反射码或循环码。下表为几种自然二进制码与格雷码的对照表:一般的,普通二进制码与格雷码可以按以下方法互相转换:二进制码- 格雷码(编码):从最右边一位起,依次将每一位与左边一位异或 (XOR),作为对应格雷码该位的值,最左边一位不变(相当于左边是0); 格雷码-二进制码(解码):从左边第二位起,将每位与左边一位解码后的值 异或,作为该位解码后的值(最左边一位依然不变).数学(计算机)描述:原码:p0n;格雷码:c0
3、n(nN);编码:c=G(p);解码:p=F(c);书写时从 左向右标号依次减小.编码:c=p XOR pi+l(iN,OSiSn-l), cn=pn;解码:pn=cn, p=c XOR pi+1(iN,0in-1).Gray Code是由贝尔实验室的Frank Gray在20世纪40年代提出的(是1880年由 法国工程师Jean-Maurice-Emlle Baudot发明的),用来在使用PCM (Pusle Code Modulation)方法传送讯号时避免出错,并于1953年3月17日取得美国专利。 由定义可知,Gray Code的编码方式不是唯一的,这里讨论的是最常用的一种。二、格雷码
4、计数器算法(原创)格雷码计数器的关键在于根据当前码计算出下一个码,也就是计算出当前哪 个位需要取反。此处给出Matlab的一种算法:fun ctio n NxG = fNextGray( CurG, N )B(l) = CurG(l);for k = 2 : NB(k) = xor( CurG(k), B(k-l);endC = CurG;for k = N : -1 : 1if B(k) = 0 | k = 1C(k) = n ot( C(k);breakendendNxG = C;三、8bits格雷码计数器的Verilog描述(原创)module GrayCnt( Clk, nRst, C
5、ntOut );input Clk, nRst;output 7:0 CntOut;reg 7:0 CntOut;reg 7:0 NextCnt;always (posedge Clk)beginif (nRst)CntOut = 8b0000_0000;elseCntOut =0; k=k-1 )tmpCntk = CntOutk人 tmpCntk+l;if( tmpCnt0=1b0)beginNextCnt0 = CntOut0;NextCnt7:1 = CntOut7:1;endelse if( tmpCnt1=1b0 )beginNextCnt0 = CntOut0;NextCnt1
6、= CntOut1;NextCnt7:2 = CntOut7:2;endelse if( tmpCnt2=1b0 )beginNextCnt1:0 = CntOut1:0;NextCnt2 = CntOut2;NextCnt7:3 = CntOut7:3; end else if( tmpCnt3=lbO ) beginNextCnt2:0 = CntOut2:0;NextCnt3=CntOut3;NextCnt7:4 = CntOut7:4; end else if( tmpCnt4=1bO ) beginNextCnt3:0 = CntOut3:0;NextCnt4=CntOut4;Nex
7、tCnt7:5 = CntOut7:5; end else if( tmpCnt5=1b0 ) beginNextCnt4:0 = CntOut4:0;NextCnt =CntOut5;NextCnt7:6 = CntOut7:6; end else if( tmpCnt6=1b0 ) beginNextCnt5:0 = CntOut5:0;NextCnt =CntOut6;NextCnt7 = CntOut7; end else beginNextCnt6:0 = CntOut6:0;NextCnt7=CntOut7; endendEndmoduleGray code counters (h
8、aving one bit change per counter transition) are often used in FIFO desig n and digital commu ni cati on.Here I will show two styles gray code counter.Style #1Fi rst style gray code counter uses a sin gle set of flip-flops as the Gray code registe r with accompa nying Gr ay-tobi nary conversion, bin
9、ary incr eme nt, and binar y-to-G ray conversion.Gitiy to Eiimn- AFigure 3 Generalized Gry counter architect口n123456789101112131415module gray_counter(input iclk,input irst_n,input ivalid,output reg 3:0 gray);wire 3:0 bin_counter;wire 3:0 gray_counter;reg 3:0 G2B_counter;/ convert gray to bin;always
10、(ocounter)beginG2B_counter3 = gray3;16G2Bcounter2 =gray2八 G2B counter3;17G2Bcounter1=gray1八 G2B counter2;18G2Bcounter。=gray0八 G2B counter1;19end2021 /binary counter increased by one22 assign bin_counter = bin_counter +ivalid;2324/convert binto gray25assign gray counter = (bin counter 1)2627always(po
11、sedge iclk or negedge irst n)28begin29if(!irstn)30begin31gray=4b0;32end33else34begin35gray= gray counter;36end八 bin_counter;37 end3838 endmodule 40Style #2A sec ond Gr ay code counter style, the one desc ribed below, uses two sets of r egiste rs, one a binary counter and a sec ond to capt ure a bina
12、ry-to-G ray conver ted value. The intent of this Gray code counter style #2 is to utilize the binary carry str uct ure, simplify the Gr ay-to-bi nary conversion; r educe comb in atio nal logic, and incr ease the uppe r freque ncy limit of the Gray code counter.1234567891011121314151617Figure 4 - Dua
13、l n-bit Gray cede ccmntei block diagram - style #2module graycounter(input iclk,input irst_n,input ivalid,outputADDSIZET :0 bin,output reg ADDSIZE : 0 gray);parameter ADDSIZE = 4;wireADDSIZE :0 binnext;wireADDSIZE :0 graynext;regADDSIZE : 0 bin_o;assign binnext = bin_o + ivalid;18assign bin=bin oADDSIZE-1 :0;1920always(posedge iclk or negedgeirst n )21if(!irst n)日22bin o,gray = 0;23else日24bin o,gray = binnext,graynext;252627endmodule
- 温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。