Binary To Bcd Verilog

Drive a 7-Segment Display using a Double Dabbler on an FPGA

Binary To Bcd Verilog

BCD or The online BCD to Binary Converter is used to convert BCD (Binary-coded decimal) to binary (Base-2) number. Binary-coded Decimal In computing and electronic systems, binary-coded decimal (BCD) is a digital encoding method for decimal numbers in which each digit is represented by its own binary sequence. GitHub - kb000/bin2bcd: Verilog module to convert binary to BCD (binary coded decimal). Binary to BCD Converter This verilog module takes a binary number as an input and outputs its corresponding BCD representation. Currently only following configurations are supported. The BCD stands for Binary Coded Decimal Number. In BCD code, each digit of the decimal number is represented as its equivalent binary number. So, the LSB and MSB of the decimal numbers are represented as its binary numbers. There are the following steps to convert the binary number to BCD: First, we will convert the binary number into decimal.

This module takes an input binary vector and converts it to Binary Coded Decimal (BCD). Binary coded decimal is used to represent a decimal number with four bits. This can be used to convert a binary number to a decimal number than can be displayed on a 7-Segment LED display. The algorithm used in the code below is known as a Double Dabble.

Binary coded decimal uses four bits per digit to represent a decimal number. For example the number 159 in decimal takes 12 bits to represent. This is useful for applications that interface to 7-Segment LEDs, among other things. The reason for this is that each 7-Segment display is treated individually (each gets 4 bits of the 12 bit number in the example above). The FPGA designer needs to know how to drive each digit, and uses BCD to do this. The table for BCD is below.
BCD and Decimal Numbers
BCDDecimal
00000
00011
00102
00113
01004
01015
01106
01117
10008
10019
othersundefined

Let's look at 159. The hundreds digit 1 is represented in binary by 0001. The tens digit 5 is represented in binary by 0101. The ones digit 9 is represented in binary by 1001. The entire number 159 in BCD is therefore: 000101011001. However 159 in binary is represented by 10011111. Again we need a way to convert this binary number 10011111 to its BCD equivalent 000101011001. To do this, we will use the Double Dabble algorithm.

The Double Dabble Algorithm is described in detail on the linked Wikipedia page. But basically it takes the input binary number as a start. It shifts it one bit at a time into the BCD output vector. It then looks at each 4-bit BCD digit independently. If any of the digits are greater than 4, that digit is incremented by 3. This loop continues for each bit in the input binary vector. See the image below for a visual depiction of how the Finite State Machine is written.

VHDL Implementation (Verilog Implementation below)

The VHDL implementation makes use of variables and the input and output widths can be changed by setting the generics. Both variables and generics help to make the code more clean and more flexible.

Binary_to_BCD.vhd:


Verilog Implementation

Binary_To_BCD.v:



Help Me Make Great Content! Support me on Patreon! Buy a Go Board!


BCD code plays an important role in digital circuits. The BCD stands for Binary Coded Decimal Number. In BCD code, each digit of the decimal number is represented as its equivalent binary number. So, the LSB and MSB of the decimal numbers are represented as its binary numbers. There are the following steps to convert the binary number to BCD:

  1. First, we will convert the binary number into decimal.
  2. We will convert the decimal number into BCD.

Let's take an example to understand the process of converting a binary number into BCD

Example 1: (11110)2

1. First, convert the given binary number into a decimal number.

Binary Number: (11110)2

Finding Decimal Equivalent of the number:

StepsBinary NumberDecimal Number
1)(11110)2((1 × 24) + (1 × 23) + (1 × 22) + (1 × 21) + (0 × 20))10
2)(11110)2(16 + 8 + 4 + 2 + 0)10
3)(11110)2(30)10

Decimal number of the Binary number (11110)2 is (30)10

2. Now, we convert the decimal to the BCD

We convert each digit of the decimal number into groups of the four-bit binary number.

Bcd
StepsDecimal NumberConversion
Step 13010(0011)2 (0000)2
Step 23010(00110000)BCD

Result:

(11110)2 = (00110000)BCD

Below is the table that contains the BCD code of the decimal and binary number.

Binary CodeDecimal NumberBCD Code
A B C DB4 :B3B2B1B0
0 0 0 000 : 0 0 0 0
0 0 0 110 : 0 0 0 1
0 0 1 020 : 0 0 1 0
0 0 1 130 : 0 0 1 1
0 1 0 040 : 0 1 0 0
0 1 0 150 : 0 1 0 1
0 1 1 060 : 0 1 1 0
0 1 1 170 : 0 1 1 1
1 0 0 080 : 1 0 0 0
1 0 0 190 : 1 0 0 1
1 0 1 0101 : 0 0 0 0
1 0 1 1111 : 0 0 0 1
1 1 0 0121 : 0 0 1 0
1 1 0 1131 : 0 0 1 1
1 1 1 0141 : 0 1 0 0
1 1 1 1151 : 0 1 0 1

In the above table, the most significant bit of the decimal number is represented by the bit B4, and the least significant bits are represented by B3, B2, B1, and B0. From the above table, we can express the SOP function for different bits of BCD code are as follows:

The K-maps of the above SOP functions are as follows:


BCD to Binary Conversion

The process of converting BCD code into Binary is opposite to the process of converting Binary code into BCD. There are the following steps to convert the BCD code into Binary:

In the first step, we will convert the BCD number into a decimal by making the four-bit groups and finding the equivalent decimal number for each group.

In the last step, we will convert a decimal number into Binary using the process of converting decimal to binary number.

Example 1: (00101000)BCD

Binary To Bcd Conversion Verilog Code

1) Convert BCD to Decimal

Make the groups of 4 digits and find the equivalent decimal number as:

StepsBCD NumberConversion
Step 1(00101000)BCD(0010)2 (1000)2
Step 2(00101000)BCD(2)10 (8)10
Step 3(00101000)BCD(28)10

The decimal number of the given BCD code is: (28)10

2. Convert Decimal to Binary

Use the long division method to convert the decimal number into a binary number as:

StepsOperationResultRemainder
1.28 / 2140
2.14 / 270
3.7 / 231
4.3 / 211
5.1 / 201

Arrange the remainders in the reverse order. So, the LSB of the binary number is the first remainder, and the MSB of the binary number is the last remainder.

The binary number of the decimal number (18)10 is: (11100)2

Result:

(00101000)BCD = (11100)2

Next TopicBinary to Gray code conversion