二——十进制转换器
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;
程序代码如下:
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;
相关文章推荐
- PHP中实现中文字符进制转换原理分析
- JS中字符问题(二进制/十进制/十六进制及ASCII码之间的转换)
- php 实现进制转换(二进制、八进制、十六进制)互相转换实现代码
- javascript parseInt() 函数的进制转换注意细节
- 进制转换算法原理(二进制 八进制 十进制 十六进制)
- Shell脚本中不同进制数据转换的例子(二进制、八进制、十六进制、base64)
- 使用JavaScript进行进制转换将字符串转换为十进制
- Javascript进制转换实例分析
- Python中不同进制互相转换(二进制、八进制、十进制和十六进制)
- 十六进制转换八进制
- Javascript进制转换
- C#生成短地址+任意进制转换
- 进制之间的转换
- 用栈实现任意进制转换
- 进制转换
- 二进制,十六进制,八进制 十进制的转换
- [HdOJ]2031 进制转换
- 九度 题目1026:又一版 A+B java
- 原码 补码 反码及进制转换
- VITAL X_MULT18X18.vhd Example