14位二进制转4位(16位)bcd
14 bit binary to 4 digit(16bit) bcd
我正在尝试将 14 位二进制转换为 4 位 bcd。但效果不佳。给我一些想法
这是我的代码
module binary2BCD(
input [13:0] binary,
output reg [15:0] o
);
reg [29:0]shift=0;
integer i;
always@(*) begin
shift = 30'b0;
shift[13:0] = binary;
for (i=0; i<14; i=i+1) begin
if (shift[17:14] > 4'b0100)
shift[17:14] = shift[17:14] + 4'b0011;
if (shift[21:18] > 4'b0100)
shift[21:18] = shift[17:14] + 4'b0011;
if (shift[25:22] > 4'b0100)
shift[25:22] = shift[17:14] + 4'b0011;
if (shift[29:26] > 4'b0100)
shift[29:26] = shift[17:14] + 4'b0011;
shift = shift << 1;
end
o= {shift[29:26], shift[25:22], shift[21:18], shift[17:14]};
end
endmodule
这看起来不错。我认为唯一的问题是你增加了错误的 BCD 数字。
if (shift[21:18] > 4'b0100)
shift[21:18] = shift[17:14] + 4'b0011;
^^^^^ should be 21:18
我正在尝试将 14 位二进制转换为 4 位 bcd。但效果不佳。给我一些想法
这是我的代码
module binary2BCD(
input [13:0] binary,
output reg [15:0] o
);
reg [29:0]shift=0;
integer i;
always@(*) begin
shift = 30'b0;
shift[13:0] = binary;
for (i=0; i<14; i=i+1) begin
if (shift[17:14] > 4'b0100)
shift[17:14] = shift[17:14] + 4'b0011;
if (shift[21:18] > 4'b0100)
shift[21:18] = shift[17:14] + 4'b0011;
if (shift[25:22] > 4'b0100)
shift[25:22] = shift[17:14] + 4'b0011;
if (shift[29:26] > 4'b0100)
shift[29:26] = shift[17:14] + 4'b0011;
shift = shift << 1;
end
o= {shift[29:26], shift[25:22], shift[21:18], shift[17:14]};
end
endmodule
这看起来不错。我认为唯一的问题是你增加了错误的 BCD 数字。
if (shift[21:18] > 4'b0100)
shift[21:18] = shift[17:14] + 4'b0011;
^^^^^ should be 21:18