Binary to BCD



Binary to BCD

10/3/08

|Dec |Binary |BCD |Dec |

|0 |0000 |0 0000 |0 |

|1 |0001 |0 0001 |1 |

|2 |0010 |0 0010 |2 |

|3 |0011 |0 0011 |3 |

|4 |0100 |0 0100 |4 |

|5 |0101 |0 0101 |5 |

|6 |0110 |0 0110 |6 |

|7 |0111 |0 0111 |7 |

|8 |1000 |0 1000 |8 |

|9 |1001 |0 1001 |9 |

|10 |1010 |1 0000 |16 = 10 + 6 |

|11 |1011 |1 0001 |17 = 11 + 6 |

|12 |1100 |1 0010 |18 = 12 + 6 |

|13 |1101 |1 0011 |19 = 13 + 6 |

|14 |1110 |1 0100 |20 = 14 + 6 |

|15 |1111 |1 0101 |21 = 15 + 6 |

We will now present, without proof, an algorithm for converting binary to BCD.

Algorithm for converting N-bit binary number to BCD:

1. Set up a Result Shift Register (RSR) and a Binary Shift Register (BSR).

2. Clear the RSR and load Binary value into BSR. Set count to 1.

3. Shift most significant bit (MSB) of BSR into least significant bit (LSB) of RSR.

4. If cont = N then exit else:

