Binary to BCD in VHDL

Discussion in 'VHDL' started by NA, May 23, 2007.

  1. NA

    NA Guest

    Hello all.
    I'm very new to VHDL and stuck with a simple task.
    This code should convert binary number to BCD number using shift and add 3
    algoritam
    http://www.engr.udayton.edu/faculty/jloomis/ece314/notes/devices/binary_to_BCD/bin_to_BCD.html

    After this code executes I always get "0000" in digit and unit :-(

    You don't have to analyze the whole code, just tell me what is generally
    wrong. Thank you people!

    variable temp: bit_vector(7 downto 0) := "00011000"; --24 (10)
    variable unit: bit_vector(3 downto 0) := "0000";
    variable digit: bit_vector(3 downto 0):= "0000";
    begin
    for i in 0 to 7 loop
    digit := digit sll 1;
    digit(0) := unit(3);
    unit := unit sll 1;
    unit(0) := temp(7);
    temp := temp sll 1;
    --This is the part where I add 3, is there any other way? It must work
    on FPGA
    case digit is
    when "0101" => digit := "1000";
    when "0110" => digit := "1001";
    when "0111" => digit := "1010";
    when "1000" => digit := "1011";
    when "1001" => digit := "1100";
    when others => digit := digit;
    end case;

    case unit is
    when "0101" => unit := "1000";
    when "0110" => unit := "1001";
    when "0111" => unit := "1010";
    when "1000" => unit := "1011";
    when "1001" => unit := "1100";
    when others => unit := digit;
    end case;
    end loop;
    NA, May 23, 2007
    #1
    1. Advertising

  2. I see two problems:

    1) Third line from the bottom: "unit := digit" -> "unit := unit"
    2) The web page talks about "hundreds" too. The line "digit := digit sll
    1;" in your code seems to lose some bits.

    -Michael.
    Michael Jorgensen, May 24, 2007
    #2
    1. Advertising

  3. NA

    Frank Buss Guest

    Frank Buss, May 24, 2007
    #3
  4. NA

    NA Guest

    Thank you both !
    NA, May 24, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Glen Herrmannsfeldt

    Re: binary to BCD assistance

    Glen Herrmannsfeldt, Jul 30, 2003, in forum: VHDL
    Replies:
    4
    Views:
    7,061
  2. Paul Leventis

    Re: binary to BCD assistance

    Paul Leventis, Jul 31, 2003, in forum: VHDL
    Replies:
    4
    Views:
    4,178
    jeppe
    Mar 26, 2008
  3. Fangs
    Replies:
    3
    Views:
    9,789
    darshana
    Oct 26, 2008
  4. Yama
    Replies:
    3
    Views:
    1,337
    Dave Pollum
    Jun 9, 2006
  5. gina
    Replies:
    0
    Views:
    906
Loading...

Share This Page