Search code examples
vhdlhardware

convert integer values to ASCII characters - VHDL


I need to show some integer values on a 2x16 display, but the display recognizes only ASCII characters. So before I send some value to the display I need to convert it to string... Any ides?

Thanks a lot!


Solution

  • There is a way to do that. So at the beginning yo need to convert the integer to a std_logic_vector and then convert that value to a BCD system, so you have binary values of each digit of an integer value. Then just save the ASCII values of each digit to an array of std_logic_vector-s, so you can display them...

    type     x_digits is array ( 3 downto 1) of natural;
    signal   x_digit_tb : x_digits;
    constant d0 : std_logic_vector (7 downto 0) := "00110000"; -- \\
    constant d1 : std_logic_vector (7 downto 0) := "00110001"; -- ||
    constant d2 : std_logic_vector (7 downto 0) := "00110010"; -- ||
    constant d3 : std_logic_vector (7 downto 0) := "00110011"; -- ||
    constant d4 : std_logic_vector (7 downto 0) := "00110100"; -- \\
    constant d5 : std_logic_vector (7 downto 0) := "00110101"; --  >> Constant ASCII-binary value of digits 0 to 9
    constant d6 : std_logic_vector (7 downto 0) := "00110110"; -- //
    constant d7 : std_logic_vector (7 downto 0) := "00110111"; -- ||
    constant d8 : std_logic_vector (7 downto 0) := "00111000"; -- ||
    constant d9 : std_logic_vector (7 downto 0) := "00111001"; -- //
    .
    .
      process(CLK)
              ----- Convert X to BCD -----
              if CLK'event and clk = '1' and dX0 = 1 then
                    x_shift(7 downto 0) := x_ein_local;
                    for l in 0 to 7 loop
                        x_shift := x_shift sll 1;
                        if  l < 7 and x_shift(11 downto 8) > "0100" then
                            x_shift(11 downto  8) := x_shift(11 downto  8) + "0011";
                        end if;
                        if  l < 7 and x_shift(15 downto 12) > "0100" then
                            x_shift(15 downto 12) := x_shift(15 downto 12) + "0011";
                        end if;
                        if  l < 7 and x_shift(19 downto 16) > "0100" then
                            x_shift(19 downto 16) := x_shift(19 downto 16) + "0011";
                        end if;
                    end loop;
                    x_BCD <= x_shift(19 downto  8);
                    x_shift := (others => '0');
                    dX0 <= 2;
                ---- Convert X to ASCII --------    
                if CLK'event and clk = '1' and dX0 = 2 then
                    for k in 1 to 3 loop
                        case k is
                            when 1 => WW := x_BCD( 3 downto  0);
                            when 2 => WW := x_BCD( 7 downto  4);
                            when 3 => WW := x_BCD(11 downto  8);
                        end case;
                        case WW is
                            when "0000" => x_digit(k) <= d0;
                            when "0001" => x_digit(k) <= d1;
                            when "0010" => x_digit(k) <= d2;
                            when "0011" => x_digit(k) <= d3;
                            when "0100" => x_digit(k) <= d4;
                            when "0101" => x_digit(k) <= d5;
                            when "0110" => x_digit(k) <= d6;
                            when "0111" => x_digit(k) <= d7;
                            when "1000" => x_digit(k) <= d8;
                            when "1001" => x_digit(k) <= d9;
                            when others => null;
                        end case;   
                    end loop;
                    dX0 <= 3;
                end if;
            end if;
     end process;
    

    The values saved in x_digits are ASCII values that could be displayed...