新手FPGA、VERILOG编程学习3——二进制数与8421码的转换案例——适合新手小白

四位二进制数转化成两个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

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值