5. Check each BCD digit of RSR. For BCD digits ( 5 add 3 to that digit.

6. Increment count.

7. Go to step 3.

Example: Consider 0FFH = 255. Let N = 12.

|Count |RSR |BSR |Step |

|1 |0000 0000 0000 |0000 1111 1111 |2 |

| |0000 0000 0000 |0001 1111 111- |3 - Shift |

| |0000 0000 0000 |0001 1111 111- |5 – add 3 if ( 5 |

|2 |0000 0000 0000 |0011 1111 11-- |3 - Shift |

| |0000 0000 0000 |0011 1111 11-- |5 – add 3 if ( 5 |

|3 |0000 0000 0000 |0111 1111 1--- |3 - Shift |

| |0000 0000 0000 |0111 1111 1--- |5 – add 3 if ( 5 |

|4 |0000 0000 0000 |1111 1111 ---- |3 - Shift |

| |0000 0000 0000 |1111 1111 ---- |5 – add 3 if ( 5 |

|5 |0000 0000 0001 |1111 111- ---- |3 - Shift |

| |0000 0000 0001 |1111 111- ---- |5 – add 3 if ( 5 |

|6 |0000 0000 0011 |1111 11-- ---- |3 - Shift |

| |0000 0000 0011 |1111 11-- ---- |5 – add 3 if ( 5 |

|7 |0000 0000 0111 |1111 1--- ---- |3 - Shift |

| |0000 0000 0011 | |5 – add 3 if ( 5 |

| | |1111 1--- ---- | |

|8 | |1111 ---- ---- |3 - Shift |

| | | |5 – add 3 if ( 5 |

| | | | |

|9 | |111- ---- ---- |3 - Shift |

| | |111- ---- ---- |5 – add 3 if ( 5 |

|10 | |11-- ---- ---- |3 - Shift |

| | | |5 – add 3 if ( 5 |

| | |11-- ---- ---- | |

|11 | |1--- ---- ---- |3 - Shift |

| | | |5 – add 3 if ( 5 |

| | |1--- ---- ---- | |

|12 | |---- ---- ---- |3 - Shift |

Algorithm for converting N-bit binary number to BCD:

1. Set up a Result Shift Register (RSR) and a Binary Shift Register (BSR).

2. Clear the RSR and load Binary value into BSR. Set count to 1.

3. Shift most significant bit (MSB) of BSR into least significant bit (LSB) of RSR.

4. If cont = N then exit else:

5. Check each BCD digit of RSR. For BCD digits ( 5 add 3 to that digit.

6. Increment count.

7. Go to step 3.

Example: Consider 0FFH = 255. Let N = 12.

|Count |RSR |BSR |Step |

|1 |0000 0000 0000 |0000 1111 1111 |2 |

| |0000 0000 0000 |0001 1111 111- |3 - Shift |

| |0000 0000 0000 |0001 1111 111- |5 – add 3 if ( 5 |

|2 |0000 0000 0000 |0011 1111 11-- |3 - Shift |

| |0000 0000 0000 |0011 1111 11-- |5 – add 3 if ( 5 |

|3 |0000 0000 0000 |0111 1111 1--- |3 - Shift |

| |0000 0000 0000 |0111 1111 1--- |5 – add 3 if ( 5 |

|4 |0000 0000 0000 |1111 1111 ---- |3 - Shift |

| |0000 0000 0000 |1111 1111 ---- |5 – add 3 if ( 5 |

|5 |0000 0000 0001 |1111 111- ---- |3 - Shift |

| |0000 0000 0001 |1111 111- ---- |5 – add 3 if ( 5 |

|6 |0000 0000 0011 |1111 11-- ---- |3 - Shift |

| |0000 0000 0011 |1111 11-- ---- |5 – add 3 if ( 5 |

|7 |0000 0000 0111 |1111 1--- ---- |3 - Shift |

| |0000 0000 0011 | |5 – add 3 if ( 5 |

| |0000 0000 1010 |1111 1--- ---- | |

|8 |0000 0001 0101 |1111 ---- ---- |3 - Shift |

| |0000 0000 0011 | |5 – add 3 if ( 5 |

| |0000 0001 1000 | | |

|9 |0000 0011 0001 |111- ---- ---- |3 - Shift |

| |0000 0011 0001 |111- ---- ---- |5 – add 3 if ( 5 |

|10 |0000 0110 0011 |11-- ---- ---- |3 - Shift |

| |0000 0011 0000 | |5 – add 3 if ( 5 |

| |0000 1001 0011 |11-- ---- ---- | |

|11 |0001 0010 0111 |1--- ---- ---- |3 - Shift |

| |0000 0000 0011 | |5 – add 3 if ( 5 |

| |0001 0010 1010 |1--- ---- ---- | |

|12 |0010 0101 0101 |---- ---- ---- |3 - Shift |

Func_to_BCD

Using the above algorithm write the function to_bcd that converts a 16 bit unsigned binary value to BCD.

-- func_to_bcd.vcd V 1.2 10/12/07

library IEEE;

use IEEE.STD_LOGIC_1164.ALL;

use IEEE.STD_LOGIC_ARITH.ALL;

use IEEE.STD_LOGIC_UNSIGNED.ALL;

package func_to_bcd is

function to_bcd (binary: std_logic_vector) return std_logic_vector;

end func_to_bcd;

library IEEE;

use IEEE.STD_LOGIC_1164.ALL;

use IEEE.STD_LOGIC_ARITH.ALL;

use IEEE.STD_LOGIC_UNSIGNED.ALL;

package body func_to_bcd is

-----------------------------------------------------------------------

--Function: to_bcd - Converts 16 bit input to BCD. If input > 9999 then

-- returns x"9999".

-- Input: binary - 16 bit unsigned value.

-- Returns: BCD unsigned.

-- Ussage: BCD = to_bcd(binary);

-----------------------------------------------------------------------

function to_bcd(binary: std_logic_vector) return STD_LOGIC_VECTOR is

variable rsr : std_logic_vector(15 downto 0); -- Shift reg for BCD result

begin

if CONV_INTEGER(binary) > 9999 then

rsr := x"9999";

else

-- Enter your code to complete this function

end if;

return rsr;

end to_bcd;

end func_to_bcd;

-- tb_to_bcd.vhd Version 1.1 by Dr. Jerry H. Tucker

-- Test bench for to_bcd.vhd

-- Created 10/12/07

library IEEE;

use IEEE.STD_LOGIC_1164.ALL;

use IEEE.STD_LOGIC_ARITH.ALL;

use IEEE.STD_LOGIC_UNSIGNED.ALL;

use work.all;

use func_to_bcd.all;

-- uses to_bcd in package func_to_bcd.vhd

entity tb is

end tb;

architecture test of tb is

signal bi, bcd: std_logic_vector(15 downto 0) := x"ffff";

signal int: integer;

begin

-- Use linear feedback shift register to

-- generate random binary values.

bi ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download