首页 > 解决方案 > 14 位二进制到 4 位(16 位)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

标签: verilog

解决方案


这看起来很不错。我认为唯一的问题是您增加了错误的 BCD 数字。

if (shift[21:18] > 4'b0100)
    shift[21:18] = shift[17:14] + 4'b0011;
                         ^^^^^ should be 21:18

推荐阅读