四位二进制数转化成两个BCD码
BCD码:二——十进制码,举例:4位二进制数1111要用两个BCD码0001 0101来表示。简而言之,对于十进制0-9的时候后四位没有变化,当数据大于10的时候就进位,最高位P5逢十进一位.具体计算方法看真值表:
那么我们可以写出对于4位位二进制数来说,计算二进制转BCD的电路,具体verilog代码如下:
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 2024/04/13 22:22:38
// Design Name:
// Module Name: 4bintobdc
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module _4bitbintobdc(
input [3:0] BIN,
output reg [3:0] BCD0,BCD1
);
always@(BIN) begin
{BCD0,BCD1} = 8'b0;
if(BIN < 10) begin
BCD0 = BIN;
BCD1 = 4'b0;
end
else begin
BCD1 = 4'b1;
BCD0 = BIN-4'd10;
end
end
endmodule
测试代码如下:
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 2024/04/13 23:25:18
// Design Name:
// Module Name: test_4bitbintobcd
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module test_4bitbintobcd(
);
reg [3:0] BIN;
wire [3:0] BCD0,BCD1;
_4bitbintobdc test1(
.BIN(BIN),
.BCD0(BCD0),
.BCD1(BCD1)
);
initial begin
#10 BIN = 0001;
#100 BIN = 0011;
#100 BIN = 0101;
#100 BIN = 1001;
#100 BIN = 1101;
end
endmodule