|
格雷碼: 任意兩個(gè)相鄰的代碼之間, 只有一位二進(jìn)制數(shù)不同的BCD碼. 常用在大于24個(gè)狀態(tài)的狀態(tài)機(jī)設(shè)計(jì)中. 一. 自然二進(jìn)制碼轉(zhuǎn)換為二進(jìn)制格雷碼 原理: 若二進(jìn)制碼表示為: B[N-1]B[N-2]...B[2]B[1]B[0]; 相應(yīng)地, 則二進(jìn)制格雷碼表示為: G[N-1]G[N-2]...G[2]G[1]G[0]. 其中最高位保留: G[N-1] = B[N-1]; 其他各位: G[i] = B[i+1] xor B[i]. (i = 0, 1, 2, ..., n-2) Binary_to_Gray.v / Verilog module Binary_to_Gray ( input [N-1:0] B, output reg [N-1:0] G ); parameter N = N_bit_Binary; // 設(shè)置自然二進(jìn)制碼的位寬 integer i; always @ (B) begin G[N-1] = B[N-1]; for (i=0; i<N-1; i="i"+1) G[i] = B[i+1] ^ B[i]; end endmodule 仿真波形 ![]() 圖1. N="3" 圖2. N="4" 二. 二進(jìn)制格雷碼轉(zhuǎn)換為自然二進(jìn)制碼 原理: 若二進(jìn)制格雷碼表示為: G[N-1]G[N-2]...G[2]G[1]G[0]; 相應(yīng)地, 則二進(jìn)制碼表示為: B[N-1]B[N-2]...B[2]B[1]B[0]. 其中最高位保留: B[N-1] = G[N-1]; 其他各位: B[i-1] = G[i-1] xor B[i]. (i = 1, 2, ..., n-1) Gray_to_Binary.v / Verilog module Gray_to_Binary ( input [N-1:0] G, output reg [N-1:0] B ); parameter N = B_bit_Gray; // 設(shè)置二進(jìn)制格雷碼的位寬 integer i; always @ (G) begin B[N-1] = G[N-1]; for (i=1; i<=N-1; i="i"+1) B[i-1] = G[i-1] ^ B[i]; end endmodule 仿真波形 ![]() 圖3. N="3" 圖4. N="4" 三. 故障分析 在轉(zhuǎn)換時(shí), 出現(xiàn)很多毛刺, 不知怎樣消除. 希望有高人指點(diǎn). 四. 其他 本文可作為Verilog的for循環(huán)的學(xué)習(xí)實(shí)例. |
|
|