【文件属性】:
文件名称:EDA课程设计
文件大小:14KB
文件格式:DOCX
更新时间:2015-06-26 13:33:26
EDA
程序可行library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity mudecoder is
port(binaryin:in std_logic_vector(1 to 5);
--5位二进制码的输入端口
bcdout1:out std_logic_vector(1 to 7); --七段译码器输出端口
bcdout2:out std_logic_vector(1 to 7)
);
end mudecoder ;
architecture m of mudecoder is
signal tembinaryin:std_logic_vector(1 to 5);
begin
process(binaryin)
begin
tembinaryin<=binaryin;
case tembinaryin is
--把0到9的5位二进制码转换成七段译码
when"00000"=>bcdout1<="1111110";bcdout2<="1111110";
when"00001"=>bcdout1<="1111110";bcdout2<="0110000";
when"00010"=>bcdout1<="1111110";bcdout2<="1101101";
when"00011"=>bcdout1<="1111110";bcdout2<="1111001";
when"00100"=>bcdout1<="1111110";bcdout2<="0110011";
when"00101"=>bcdout1<="1111110";bcdout2<="1011011";
when"00110"=>bcdout1<="1111110";bcdout2<="1011111";
when"00111"=>bcdout1<="1111110";bcdout2<="1110000";
when"01000"=>bcdout1<="1111110";bcdout2<="1111111";
when"01001"=>bcdout1<="1111110";bcdout2<="1111011";
--把10到19的5位二进制码转换成七段译码
when"01010"=>bcdout1<="0110000";bcdout2<="1111110";
when"01011"=>bcdout1<="0110000";bcdout2<="0110000";
when"01100"=>bcdout1<="0110000";bcdout2<="1101101";
when"01101"=>bcdout1<="0110000";bcdout2<="1111001";
when"01110"=>bcdout1<="0110000";bcdout2<="0110011";
when"01111"=>bcdout1<="0110000";bcdout2<="1011011";
when"10000"=>bcdout1<="0110000";bcdout2<="1011111";
when"10001"=>bcdout1<="0110000";bcdout2<="1110000";
when"10010"=>bcdout1<="0110000";bcdout2<="1111111";
when"10011"=>bcdout1<="0110000";bcdout2<="1111011";
--把20到21的5位二进制码转换成七段译码
when"10100"=>bcdout1<="1101101";bcdout2<="1111110";
when"10101"=>bcdout1<="1101101";bcdout2<="0110000";
--如果5位二进制码不在0到21范围内,那么两个七段译码器都显示0
when others=>bcdout1<="1101101";bcdout2<="1111110";
end case;
end process;
end m;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all; --引用必要的库函数和包集合
entity pingponggame is --实体名为pingponggame
port(reset:in std_logic;
clk:in std_logic;
startbutton:in std_logic; --开始游戏输入端口
serve:in std_logic_vector(1 to 2); --发球输入端口
hit1,hit2:in std_logic; --甲和乙的击球输入端口
--控制8个发光二极管的输出端口
light:out std_logic_vector(1 to 8);
score11,score12,score21,score22:out std_logic_vector(1 to 7)); --4个用于控制4个7段译码器的输出端口
end pingponggame;
architecture game of pingponggame is
type pingpong is (waitserve,light1on,ballmoveto2,allow2hit,
light8on,ballmoveto1,allow1hit);
---设置7个状态,为枚举数据类型,记为pingpong
signal state:pingpong;
signal i:integer range 0 to 8;
signal count1,count2:std_logic_vector(1 to 5):="00000";
---内部计数器,是5位二进制变量
component mudecoder is
port(binaryin: in std_logic_vector(1 to 5);
bcdout1:out std_logic_vector(1 to 7);
bcdout2:out std_logic_vector(1 to 7)
);
end component; ---调用记分译码器
begin
process(clk) --状态机进程
begin --进程开始
if reset='1' then --异步置位
i<=0;count1<="00000";count2<="00000";
elsif clk'event and clk='1' then --当处于时钟inclock上升沿时
if count1="10101"or count2="10101"then
i<=0;count1<="00000";count2<="00000";
elsif startbutton='0' then
i<=0;count1<="00000";count2<="00000";
else --以下case语句是程序中最关键的状态机部分
case state is
when waitserve=> --进程处于等待发球状态
case serve is
when "10"=> i<=1;state<=light1on;
when "01"=> i<=8;state<=light8on;
when "11"=>i<=0;
when others=> i<=0;
end case;
when light> --进程处于第一盏灯亮状态
i<=2;
if hit2='1' then
i<=0;
count1<=count1+1;state<=waitserve;
else
state<=ballmoveto2;
end if;
when light> --进程处于第八盏灯亮状态
i<=7;
if hit1='1' then
i<=0;
count2<=count2+1;state<=waitserve;
else
state<=ballmoveto1;
end if;
when ballmoveto1=> --进程处于球向甲移动状态
if hit1='1' then
i<=0;
count2<=count2+1;state<=waitserve;
elsif i=2 then i<=1;
state<=allow1hit;
else i<=i-1;
end if;
when ballmoveto2=> --进程处于球向乙移动状态
if hit2='1'then
i<=0;
count1<=count1+1;state<=waitserve;
elsif i=7 then i<=8;
state<=allow2hit;
else i<=i+1;