北京邮电大学数电实验打地鼠.docx
《北京邮电大学数电实验打地鼠.docx》由会员分享,可在线阅读,更多相关《北京邮电大学数电实验打地鼠.docx(24页珍藏版)》请在冰点文库上搜索。
北京邮电大学数电实验打地鼠
北京邮电大学数电实验——打地鼠
2015-2016学年第一学期
数字电路实验报告
实验名称:
打地鼠实验
学院:
信息与通信工程学院
专业:
电子信息工程
班级:
班内序号:
学号:
姓名:
北京邮电大学
时间:
2015年11月8日
一、设计课题的任务要求
1、设计一个挑战反应速度的“打地鼠”游戏,采用用8×8双色点阵显示游戏界面,其
中游戏边界采用绿色LED显示,随机出现的地鼠采用红色LED显示,游戏有16个
洞穴,如图1所示。
2、游戏洞穴每次随机出现一个地鼠,每个地鼠的最长保持时间为2秒,2秒后随机出
现下一个地鼠。
以4×4键盘的按键代表锤子,16个洞穴与16个按键一一对应,一
旦锤子在2秒内击中地鼠,地鼠消失,数码管计分器分数加1分;若锤子一直没有
击中地鼠,2秒后该地鼠消失。
用两个数码管显示游戏成绩,当游戏成绩达到10分
时游戏结束,点阵显示字符“V”。
3、用两个数码管对整个游戏进行倒计时,当游戏时间超过59秒而成绩未达到10分时,
游戏失败,点阵显示字符“X”。
4、按复位键重新开始游戏,并开始倒计时。
二、系统设计
1.设计思路
用8*8双色点阵显示游戏界面,其中游戏边界使用6*6,红色LED灯表示地鼠出现,4*4键盘对应锤子,两个数码管显示游戏倒计时,两个数码管显示分数,当游戏成绩达到十分时游戏结束,点阵显示“V”,当游戏时间超过59秒而成绩未达到10分时,游戏失败,点阵实现“X”。
2.总体框图
是是
否
否
是
3.分块设计
3、仿真波形及波形分析
因按照原程序仿真时间过长,所以将原程序所有分频统一调小1000倍,从而实现仿真中60ms等于实际过程中60s的效果,并在过程中人为设置按键,复位和暂停观察仿真结果。
3.1数码管扫描
如图为键盘扫描波形。
因为6个数码管的管脚连接在一起,无法使其同时显示不同的数字,所以需要对数码管进行扫描,使不同的数码管在不同的时间亮起,显示不同的数字,并通过人眼的视觉暂留效应实现数字的清晰显示。
3.2键盘扫描
如图为键盘扫描波形。
tempclk3(1000Hz)每发生一次,kbrow的一位变为低电平,其余三位为高电平,表示扫描该行,并记录此时数值。
当kbrow的高位到低位依次变为低电平,表示进行了一次完整的扫描。
3.3点阵扫描
如图为点阵扫描波形。
Tempclk4(500Hz)每发生一次,row的一位变为低电平,其余七位为高电平,表示扫描该行,。
当row的高位到低位依次变为低电平,表示进行了一次完整的扫描。
3.4田地边界
如图为田地边界的显示(绿色LED灯)。
此处稍作说明的是,此程序中的边界与课题要求中稍有不同,占据点阵正中央的6*6边界,完全对称。
3.5地鼠出现
如图为地鼠出现模块波形。
可以看到,当未按下或正确按下按键时,colr(红色LED灯)波形2s变化一次,即地鼠2s变化出现。
当正确按下按键,波形立刻变化,即地鼠立刻变换位置。
3.6暂停
如图为按下暂停键之后的波形。
可以看到,点阵(colg和colr)不再亮起。
程序暂停。
3.7复位
如图为按下复位键之后波形。
可以看到,colr的波形按照初始时刻波形重新变化,表示游戏重新开始。
4、源程序
libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
-----------------------------------------------------
entitydadishuis
port(
clk:
instd_logic;
rst:
instd_logic;
-------------------------------------------------------------
kbrow:
instd_logic_vector(3downto0);
kbcol:
outstd_logic_vector(3downto0);---键盘
---------------------------------------------------------
row:
outstd_logic_vector(7downto0);
colg:
outstd_logic_vector(7downto0);
colr:
outstd_logic_vector(7downto0);
--------------------------------------------------------
shumaguan:
outstd_logic_vector(6downto0);
catout:
outstd_logic_vector(5downto0);----数码管
--------------------------------------------------------------------
clear:
instd_logic;--BTN0---复位
-------------------------------------------------------------------
mode:
instd_logic
);
endentity;
architectureaofdadishuis
---------------------------------------------------------------------------------------------------------
TYPEmatrix_indexisarray(29downto0)ofstd_logic_vector(15downto0);
constantdishu:
matrix_index:
=(x"4000",x"0800",x"0001",x"8000",x"0020",x"0010",x"0100",x"0010",x"8000",x"0002",
x"4000",x"0080",x"0010",x"0002",x"0800",x"4000",x"0001",x"0400",x"0020",x"8000",
x"0002",x"0800",x"0001",x"1000",x"0200",x"0008",x"4000",x"0001",x"0008",x"0010"
);
signalcnt:
integerrange0to10000;
signalcnt2:
integerrange0to50000000;
signalclk_tmp:
std_logic;
signalst1:
std_logic_vector(7downto0);--点阵--
signaldata:
std_logic_vector(7downto0);
signalling:
std_logic_vector(1downto0);
signalchange:
integerrange0to29;
signalk:
std_logic_vector(15downto0):
=x"0000";
signalred:
std_logic_vector(7downto0);
signallocation:
std_logic_vector(7downto0);
-----------------------------------------------------------------------------------------------------------
signalscanand:
std_logic_vector(7downto0);
signalcol:
std_logic_vector(3downto0);
signalcntscan:
integerrange0to3;
signalclk_tmp1:
std_logic;
signalkey:
std_logic_vector(15downto0):
=x"1111";--jianpan--
signalcounter1:
integerrange0to3;
signalcounter2:
integerrange0to50000;
signaln:
std_logic_vector(15downto0);
-----------------------------------------------------------------------------------
signalco:
std_logic_vector(6downto0);
signalco1:
std_logic_vector(6downto0);
signalco2:
std_logic_vector(6downto0);
signalco3:
std_logic_vector(6downto0);--记分
signalco4:
std_logic_vector(6downto0);
signalcat:
std_logic_vector(5downto0);
signalup:
integerrange0to1:
=0;
signalcounter3:
integerrange0to25000000;
----------------------------------------------------------------------------
signali:
integerrange0to20;
signalj:
integerrange0to60;
signalvx:
integerrange0to2;
signalstop:
integerrange0to1:
=0;--gameover
-------------------------------------------------------------------------------
signalfuwei:
integerrange0to1:
=0;--复位
begin
ling<="00";
---------------------------------------------------dianzhen
process(clk,rst)
begin
ifrising_edge(clk)andrst='0'then
ifcnt=10000then
cnt<=0;clk_tmp<=notclk_tmp;
else
cnt<=cnt+1;
endif;
endif;
endprocess;
process(clk_tmp1,up,rst,fuwei)
variablecount9:
integerrange0to1000:
=0;
begin
iffalling_edge(clk_tmp1)andfuwei=1then
change<=29;
elsiffalling_edge(clk_tmp1)andfuwei=0then
ifup=1then
count9:
=0;
ifchange=0thenchange<=29;
elsechange<=change-1;
endif;
elsifup=0then
ifcount9=1000then
count9:
=0;
ifchange=0thenchange<=29;
elsechange<=change-1;
endif;
elsecount9:
=count9+1;
endif;
endif;
casechangeis
when29=>k<=dishu(29);
when28=>k<=dishu(28);
when27=>k<=dishu(27);
when26=>k<=dishu(26);--随机
when25=>k<=dishu(25);
when24=>k<=dishu(24);
when23=>k<=dishu(23);
when22=>k<=dishu(22);
when21=>k<=dishu(21);
when20=>k<=dishu(20);
when19=>k<=dishu(19);
when18=>k<=dishu(18);
when17=>k<=dishu(17);
when16=>k<=dishu(16);
when15=>k<=dishu(15);
when14=>k<=dishu(14);
when13=>k<=dishu(13);
when12=>k<=dishu(12);
when11=>k<=dishu(11);
when10=>k<=dishu(10);
when9=>k<=dishu(9);
when8=>k<=dishu(8);
when7=>k<=dishu(7);
when6=>k<=dishu(6);
when5=>k<=dishu(5);
when4=>k<=dishu(4);
when3=>k<=dishu(3);
when2=>k<=dishu
(2);
when1=>k<=dishu
(1);
when0=>k<=dishu(0);
endcase;
endif;
endprocess;
process(clk_tmp,rst)
begin
ifrising_edge(clk_tmp)andrst='0'then
if(vx=2)then
ifst1="00000000"orst1="11111110"then
st1<="01111111";data<="11111100";red<="00000000";
elsifst1="01111111"then
st1<="10111111";data<='1'&"0000"&'1'&ling(1downto0);red<='0'&k(15downto12)&"000";
elsifst1="10111111"then
st1<="11011111";data<='1'&"0000"&'1'&ling(1downto0);red<='0'&k(11downto8)&"000";
elsifst1="11011111"then
st1<="11101111";data<='1'&"0000"&'1'&ling(1downto0);red<='0'&k(7downto4)&"000";
elsifst1="11101111"then
st1<="11110111";data<='1'&"0000"&'1'&ling(1downto0);red<='0'&k(3downto0)&"000";
elsifst1="11110111"then
st1<="11111011";data<="11111100";red<="00000000";
elsifst1="11111011"then
st1<="11111101";data<="00000000";red<="00000000";
elsifst1="11111101"then
st1<="11111110";data<="00000000";red<="00000000";
endif;
elsif(vx=1)then
ifst1="00000000"orst1="11111110"then
st1<="01111111";data<="00000000";red<="00000000";
elsifst1="01111111"then
st1<="10111111";data<="01000001";red<="00000000";
elsifst1="10111111"then
st1<="11011111";data<="00100010";red<="00000000";
elsifst1="11011111"then
st1<="11101111";data<="00010100";red<="00000000";
elsifst1="11101111"then
st1<="11110111";data<="00001000";red<="00000000";
elsifst1="11110111"then
st1<="11111011";data<="00000000";red<="00000000";
elsifst1="11111011"then
st1<="11111101";data<="00000000";red<="00000000";
elsifst1="11111101"then
st1<="11111110";data<="00000000";red<="00000000";
endif;
elsif(vx=0)then
ifst1="00000000"orst1="11111110"then
st1<="01111111";data<="00000000";red<="10000001";
elsifst1="01111111"then
st1<="10111111";data<="00000000";red<="01000010";
elsifst1="10111111"then
st1<="11011111";data<="00000000";red<="00100100";
elsifst1="11011111"then
st1<="11101111";data<="00000000";red<="00011000";
elsifst1="11101111"then
st1<="11110111";data<="00000000";red<="00011000";
elsifst1="11110111"then
st1<="11111011";data<="00000000";red<="00100100";
elsifst1="11111011"then
st1<="11111101";data<="00000000";red<="01000010";
elsifst1="11111101"then
st1<="11111110";data<="00000000";red<="10000001";
endif;
endif;
endif;
endprocess;
row<=st1;
colg<=data;
colr<=red;
----------------------------------------------------------------------------------------------------键盘
process(clk,rst)
begin
ifrising_edge(clk)andrst='0'then
ifcounter2=50000then
counter2<=0;clk_tmp1<=notclk_tmp1;
else
counter2<=counter2+1;
endif;
endif;
endprocess;
process(clk_tmp1,rst)
begin
ifrising_edge(clk_tmp1)andrst='0'then
ifcntscan=3then
cntscan<=0;
else
cntscan<=cntscan+1;
endif;
casecntscanis
when0=>col<="0111";
when1=>col<="1011";
when2=>col<="1101";
when3=>col<="1110";
endcase;
endif;
endprocess;
process(clk_tmp1,rst)
begin
iffalling_edge(clk_tmp1)andrst='0'then
ifkbrow="1111"then
ifcounter1=3then
counter1<=0;
else
counter1<=counter1+1;
endif;
key<=x"1111";
else
counter1<=0;
casescanandis
when"01110111"=>key<=x"8000";
when"01111011"=>key<=x"0800";
when"01111101"=>key<=x"0080";
when"01111110"=>key<=x"0008";
when"10110111"=>key<=x"4000";
when"10111011"=>key<=x"0400";
when"10111101"=>key<=x"0040";
when"10111110"=>key<=x"0004";
when"11010111"=>key<=x"2000";
when"11011011"=>key<=x"0200";
when"11011101"=>key<=x"0020";
when"11011110"=>key<=x"0002";
when"11100111"=>key<=x"1000";