Infrastructure projects (10) BCD binary switch programming to explain

EDITORIAL words

In the previous section of learning, we have mastered to convert binary number to methods BCD code. So now we turn to think about, what kind of circuit design, it can be converted into a binary code BCD it?

basic concepts

In mathematics, we know just a decimal number such as 5468, its calculations can be converted as follows: 5468 = 5 * 1000 + 4 * 100 + 6 * 10 + 8, so BCD to algorithm produces binary numbers is:

abcd = a*1000 + b*100 + c*10 +d

This algorithm is an algorithm that most conventional, which need to use multipliers and adders, this implementation resources consuming, the following will introduce a dream wing brothers algorithm that will need to add and shift complete BCD to binary function, thereby saving logic resources as possible.

Shift algorithm principle

Before describing this algorithm, the dream first wing to explain a little brother problem: a left binary code is not equal to the left of the binary code * 2, for example, binary code 101001, equal to decimal 41, a left 1,010,010 bits obtained, equal to decimal 82.

That binary code left a plus left three can be equivalent to a binary code multiplied by 10, then if we can take advantage of a shift instead of multiplication? Here we have to design a circuit input 3 bcd bit code into a binary code, the function of rotation of binary BCD code.

Top-level block diagram

 

Top-level module port description

Port Name

Port Description

clk

50MHz system clock input

rst_n

System Reset Low

bw

BCD code one hundred input

Shiva

BCD input code ten

wt

BCD code bits are input

binary

Outputting the converted binary number

Code

/****************************************************          

 * Engineer: Dream Brother Wing

 *   QQ             :   761664056

 * The module function: BCD binary code switch module *************************************** ************** /

01  module bcd_to_bin(

CLK 02 ,    // system clock 50Mhz

RST_N 03 ,  // low reset system

BW 04 ,    one hundred // input BCD code

Shiw 05 ,   // enter the ten-digit BCD code

GEW 06 ,    a bit // input BCD code   

07 binary // binary output

08              );

09                  

10  input clk;

11  input rst_n;

12 is   INPUT  [ . 3 : 0 ]  BW ;   // one hundred

13  input [3:0] shiw; //十位

14  input [3:0] gew;  //个位

15  

16  output [9:0] binary; //转换结果

17  

18  reg [9:0] bwValue1; //百位BCD码转换寄存器1

19  reg [9:0] bwValue2; //百位BCD码转换寄存器2

20  reg [9:0] bwValue3; //百位BCD码转换寄存器3

21  reg [9:0] shiwValue1; //十位BCD码转换寄存器1

22  reg [9:0] shiwValue2; //十位BCD码转换寄存器2

23  reg [9:0] gewValue;    //个位BCD码转换寄存器

24  

25  /***********转换操作*******************/

26  always @(posedge clk or negedge rst_n)

27  if (!rst_n)

28      begin //寄存器赋初值

29          bwValue1 <= 0; 

30          bwValue2 <= 0;

31          bwValue3 <= 0; 

32          shiwValue1 <= 0;

33          shiwValue2 <= 0;

34          gewValue <= 0;

35      end

36  else

37      begin

38      //由我们得出的规律可知:a*100=a*(64+32+4)=a*64+a*32+a*4

39      //=a000000+a00000+a00,即a左移6位加上左移5位加上a左移2位 

40          bwValue1 <= bw<<6; 

41          bwValue2 <= bw<<5; 

42          bwValue3 <= bw<<2; 

43      //由我们得出的规律可知:a*10=a*(8+2)=a*8+a*2 =a000+a0,

44      //即a左移3位加上左移1位               

45          shiwValue1 <= shiw<<3;

46          shiwValue2 <= shiw<<1;    

47          gewValue <= gew; //个位数据不变

48      end 

49  //将所有的各个位的转换结果相加就是转换后的二进制数

50  assign binary = bwValue1 + bwValue2 + bwValue3 + shiwValue1

51                  + shiwValue2 + gewValue;

52  

53  endmodule

18~23行我们定义了BCD码转换需要用到的寄存器,因为我们从算法原理这一部分中总结的规律是:百位的BCD码转换需要(a*100=a*(64+32+4)=a*64+a*32+a*4=a000000+a00000+a00,即a左移6位加上左移5位加上a左移2位)三部分组成,所以需要三组寄存器,同理十位和个位也分别需要两组和一组寄存器;37~48行测试执行移位操作,50~51行将移位的结果输出。

 测试代码如下:

/****************************************************          

 *   Engineer      :   梦翼师兄

 *   QQ             :   761664056

 *   The module function: BCD码转二进制数测试模块 *****************************************************/

01  `timescale 1ns/1ps

02

03  module bcd_to_bin_tb;

04

05      reg clk;

06      reg rst_n;

07      reg [3:0] bw;   //百位

08      reg [3:0] shiw; //十位

09      reg [3:0] gew;  //个位

10      

11      wire [9:0] binary;

12      

13      initial begin

14          clk = 0;

15          rst_n = 0;

16         bw = 4'd0; shiw = 4'd0; gew = 4'd0;

17          #1000 rst_n = 1;

18          

19          #100 bw = 4'd1; shiw = 4'd2; gew = 4'd0; //120

20          #100 bw = 4'd3; shiw = 4'd2; gew = 4'd9; //329

21          #100 bw = 4'd7; shiw = 4'd0; gew = 4'd3; //703

22          #100 bw = 4'd0; shiw = 4'd2; gew = 4'd7; //027

23          #100 bw = 4'd2; shiw = 4'd9; gew = 4'd0; //290

24          

25      end

26      

27      always #10 clk = ~clk;

28      

29       bcd_to_bin bcd_to_bin(

30        .clk(clk),         //系统50Mhz时钟

31        .rst_n(rst_n),   //系统低电平复位

32        .bw(bw),         //输入的BCD码的百位

33        .shiw(shiw),     //输入的BCD码的十位

34        .gew(gew),       //输入的BCD码的个位   

35        .binary(binary)  //输出的二进制数

36       );

37      

38  endmodule

我们在测试代码中写入了5组BCD码来检测输出是否正确

仿真分析

 

 从仿真图可以看出,分别输入5组BCD码:120、329、703、27、290,观察输出可知转换之后的二进制数是正确的,成功的把BCD码转换成了二进制码。所以本次设计是成功的。

 

Guess you like

Origin www.cnblogs.com/mengyi1989/p/11521080.html
Recommended