Binary to BCD



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 |

| |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.

-- pkg_func.vcd V 1.0 1/30/09

library IEEE;

use IEEE.STD_LOGIC_1164.ALL;

use IEEE.STD_LOGIC_ARITH.ALL;

use IEEE.STD_LOGIC_UNSIGNED.ALL;

package pkg_func is

function to_bcd (binary: std_logic_vector) return std_logic_vector;

end pkg_func;

library IEEE;

use IEEE.STD_LOGIC_1164.ALL;

use IEEE.STD_LOGIC_ARITH.ALL;

use IEEE.STD_LOGIC_UNSIGNED.ALL;

package body pkg_func 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

variable bsr : std_logic_vector(15 downto 0); -- Shift reg for binary input

variable correction : std_logic_vector(15 downto 0); -- Add to do BCD correction

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 pkg_func;

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

-- Test bench for function to_bcd in pkg_func.vhd

-- Created 1/30/09

library IEEE;

use IEEE.STD_LOGIC_1164.ALL;

use IEEE.STD_LOGIC_ARITH.ALL;

use IEEE.STD_LOGIC_UNSIGNED.ALL;

use work.all;

use pkg_func.all;

-- uses to_bcd in package pkg_func.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 values.

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

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

Google Online Preview   Download