数字逻辑实验报告.docx
- 文档编号:4631120
- 上传时间:2023-05-07
- 格式:DOCX
- 页数:19
- 大小:321.48KB
数字逻辑实验报告.docx
《数字逻辑实验报告.docx》由会员分享,可在线阅读,更多相关《数字逻辑实验报告.docx(19页珍藏版)》请在冰点文库上搜索。
数字逻辑实验报告
北京邮电大学
数字电路与逻辑设计实验报告
实验题目:
掷骰子游戏电路的设计与实现
学生姓名:
班级:
学号:
序号:
目录
一、设计课题的任务要求
二、系统设计
三、仿真波形及波形分析
四、源程序
五、功能说明及资源利用情况
六、故障及问题分析
七、总结和结论
一、设计课题的任务要求
设计并实现一个掷骰子游戏电路。
基本要求:
1、电路可供甲乙二人游戏,游戏者甲使用的按键为BTN0,游戏者乙使用的按键为BTN1。
2、每按一次按键,代表掷一次骰子,可随机得到1~6范围内的两个数字。
3、甲乙按键产生的随机数字分别用数码管DISP0-DISP1、DISP2-DISP3显示,并用DISP7显示比赛局数,比赛结束用8×8点阵显示获胜方,并伴有声音效果。
4、具体游戏规则如下:
(1)第一局比赛,甲乙依次各按一次按键,按键所得两数之和为7或11者胜;若无人取胜,则进行第二局比赛;
(2)第二局比赛,甲乙每人各按一次按键,按键所得二数之和与第一局比赛相同者获胜,若无人获胜,则进行第三局比赛,重复进行步骤
(2),直到出现胜者为止。
(3)游戏局数最多进行六局。
在第六局比赛时,若重复进行步骤
(2)仍未出现胜者,以按键所得两数之和最大者为获胜方。
提高要求:
1、增加多人游戏的功能,数码管可分时记录显示每个游戏者的骰子点数。
2、点阵显示增加游戏开机动画、结束动画,并伴有乐曲播放。
3、自拟其它功能。
二、系统设计
1、设计思路:
按照实验要求,使用状态机分别表示游戏的不同状态;使用分频器来控制时钟;控制器实现具体的游戏规则;8*8LED点阵来显示胜负;数码管显示局数和甲乙掷出的随机数;随机数用一到六的循环产生。
在编译时采用元件例化来生成各自的模块。
流程图如下
2、总体框图:
输入
3、分块设计:
分别包括分频器、防抖器、随机数的产生、判断器、译码器和显示器等模块,综合起来实现所要求的功能。
分频器:
防抖器:
随机数的产生:
判断器:
译码器:
显示器:
三、仿真波形及波形分析
根据甲先乙后的顺序进行仿真如下:
从仿真中可以看出随机数的产生,若时间轴向后移可以将游戏看得更加清楚。
四、源程序
分频器:
libraryieee;
useieee.std_logic_1164.all;
entityfenpingqiis
port(clk:
instd_logic;
clktmp:
outstd_logic);
endfenpingqi;
architectureaoffenpingqiis
signaltmp:
integerrange0to499:
=0;
signalclktmp1:
std_logic;
begin
p3:
process(clk)--frequenttime
begin
ifclk'eventandclk='1'then
iftmp=499then
tmp<=0;clktmp1<=notclktmp1;
else
tmp<=tmp+1;
endif;
endif;
endprocessp3;
clktmp<=clktmp1;
enda;
防抖器:
libraryieee;
useieee.std_logic_1164.all;
entityfangdouqiis
port(clk,btn1,btn2:
instd_logic;
btn1_en,btn2_en:
outstd_logic);
endfangdouqi;
architectureaoffangdouqiis
signaltempcount1:
integerrange0to5:
=0;
signaltempcount2:
integerrange0to5:
=0;
begin
p5:
process(clk,btn1,btn2)
begin
ifclk'eventandclk='1'then
ifbtn1='1'then
iftempcount1=5thentempcount1<=tempcount1;
elsetempcount1<=tempcount1+1;
endif;
iftempcount1=4thenbtn1_en<='1';
elsebtn1_en<='0';
endif;
elsetempcount1<=0;
endif;
ifbtn2='1'then
iftempcount2=5thentempcount2<=tempcount2;
elsetempcount2<=tempcount2+1;
endif;
iftempcount2=4thenbtn2_en<='1';
elsebtn2_en<='0';
endif;
elsetempcount2<=0;
endif;
endif;
endprocessp5;
enda;
计数器
libraryieee;
useieee.std_logic_1164.all;
entityjishuqiis
port(clktmp:
instd_logic;
randnum2:
outintegerrange1to12;
randnum:
outintegerrange1to12);
endjishuqi;
architectureaofjishuqiis
signalrandnum1:
integerrange1to12;
signalrandnum3:
integerrange1to12;
begin
p4:
process(clktmp)--counter1
begin
ifclktmp'eventandclktmp='1'then
ifrandnum1=6then
randnum1<=1;
else
randnum1<=randnum1+1;
endif;
ifrandnum3=7then
randnum3<=1;
else
randnum3<=randnum3+1;
endif;
endif;
ifrandnum3=7then
randnum2<=1;
else
randnum2<=randnum3;
endif;
randnum<=randnum1;
endprocessp4;
enda;
掷骰子结果的产生:
libraryieee;
useieee.std_logic_1164.all;
entitycreatersis
port(clk:
instd_logic;
clktmp:
instd_logic;
btn1_en,btn2_en:
instd_logic;
randnum,randnum1:
inintegerrange1to12;
count:
inintegerrange0to5;
sgn11,sgn22:
outstd_logic;
a1,a2,b1,b2:
outintegerrange1to12;
a3,a4,b3,b4:
outintegerrange1to12);
endcreaters;
architectureaofcreatersis
signala11,a22,b11,b22:
integerrange1to12:
=1;
signala33,a44,b33,b44:
integerrange1to12;
signalsgn1,sgn2:
std_logic:
='0';
begin
p1:
process(a11,a22,a33,sgn1,sgn2,b11,b22,b33,btn1_en,btn2_en,clktmp)
begin
ifclktmp'eventandclktmp='1'then
ifsgn1='1'andsgn2='1'then
sgn1<='0';sgn2<='0';
endif;
ifbtn1_en='1'andsgn1='0'then
sgn1<='1';
a11<=randnum;--getrandnum;
a22<=randnum1;
a33<=randnum1+randnum;
ifcount=0then
a44<=randnum1+randnum;
endif;
endif;
ifbtn2_en='1'andsgn1='1'andsgn2='0'then
sgn2<='1';
b11<=randnum;
b22<=randnum1;
b33<=randnum1+randnum;
ifcount=0then
b44<=randnum1+randnum;
endif;
endif;
endif;
a1<=a11;
a2<=a22;
a3<=a33;
b1<=b11;
b2<=b22;
b3<=b33;
a4<=a44;
b4<=b44;
sgn11<=sgn1;
sgn22<=sgn2;
endprocessp1;
enda;
判断器:
libraryieee;
useieee.std_logic_1164.all;
entitypanduanqiis
port(sgn1,sgn2:
instd_logic;
clk:
instd_logic;
a3,a4,b3,b4:
inintegerrange1to12;
disp77:
outintegerrange1to6;
count1:
outintegerrange0to5;
winsgn:
outstd_logic_vector(1downto0));
endpanduanqi;
architectureaofpanduanqiis
signalcount:
integerrange0to5:
=0;
signaldisp7:
integerrange0to5:
=1;
begin
p7:
process(clk,a3,a4,b3,b4,sgn1,sgn2)--judgment
begin
ifclk'eventandclk='1'then
ifsgn1='1'andsgn2='1'then规定甲先掷乙后掷
ifcount=0then
if(a3=11ora3=7)thenwinsgn<="10";disp7<=disp7+1;count<=0;
elsif(b3=11orb3=7)thenwinsgn<="01";disp7<=disp7+1;count<=0;
elsecount<=count+1;
endif;
elsifcount=5then
if(a3=11ora3=7)thenwinsgn<="10";
elsif(b3=11orb3=7)thenwinsgn<="01";
elsifa3>b3thenwinsgn<="10";
elsewinsgn<="01";
endif;
disp7<=disp7+1;count<=0;
else
ifa3=a4thenwinsgn<="10";disp7<=disp7+1;count<=0;
elsifb3=b4thenwinsgn<="01";disp7<=disp7+1;count<=0;
elsecount<=count+1;
endif;
endif;
endif;
endif;
count1<=count;
if(disp7=7)then
disp77<=1;
else
disp77<=disp7;
endif;
endprocessp7;
enda;
译码器:
libraryieee;
useieee.std_logic_1164.all;
entityyimaqiis
port(clktmp:
instd_logic;
a1,a2,b1,b2:
inintegerrange1to12;
disp7:
inintegerrange1to6;
num:
outstd_logic_vector(6downto0);
cat:
outstd_logic_vector(5downto0));
endyimaqi;
architectureaofyimaqiis
signaltempcat:
std_logic_vector(5downto0);
signaltempnum:
integerrange0to6:
=1;
signalnum1:
std_logic_vector(6downto0);
signalcycle:
integerrange0to4:
=0;
begin
p6:
process(clktmp)--translatethenum1torightnum;
begin
ifclktmp'eventandclktmp='1'then
if(cycle=4)then
cycle<=0;
else
cycle<=cycle+1;
endif;
endif;
casecycleis
when0=>tempnum<=a1;tempcat<="111110";
when1=>tempnum<=a2;tempcat<="111101";
when2=>tempnum<=b1;tempcat<="111011";
when3=>tempnum<=b2;tempcat<="110111";
when4=>tempnum<=disp7;tempcat<="011111";
endcase;
casetempnumis
when0=>num1<="1111110";
when1=>num1<="0110000";
when2=>num1<="1101101";
when3=>num1<="1111001";
when4=>num1<="0110011";
when5=>num1<="1011011";
when6=>num1<="1011111";
endcase;
num<=num1;
cat<=tempcat;
endprocessp6;
enda;
显示器:
libraryieee;
useieee.std_logic_1164.all;
entityxianshiqiis
port(clk:
instd_logic;
winsgn:
instd_logic_vector(1downto0);
row:
outstd_logic_vector(7downto0);
col:
outstd_logic_vector(7downto0)
);
endxianshiqi;
architectureaofxianshiqiis
signalcyc:
integerrange0to7:
=0;
signalrow1:
std_logic_vector(7downto0);
signalcol1:
std_logic_vector(7downto0);
begin
p8:
process(clk)
begin
ifclk'eventandclk='1'then
ifcyc=7then
cyc<=0;
else
cyc<=cyc+1;
endif;
ifwinsgn="10"then
casecycis
when0=>row1<="01111111";col1<="01111100";
when1=>row1<="10111111";col1<="01010100";
when2=>row1<="11011111";col1<="01111100";
when3=>row1<="11101111";col1<="01010100";
when4=>row1<="11110111";col1<="01111100";
when5=>row1<="11111011";col1<="00010000";
when6=>row1<="11111101";col1<="00010000";
when7=>row1<="11111110";col1<="00010000";
endcase;
elsifwinsgn="01"then
casecycis
when0=>row1<="01111111";col1<="11111111";
when1=>row1<="10111111";col1<="01000000";
when2=>row1<="11011111";col1<="00100000";
when3=>row1<="11101111";col1<="00010000";
when4=>row1<="11110111";col1<="00001000";
when5=>row1<="11111011";col1<="00000100";
when6=>row1<="11111101";col1<="10000010";
when7=>row1<="11111110";col1<="11111111";
endcase;
else
endif;
endif;
row<=row1;
col<=col1;
endprocessp8;
enda;
五、功能说明及资源利用情况
分频器对固有频率分频提供给控制器、译码器、显示器合适的时钟;防抖器通过改变频率有效防止毛刺出现,使整个系统工作更加稳定;计数器及骰子结果的产生通过一到六的循环,同时在时钟作用下取数实现骰子数的随机出现;判断器则根据游戏规则采用状态机在时钟作用下来实现,设置变量规定甲乙投掷的顺序;译码器是使数码管在循环扫描作用下实现对掷出骰子数及局数的显示;8*8LED点阵编程控制行列高低电平同时循环扫描来实现对甲乙胜负的显示。
所有模块在分频器作用下综合起来一起工作实现掷骰子的游戏。
采用元件例化,尽可能提高资源利用率。
六、故障及问题分析
初次编译程序,在调试过程中,系统工作不稳定,不能跟随按键及时显示,在老师的建议下加入了防抖器,使得系统的工作更稳定;
8*8LED点阵在显示时不稳定,有些地方有不应该亮的点亮起,经过反复试验,与同学交流,改变了行和列的编译方式,解决了问题;
8*8LED点阵显示亮度不够,改变了频率;
初次编译时,甲乙的投掷先后易引起混乱,引入了变量规定甲乙的先后顺序。
七、总结和结论
在这次数电实验中我学到了很多。
在初次拿到题目时比较混乱,不知从何处下手,通过与同学讨论,学习VHDL的相关内容,思路也渐渐清晰起来,画出了流程图及模块分布。
在编写程序的过程中,采纳建议使用了元件例化,使整个系统更加清晰,编写更加有的放矢,纠错也更容易,通过一次次的调试改进,加防抖器及改变频率,终于基本完成了游戏的设计。
在编写过程中最为困难的是8*8LED点阵的显示,之前对这个器件不太了解,查了好多资料弄清楚他的工作原理、编写方法,后来又在反复调试中改进,实现了其完美的显示。
我认识到在做实验时,一定要积极主动地去学习,查资料,不要怕犯错,要坚持不懈,并且及时与老师同学沟通,取长补短,发现不足之处。
要积极地去实践,才可以真正提高自己的逻辑思维和动手能力。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数字 逻辑 实验 报告
![提示](https://static.bingdoc.com/images/bang_tan.gif)