您的位置:首页 > 其它

二——十进制转换器

2015-06-19 13:45 246 查看
要求:输入一个八位二进制数,在数码管上以十进制方式显示(0~255)

程序代码如下:

library ieee;

use ieee.std_logic_1164.all;

use ieee.std_logic_arith.all;

use ieee.std_logic_unsigned.all;

package packagexp is

    function transform(a : integer)

        return std_logic_vector;

end;

package body packagexp is

    function transform(a : integer)

       return std_logic_vector is

       

       variable temp : std_logic_vector(7 downto 0);

      begin

          case a is

              when 0 => temp :="00111111" ;  --0
     when 1 => temp :="00000110" ;  --1
     when 2 => temp :="01011011" ;  --2
     when 3 => temp :="01001111" ;  --3
       when 4 => temp :="01100110" ;  --4
     when 5 => temp :="01101101" ;  --5
     when 6 => temp :="01111101" ;  --6
     when 7 => temp :="00000111" ;  --7
     when 8 => temp :="01111111" ;  --8
     when 9 => temp :="01101111" ;  --9
     when others => null;

          end case;

          return temp;

    end function transform;

end;
   

library ieee;

use ieee.std_logic_1164.all;

use ieee.std_logic_arith.all;

use ieee.std_logic_unsigned.all;

use work.packagexp.all;

entity y2_10 is 

port( clk : std_logic;                          --数码管扫描时钟

      a,b,c,d,e,f,g,h:in std_logic;             --8位二进制数
 scan:out std_logic_vector(5 downto 0);       --数码管扫描参数
 light:out std_logic_vector( 7 downto 0)      --彩灯输出
 );

end y2_10;

architecture behv of y2_10 is

  signal din : std_logic_vector(7 downto 0);        --输入的八位二进制数

  signal t   : std_logic_vector(2 downto 0);        --动态扫描参数

  signal d3,d2,d1: std_logic_vector(7 downto 0);    --存储十进制数的百位,十位,个位

  signal result : integer range 0 to 255;

  begin

  

  process(din)    --转换进程

    variable temp : integer range 0 to 255 :=0;       --暂存转换结果

    begin
   din <= a&b&c&d&e&f&g&h;
   --可以用函数一步得到整数

        result <= conv_integer(din);

  end process;

  

  process(result)                --根据得到的整数值计算百位,十位,个位

    variable s3,s2,s1 :integer range 0 to 9;         --暂存整数型各个数位

    variable save:integer range 0 to 255;
begin
 s1 := 0; s2 := 0; s3 := 0; 
 --取出百位
 save := result;
 for n in 0 to 2 loop
     if save > 99 then
         s3 := s3 + 1;
         save := save - 100;
     else
        exit;
     end if;
 end loop; 
 
--取出十位
--save值是只剩十位数个位数,例如51
for n in 0 to 9 loop
   if save > 9 then
       save := save - 10;
       s2 := s2 + 1;
   else
       exit;
   end if;
end loop;

--取出个位
s1 := save;

d3 <= transform(s3);
d2 <= transform(s2);
d1 <= transform(s1);

  end process;

  

  process(clk)                 --数码管动态刷新

    begin
   if clk'event and clk = '1' then
   if t="101" then
   t <= "000";
else
   t <= t + 1;
end if;
end if;

  end process;

  

  process(t,d1,d2,d3)

    begin
  case t is
    when "000" => scan <= "000001"; light <= d1;
  when "001" => scan <= "000010"; light <= d2;
  when "010" => scan <= "000100"; light <= d3;
  when "011" => scan <= "001000"; light <= "00000000";
  when "100" => scan <= "010000"; light <= "00000000";
  when "101" => scan <= "100000"; light <= "00000000";
  when others => scan <= null;
end case;

  end process;

end behv;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  VHDL 进制转换