北邮数电综合实验报告.docx
《北邮数电综合实验报告.docx》由会员分享,可在线阅读,更多相关《北邮数电综合实验报告.docx(37页珍藏版)》请在冰点文库上搜索。
北邮数电综合实验报告
北京邮电大学电子工程学院
2012级数字电路与逻辑设计实验报告
点阵赛车游戏
班级:
学号:
班内序号:
姓名:
目录
任务要求3
系统设计4
设计思路4
设计框图4
分块介绍6
仿真波形6
VHDL源程序10
功能说明25
元器件清单25
故障及问题分析26
实验总结26
任务要求
题目六、点阵赛车游戏
基本要求:
1、用8×8点阵进行5秒倒计时显示,如下图所示。
2、当5秒倒计时结束后,点阵显示下图所示的赛道和赛车的初始位置,赛车游戏开始,并开始计时,用两个数码管显示时间。
图中的红色表示赛道,黄色表示赛车的初始位置,箭头表示赛车行进的方向。
3、用BTN1~BTN3三个按键分别控制赛车的左移、前进、右移,最终使赛车在不碰撞赛道的情况下走完全程(即图2中的绿色位置),游戏结束,点阵显示“V”图案,数码管显示走完全程所耗费的时间。
4、当游戏时间超过59秒,或者赛车在行进过程中碰撞赛道,游戏失败,点阵显示“X”图案。
5、通过按键BTN0进行复位,控制点阵返回到图1所示的初始状态。
提高要求:
1、有多种游戏赛道可选,5秒倒计时显示后赛道随机出现。
2、赛车的初始位置随机出现。
3、在赛车行进过程中,赛道中随机出现障碍物(用8×8点阵中的一个LED表示),通过BTN1~BTN3三个按键的控制躲避障碍物,走完全程。
若赛车碰到障碍物和赛道,则游戏失败。
系统设计
设计思路
程序采用自顶向下设计的思路,先将主程序启动,然后依次启动子程序,主程序为点阵倒计时部分,子程序包括数码管显示部分、点阵赛道显示部分、赛车移动部分、逻辑判断部分。
设计框图
系统结构图
点阵
数码管显示
red
green
时间计数器
控制电路
按键
分频器
Clk2
Clk1
reset
cp
ASM图
分块说明
(1)输入部分:
设计方案中有七个输入端,时钟信号输入clk接外部时钟,为计数器提供有效的时钟边沿进行计数;复位信号输入reset为低电平有效,当reset=0时,计数器复位到初始状态“000”,同时赛道恢复最初状态,当reset=1时,计数器正常计数;启动信号输入start为高电平有效,当start=1时,倒计时部分启动。
另有四个输入端控制赛车移动,分别对应左移、右移、前进、后退。
(2)处理部分:
设计方案中此部分包含2个模块:
逻辑判断模块:
判断代表赛车的点所在的行与列是否在赛道范围内,如不在则输出失败信号,如在赛道内进行下一步判断。
按键扫频模块:
通过分频器调整一个时钟信号,对赛车移动的控制按键进行判断,是否移动赛车。
(3)输出部分:
由译码电路,数码管根据a-g以及cat1-cat6的输出显示数字图形,点阵根据row0-7,cul(r)0-7,cul(g)0-7的输出显示赛道及赛车,利用人眼的视觉延缓效应,当时钟信号clk的频率>50Hz,实验中取100Hz以上较佳,但同时不可过大,最大到1-2kHz,就可看到预期实验结果。
仿真波形
倒计时“5”
倒计时“4”:
倒计时“3”:
倒计时“2”:
倒计时“1”:
固定赛道:
数码管计时:
左移,圈出来的表示赛车位置,可见向左移动了一格:
前进:
右移:
成功,显示红色的V:
失败,显示红色的X:
VHDL源程序
LIBRARYIEEE;
USEIEEE.STD_LOGIC_1164.ALL;
useieee.std_logic_unsigned.all;
ENTITYcarIS
PORT(
clk:
INSTD_LOGIC;--pin18
start,reset:
INSTD_LOGIC;--BTN
l,r,f,b:
INSTD_LOGIC;--BTN
ashow:
OUTSTD_LOGIC_VECTOR(23DOWNTO0);--row7-row0cul0-cul7(r)cul0-cul7(g)点阵显示
nstate:
OUTSTD_LOGIC_VECTOR(5DOWNTO0);--cat5-cat0数码管状态
nshow:
OUTSTD_LOGIC_VECTOR(7DOWNTO0));--abcdefgp数码管显示
ENDcar;
ARCHITECTUREmainofcarIS
signalcnt:
integerrange0to25000;
signalcnt1:
integerrange0to25000000;
signalcnt2:
integerrange0to5000000;
signalclk_tmp:
std_logic;
signalclk_tmp1:
std_logic;
signaltclk:
std_logic;
signalclk_tmp2:
std_logic;
signalrow:
std_logic_vector(7downto0);
signalcul:
std_logic_vector(15downto0);
signalaclk:
std_logic;
signaluseclk:
std_logic;
sharedvariabletend:
integerrange0to1;
signaltostart:
std_logic;
sharedvariablesai:
integerrange1to3;
sharedvariablelab:
integerrange-1to1;
sharedvariablerrr:
integerrange0to3000;
sharedvariablerr:
integerrange0to3000;
signalpreset:
std_logic;--逻辑判断模块重置
signalareset:
std_logic;--点阵模块重置
signalnreset:
std_logic;--数码管重置
signaltreset:
std_logic;--计时器重置
signalpwork:
std_logic;--逻辑判断模块工作
signalawork:
std_logic;--点阵工作
signalnwork:
std_logic;--数码管工作
signaltwork:
std_logic;--计时器工作
signalworked:
std_logic;
signalvic:
std_logic;--成功
signalfal:
std_logic;--失败
typenstisarray(0to9)ofstd_logic_vector(7downto0);
signalnshowed:
nst;
sharedvariableashan:
integerrange0to9;
sharedvariableshan:
integerrange0to4;
sharedvariableaashan:
integerrange0to7;
sharedvariableaa1shan:
integerrange0to3;
sharedvariablea1shan:
integerrange0to9;
sharedvariablex:
integerrange-2to5;
sharedvariabley:
integerrange0to7;
sharedvariablettime4:
integerrange0to9;
sharedvariablettime3:
integerrange0to9;
sharedvariablettime2:
integerrange0to9;
sharedvariablettime1:
integerrange0to9;
sharedvariablettime:
integerrange0to9;
begin
nshowed
(1)<="01100000";
nshowed
(2)<="11011010";
nshowed(3)<="11110010";
nshowed(4)<="01100110";
nshowed(5)<="10110110";
nshowed(6)<="10111110";
nshowed(7)<="11100000";
nshowed(8)<="11111110";
nshowed(9)<="11110110";
nshowed(0)<="11111100";
ransaidao:
process(clk)
begin
if(clk'eventandclk='1')then
if(rrr=3000)then
rrr:
=0;
else
rrr:
=rrr+1;
endif;
if(rrr<=1000)then
sai:
=1;
elsif(rrr>1000andrrr<=2000)then
sai:
=2;
else
sai:
=3;
endif;
endif;
endprocess;
rancar:
process(clk)
begin
if(clk'eventandclk='1')then
if(rr=3000)then
rr:
=0;
else
rr:
=rr+1;
endif;
if(rr<=1000)then
lab:
=-1;
elsif(rr>1000andrr<=2000)then
lab:
=0;
else
lab:
=1;
endif;
endif;
endprocess;
tdiv:
process(clk,clk_tmp)--计时器分频
begin
if(clk'eventandclk='1')then
ifcnt=24999then
cnt<=0;
clk_tmp<=notclk_tmp;
else
cnt<=cnt+1;
endif;
endif;
tclk<=clk_tmp;
endprocess;
adiv:
process(clk,clk_tmp1)--点阵分频
begin
if(clk'eventandclk='1')then
ifcnt1=24999999then
cnt1<=0;
clk_tmp1<=notclk_tmp1;
else
cnt1<=cnt1+1;
endif;
endif;
aclk<=clk_tmp1;
endprocess;
usediv:
process(clk,clk_tmp2)--按键控制判断分频
begin
if(clk'eventandclk='1')then
ifcnt2=4999999then
cnt2<=0;
clk_tmp2<=notclk_tmp2;
else
cnt2<=cnt2+1;
endif;
endif;
useclk<=clk_tmp2;
endprocess;
control:
process(clk,reset,start)--控制器
begin
if(clk'eventandclk='1')then
if(start='1')then
tostart<='1';
endif;
if(reset='1'ortend=1)then
preset<='1';areset<='1';nreset<='1';treset<='1';tostart<='0';awork<='0';
else
preset<='0';areset<='0';nreset<='0';treset<='0';
if(tostart='1')then
awork<='1';--启动点阵模块
endif;
endif;
endif;
endprocess;
a:
process(aclk,areset)--点阵工作模块
begin
if(areset='1')then
aa1shan:
=0;
else
if(aclk'eventandaclk='1')then
if(vic='1'orfal='1')then
if(aa1shan<=2)then
aa1shan:
=aa1shan+1;
endif;
endif;
endif;
endif;
endprocess;
process(tclk,areset)
begin
if(areset='1')then
a1shan:
=0;
else
if(tclk'eventandtclk='1')then
if(a1shan=9)then
a1shan:
=0;
else
a1shan:
=a1shan+1;
endif;
endif;
endif;
endprocess;
process(aclk,awork,worked,areset)
begin
if(areset='1')then
aashan:
=0;
else
if(aclk'eventandaclk='1'andawork='1'andworked='0')then
if(aashan=7)then
aashan:
=0;
else
aashan:
=aashan+1;
endif;
endif;
endif;
endprocess;
process(tclk,areset)
begin
if(areset='1')then
ashan:
=0;
else
if(tclk'eventandtclk='1')then
if(ashan=9)then
ashan:
=0;
else
ashan:
=ashan+1;
endif;
endif;
endif;
endprocess;
process(clk,areset,awork)
begin
if(areset='1')then
ashow<="000000000000000000000000";
tend:
=0;worked<='0';twork<='0';pwork<='0';
else
if(clk'eventandclk='1')then
if(awork='1')then
if(worked='0')then
caseaashanis
when0=>
caseashanis
when1=>ashow<="111111111111111100000000";
when0=>ashow<="101111111111011100000000";
when2=>ashow<="110111111000010100000000";
when3=>ashow<="111011111000010100000000";
when4=>ashow<="111101111011010100000000";
when5=>ashow<="111110111001010100000000";
when6=>ashow<="111111011111011100000000";
when7=>ashow<="111111111111111100000000";
when8=>ashow<="111111111111111100000000";
when9=>ashow<="111111111111111100000000";
endcase;
when1=>
caseashanis
when1=>ashow<="111111111111111100000000";
when0=>ashow<="101111111111011100000000";
when2=>ashow<="110111111000010100000000";
when3=>ashow<="111011111000010100000000";
when4=>ashow<="111101111011010100000000";
when5=>ashow<="111110111001010100000000";
when6=>ashow<="111111011111011100000000";
when7=>ashow<="111111111111111100000000";
when8=>ashow<="111111111111111100000000";
when9=>ashow<="111111111111111100000000";
endcase;
when2=>
caseashanis
when1=>ashow<="011111110011110000000000";
when0=>ashow<="101111110010000000000000";
when2=>ashow<="110111110010000000000000";
when3=>ashow<="111011110011110000000000";
when4=>ashow<="111101110000010000000000";
when5=>ashow<="111110110000010000000000";
when6=>ashow<="111111010000010000000000";
when7=>ashow<="111111100011110000000000";
when8=>ashow<="111111111111111100000000";
when9=>ashow<="111111111111111100000000";
endcase;
when3=>
caseashanis
when1=>ashow<="011111110010010000000000";
when0=>ashow<="101111110010010000000000";
when2=>ashow<="110111110010010000000000";
when3=>ashow<="111011110011110000000000";
when4=>ashow<="111101110000010000000000";
when5=>ashow<="111110110000010000000000";
when6=>ashow<="111111010000010000000000";
when7=>ashow<="111111100000010000000000";
when8=>ashow<="111111111111111100000000";
when9=>ashow<="111111111111111100000000";
endcase;
when4=>
caseashanis
when1=>ashow<="011111110011110000000000";
when0=>ashow<="101111110000010000000000";
when2=>ashow<="110111110000010000000000";
when3=>ashow<="111011110011110000000000";
when4=>ashow<="111101110000010000000000";
when5=>ashow<="111110110000010000000000";
when6=>ashow<="111111010000010000000000";
when7=>ashow<="111111100011110000000000";
when8=>ashow<="111111111111111100000000";
when9=>ashow<="111111111111111100000000";
endcase;
when5=>
caseashanis
when1=>ashow<="011111110011110000000000";
when0=>ashow<="101111110000010000000000";
when2=>ashow<="11011111000001000000000