#学习记录#
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. 电子工业出版社.