Verilog 格雷码(2)二进制与格雷码转换电路

本文详细介绍了如何使用Verilog语言在数字设计中实现二进制码到格雷码的编码和格雷码到二进制码的解码过程,通过代码示例和仿真结果展示了转换原理及其实现步骤。
摘要由CSDN通过智能技术生成

#学习记录#

数字设计-格雷码编码/解码(1)-CSDN博客

1  二进制码转化为格雷编码

  二进制转化为格雷码的规则如图1、图2所示。

图1  二进制转格雷码公式

图2  二进制码转格雷码图示

1.1  代码

`timescale 1ns / 1ps
//
// Company: 
// Engineer: Mr-pn-junction
// 
// Create Date: 2023/11/04 08:45:07
// Design Name: 
// Module Name: binary_to_gray
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module binary_to_gray(
    input [2:0] binary_value,
    output wire [2:0] gray_value
    );
generate
    genvar i;
    for(i = 0;i<2;i=i+1)begin:bin
        assign gray_value[i] = binary_value[i]^binary_value[i+1];
    end
endgenerate
    assign gray_value[2] = binary_value[2];
endmodule

1.2  testbench

`timescale 1ns / 1ps
//
// Company: 
// Engineer: Mr-pn-junction
// 
// Create Date: 2023/11/04 09:10:56
// Design Name: 
// Module Name: binary_to_gray_tb
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module binary_to_gray_tb(  );
    reg [2:0] binary_value;
    wire [2:0] gray_value;
binary_to_gray tb(
    .binary_value(binary_value),
    .gray_value(gray_value)
    );
initial begin
    binary_value = 3'b000;
    #5
    binary_value = 3'b001;
    #5
    binary_value = 3'b010;
    #5
    binary_value = 3'b011;
    #5
    binary_value = 3'b100;
    #5
    binary_value = 3'b101;
    #5
    binary_value = 3'b110;
    #5
    binary_value = 3'b111;
    $stop;
end
endmodule

1.3  仿真结果

图3  仿真结果

2  格雷码转化为二进制编码

  格雷码转化为二进制码的转化规则如图4所示

图4  格雷码转化为二进制码转化规则

2.1格雷码转化为二进制码通用电路

module  gray_to_binary(gray_value,binary_value);
    input  [SIZE:0] gray_value;
    output [SIZE:0] binary_value;
    wire   [SIZE:0] binary_value;
assign  binary_value[SIZE] = gray_value[SIZE];
generate
    genvar i;
    for(i = 0; i<SIZE; i=i+1)begin:gray
        assign binary_value[i] = binary_value[i+1]^gray_value[i];
    end
endgenerate
endmodule 

参考文献

[1]  Verilog高级数字系统设计技术与实例分析. Kishore Mishra. 电子工业出版社.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值