1、数字逻辑实验报告北京邮电大学数字电路与逻辑设计实验报告实验题目:掷骰子游戏电路的设计与实现学生姓名:班级:学号: 序号:目录一、 设计课题的任务要求二、 系统设计三、 仿真波形及波形分析四、 源程序五、 功能说明及资源利用情况六、 故障及问题分析七、 总结和结论一、 设计课题的任务要求设计并实现一个掷骰子游戏电路。基本要求:1、电路可供甲乙二人游戏,游戏者甲使用的按键为 BTN0,游戏者乙使用的按键为 BTN1。2、每按一次按键,代表掷一次骰子,可随机得到 16 范围内的两个数字。3、甲乙按键产生的随机数字分别用数码管 DISP0-DISP1、DISP2-DISP3 显示,并用 DISP7 显
2、示比赛局数,比赛结束用 88 点阵显示获胜方,并伴有声音效果。4、具体游戏规则如下:(1)第一局比赛,甲乙依次各按一次按键,按键所得两数之和为7或11者胜;若无人取胜,则进行第二局比赛;(2)第二局比赛,甲乙每人各按一次按键,按键所得二数之和与第一局比赛相同者获胜,若无人获胜,则进行第三局比赛,重复进行步骤(2),直到出现胜者为止。(3)游戏局数最多进行六局。在第六局比赛时,若重复进行步骤(2)仍未出现胜者,以按键所得两数之和最大者为获胜方。提高要求: 1、增加多人游戏的功能,数码管可分时记录显示每个游戏者的骰子点数。 2、点阵显示增加游戏开机动画、结束动画,并伴有乐曲播放。 3、自拟其它功能
3、。二、系统设计1、设计思路:按照实验要求,使用状态机分别表示游戏的不同状态;使用分频器来控制时钟;控制器实现具体的游戏规则;8*8LED点阵来显示胜负;数码管显示局数和甲乙掷出的随机数;随机数用一到六的循环产生。在编译时采用元件例化来生成各自的模块。流程图如下2、总体框图:输入3、分块设计:分别包括分频器、防抖器、随机数的产生、判断器、译码器和显示器等模块,综合起来实现所要求的功能。分频器:防抖器:随机数的产生:判断器:译码器:显示器:三、 仿真波形及波形分析根据甲先乙后的顺序进行仿真如下:从仿真中可以看出随机数的产生,若时间轴向后移可以将游戏看得更加清楚。四、 源程序分频器:libraryi
4、eee;use ieee.std_logic_1164.all;entityfenpingqi isport (clk:instd_logic; clktmp:outstd_logic);endfenpingqi;architecture a of fenpingqi is signaltmp: integer range 0 to 499:=0;signal clktmp1: std_logic;beginp3:process(clk) -frequent time begin ifclkevent and clk=1then iftmp=499 then tmp=0;clktmp1= no
5、t clktmp1; else tmp=tmp+1; end if; end if; end process p3; clktmp=clktmp1;end a ;防抖器:libraryieee;use ieee.std_logic_1164.all;entityfangdouqi isport (clk,btn1, btn2 :in std_logic; btn1_en,btn2_en :out std_logic);endfangdouqi;architecture a of fangdouqi is signal tempcount1:integer range 0 to 5 :=0;si
6、gnal tempcount2:integer range 0 to 5 :=0;beginp5:process(clk,btn1,btn2) begin ifclkevent and clk=1 then if btn1=1 then if tempcount1=5 then tempcount1=tempcount1; else tempcount1=tempcount1+1; end if; if tempcount1=4 then btn1_en=1; else btn1_en=0; end if; else tempcount1 =0; end if; if btn2=1 then
7、if tempcount2=5 then tempcount2=tempcount2; else tempcount2=tempcount2+1; end if; if tempcount2=4 then btn2_en=1; else btn2_en=0; end if; else tempcount2 =0; end if; end if; end process p5;end a;计数器libraryieee;use ieee.std_logic_1164.all;entityjishuqi isport (clktmp:instd_logic; randnum2:out integer
8、 range 1 to 12;randnum :out integer range 1 to 12);endjishuqi; architecture a of jishuqi is signal randnum1: integer range 1 to 12;signal randnum3: integer range 1 to 12;begin p4:process(clktmp) -counter1 begin ifclktmpevent and clktmp=1 then if randnum1= 6 then randnum1=1; else randnum1=randnum1+1;
9、 end if; if randnum3= 7 then randnum3=1; else randnum3=randnum3+1; end if; end if; if randnum3=7 then randnum2=1; else randnum2=randnum3; end if; randnum=randnum1;end process p4;end a;掷骰子结果的产生:libraryieee;use ieee.std_logic_1164.all;entitycreaters isport (clk:instd_logic; clktmp:instd_logic; btn1_en
10、,btn2_en :in std_logic; randnum,randnum1:in integer range 1 to 12; count:in integer range 0 to 5; sgn11,sgn22 :out std_logic; a1,a2,b1,b2:out integer range 1 to 12; a3,a4,b3,b4:out integer range 1 to 12);endcreaters;architecture a of creaters is signal a11,a22,b11,b22: integer range 1 to 12:=1;signa
11、l a33,a44,b33,b44: integer range 1 to 12;signal sgn1,sgn2:std_logic:=0; beginp1:process (a11,a22,a33,sgn1,sgn2,b11,b22,b33,btn1_en,btn2_en,clktmp) begin ifclktmpevent and clktmp=1then if sgn1=1and sgn2=1then sgn1=0;sgn2=0; end if; if btn1_en=1 and sgn1=0 then sgn1=1; a11=randnum; -get rand num; a22=
12、randnum1; a33=randnum1+randnum; if count=0 then a44=randnum1+randnum; end if; end if; if btn2_en=1 and sgn1=1and sgn2=0 then sgn2=1; b11=randnum; b22=randnum1; b33=randnum1+randnum; if count=0 then b44=randnum1+randnum; end if; end if; end if; a1=a11; a2=a22; a3=a33; b1=b11; b2=b22; b3=b33; a4=a44;
13、b4=b44; sgn11=sgn1; sgn22=sgn2;end process p1; end a ;判断器:libraryieee;use ieee.std_logic_1164.all;entitypanduanqi isport (sgn1,sgn2 :in std_logic;clk:instd_logic; a3,a4,b3,b4: in integer range 1 to 12; disp77: out integer range 1 to 6; count1:out integer range 0 to 5;winsgn :out std_logic_vector(1 d
14、ownto 0);endpanduanqi;architecture a of panduanqi is signal count : integer range 0 to 5:=0; signal disp7 : integer range 0 to 5:=1;begin p7:process(clk,a3,a4,b3,b4,sgn1,sgn2) -judgment begin ifclkevent and clk=1 then if sgn1=1and sgn2=1then 规定甲先掷乙后掷 if count=0 then if (a3=11 or a3=7) then winsgn=10
15、; disp7=disp7+1 ;count=0; elsif (b3=11 or b3=7) then winsgn=01; disp7=disp7+1 ;count=0; else count=count+1; end if; elsif count=5 then if (a3=11 or a3=7 ) then winsgn=10; elsif (b3=11 or b3=7) then winsgnb3 then winsgn=10; elsewinsgn=01; end if; disp7=disp7+1 ;count=0; else if a3=a4 then winsgn=10;d
16、isp7=disp7+1 ;count=0; elsif b3=b4 then winsgn=01;disp7=disp7+1 ;count=0; else count=count+1; end if; end if; end if; end if; count1=count; if (disp7=7)then disp77=1; else disp77=disp7; end if; end process p7;end a;译码器:libraryieee;use ieee.std_logic_1164.all;entityyimaqi isport (clktmp:instd_logic;
17、a1,a2,b1,b2:in integer range 1 to 12; disp7: in integer range 1 to 6;num : out std_logic_vector(6 downto 0);cat : out std_logic_vector(5 downto 0);endyimaqi;architecture a of yimaqi is signaltempcat: std_logic_vector(5 downto 0);signaltempnum: integer range 0 to 6:=1;signal num1: std_logic_vector(6
18、downto 0); signal cycle :integer range 0 to 4:=0; begin p6:process(clktmp) -translate the num1 to right num; begin ifclktmpevent and clktmp=1 then if(cycle=4)then cycle=0; else cycletempnum=a1; tempcattempnum=a2; tempcattempnum=b1; tempcattempnum=b2; tempcattempnum=disp7; tempcatnum1num1num1num1num1
19、num1num1=1011111; end case; num=num1; cat=tempcat; end process p6;end a;显示器:libraryieee;use ieee.std_logic_1164.all;entityxianshiqi isport (clk:instd_logic; winsgn: in std_logic_vector(1 downto 0); row: out std_logic_vector(7 downto 0); col: out std_logic_vector(7 downto 0) );endxianshiqi;architectu
20、re a of xianshiqi is signalcyc :integer range 0 to 7:=0;signal row1 :std_logic_vector(7 downto 0);signal col1 :std_logic_vector(7 downto 0);beginp8:process(clk) begin ifclkevent and clk=1 then ifcyc=7 then cyc=0; else cycrow1=01111111;col1row1=10111111;col1row1=11011111;col1row1=11101111;col1row1=11
21、110111;col1row1=11111011;col1row1=11111101;col1row1=11111110;col1row1=01111111;col1row1=10111111;col1row1=11011111;col1row1=11101111;col1row1=11110111;col1row1=11111011;col1row1=11111101;col1row1=11111110;col1=11111111; end case; else end if; end if; row=row1; col=col1; end process p8;end a;五、 功能说明及
22、资源利用情况分频器对固有频率分频提供给控制器、译码器、显示器合适的时钟;防抖器通过改变频率有效防止毛刺出现,使整个系统工作更加稳定;计数器及骰子结果的产生通过一到六的循环,同时在时钟作用下取数实现骰子数的随机出现;判断器则根据游戏规则采用状态机在时钟作用下来实现,设置变量规定甲乙投掷的顺序;译码器是使数码管在循环扫描作用下实现对掷出骰子数及局数的显示;8*8LED点阵编程控制行列高低电平同时循环扫描来实现对甲乙胜负的显示。所有模块在分频器作用下综合起来一起工作实现掷骰子的游戏。采用元件例化,尽可能提高资源利用率。六、 故障及问题分析初次编译程序,在调试过程中,系统工作不稳定,不能跟随按键及时显
23、示,在老师的建议下加入了防抖器,使得系统的工作更稳定;8*8LED点阵在显示时不稳定,有些地方有不应该亮的点亮起,经过反复试验,与同学交流,改变了行和列的编译方式,解决了问题;8*8LED点阵显示亮度不够,改变了频率;初次编译时,甲乙的投掷先后易引起混乱,引入了变量规定甲乙的先后顺序。七、 总结和结论在这次数电实验中我学到了很多。在初次拿到题目时比较混乱,不知从何处下手,通过与同学讨论,学习VHDL的相关内容,思路也渐渐清晰起来,画出了流程图及模块分布。在编写程序的过程中,采纳建议使用了元件例化,使整个系统更加清晰,编写更加有的放矢,纠错也更容易,通过一次次的调试改进,加防抖器及改变频率,终于基本完成了游戏的设计。在编写过程中最为困难的是8*8LED点阵的显示,之前对这个器件不太了解,查了好多资料弄清楚他的工作原理、编写方法,后来又在反复调试中改进,实现了其完美的显示。我认识到在做实验时,一定要积极主动地去学习,查资料,不要怕犯错,要坚持不懈,并且及时与老师同学沟通,取长补短,发现不足之处。要积极地去实践,才可以真正提高自己的逻辑思维和动手能力。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1