FPGA实验报告.docx
《FPGA实验报告.docx》由会员分享,可在线阅读,更多相关《FPGA实验报告.docx(28页珍藏版)》请在冰豆网上搜索。
FPGA实验报告
FPGA实验
一、实验目的
1.熟悉QuartusⅡ软件基本使用方法;
2.掌握电路的基本设计方法;
3.掌握电路的基本仿真方法;
4.掌握电路的基本下载方法;
5.学习状态机的设计技巧;
6.掌握CASE语句的使用;
7.掌握时钟分频电路的HDL设计;
8.掌握CASE语句的使用;
9.掌握VHDL模块化程序设计方式;
10.学习VHDL程序的设计技巧;
11.掌握VHDL语言译码电路的设计;
12.掌握选择信号赋值语句的设计方法;
二、实验器材
1.KH-310下载板;
2.KH-310LED显示模块;
3.KH-310时钟模块;
5.KH-310七段数码管模块。
4.计算机。
三、实验原理
乒乓球机:
乒乓球游戏机是用8~16个发光二极管代表乒乓球台,中间两个发光二极管兼作乒乓球网,用点亮的发光二极管按一定方向移动来表示球的运动。
另外设置发球开关:
Af、Bf;和接球开关Aj、Bj。
利用若干七段数码管作为计分牌。
甲乙双方按乒乓球比赛规则来操作开关。
当甲方按动发球开关Af时,靠近甲方的第一个灯亮,然后顺序向乙方移动。
当球过网后,乙方可以接球,接球后灯反方向运动,双方继续比赛,如果一方提前击球(过网击球)或未击到球,则判失分,对方加分。
重新发球后继续比赛。
译码器:
状态机:
状态机的一个常用的用处是构成控制步进电机的逻辑电路。
表1给出了用于顺时针和逆时针驱动步进电机的二进制码。
状态机的设计中我们需要提到一些称为常态和次态的状态,表2列举了步进电机的状态转换顺序。
时钟:
图3.27
四、实验内容
1.用VHDL语言和原理图分别完成设计、仿真和下载基本逻辑门电路半加器的过程:
输入:
a,b;输出:
求和sum,进位c;
2.控制8个LED进行花式显示,设计四种显示模式:
1.从左到右逐个点亮LED;
2.从右到左逐个点亮LED;
3.从两边到中间逐个点亮LED;
4.从中间到两边逐个点亮LED。
四种模式循环切换,由复位键rst控制系统的运行与停止。
3.设计一个乒乓球游戏机,模拟乒乓球比赛基本过程和规则,并能自动裁判和计分。
4.设计一个简单的指令译码器,按指定的要求,对两个逻辑变量进行运算,并将运算的结果在发光二极管上显示。
5.使用VHDL语言,编制一个4相步进电机控制程序,由发光二极管指示步进电机的相序,要求正反转控制,通过改变时钟频率实现调速。
6.设计一个电子钟,要求可以显示时、分、秒,用户可以设置时间且需要设置的时间显示要求闪烁。
五、实验连线
跑马灯:
输入信号:
时钟clk接10Hz输入(SW7)P152;
复位rst接拨动开关I01(拨码开关SW3左1)P1;
输出信号:
8位输出信号接LED模块O25—O32。
P43~P50;
乒乓球:
输入信号:
控制时钟CLK:
可调时钟SW7输入;
接发球输入Af,Aj,Bf,Bj:
脉冲发生器EPI0~EPI3(Pin236Pin238Pin237Pin239);
系统复位Clr:
拨码开关I01;
输出信号:
乒乓球信号shift[15..0]:
LED显示模块O25~O40;
计分输出q[6..0]:
七段数码管O56~O50(Pin79~Pin73)(JP1);
Sellread[3..0]:
数码管选择SO61~SO58(Pin86~Pin83)(数码管跳线接左侧扫描方式)。
译码器:
输入信号:
译码器输入开关c、b、a接拨动开关I01(拨码开关SW3左1P1)、I02(拨码开关SW3左2P2)、I03(拨码开关SW3左3P3),输入数据data1、data2接IO9(拨码开关SW4左1P11)、IO10(拨码开关SW3左2P12)
输出信号:
LED模块O25P43;
步进机:
输入信号:
时钟clk接10Hz输入(SW7)P152;方向dir接拨动开关I01(拨码开关SW3左1)P1;
输出信号:
4位输出信号接LED模块O25—O28。
P43~P46;
电子钟:
输入信号:
时钟clk:
接开发板可调时钟SW7(P152),选择100KHz频率;
模式选择mode:
接脉冲发生器模块EPI0;P236
时间设定set:
接脉冲发生模块EPI1;P238
输出信号:
Seg7[7..0]接数码管段扫描信号O50~O56、SO57;P73、74、75、76、77、78、79、82
Segctr[7..0]接数码管位选显示信号SO58~SO65;P83、84、85、86、87、88、93、94
CLR;P1清零信号
六、实验步骤
1.新建文本文件
2.建立工程项目
3.编译并综合
4.设计仿真
5.管脚的分配与定位
6.硬件连接
7.器件下载编程与硬件实现
七、实验操作
1.跑马灯:
下载程序,将SW7拨至第二段(10Hz),拨码开关SW3的IO1拨至ON的位置,此时,LED灯的前8位会有规律的闪烁。
2.乒乓球:
下载程序,将SW3的IO1拨到ON后可进行比赛,PULS1为甲方发球,PULS2为甲方接球,PULS3为乙方发球,PULS4为乙方接球。
LED灯中,始终点亮的代表球网,将SW7拨至第四段的位置。
点击发球之后,双方可进行比赛,比赛规则参照实验原理部分。
3.译码器:
改变译码器的输入值(a、b、c)和输入数据值(data1、data2),输出数据按设计值输出
4.步进机:
下载程序,将SW7拨至第二段(10Hz),此时,LED灯的前1位会有规律的闪烁。
改变方向开关的电平信号,指示灯的移动方向立即改变。
5.电子钟:
下载程序,按PULSE1可进行时、分、秒的切换,按PULSE2可以对时,分,秒进行设置,I01为系统的使能,I01为off时开始计时。
八、实验程序
1.半加器
libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
entityhalfaddis
port(a:
instd_logic;
b:
instd_logic;
sum:
outstd_logic;
c,d:
outstd_logic);
end;
architecturemainofhalfaddis
begin
c<=aandb;
sum<=axorb;
end;
2.跑马灯
libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
entityledis
port(clk:
instd_logic;
rst:
instd_logic;
q:
outstd_logic_vector(7downto0));
end;
architectureledofledis
constants0:
std_logic_vector(1downto0):
="00";--模式1
constants1:
std_logic_vector(1downto0):
="01";--―模式2
constants2:
std_logic_vector(1downto0):
="10";--―模式3
constants3:
std_logic_vector(1downto0):
="11";--―模式4
signalpresent:
std_logic_vector(1downto0);--――当前模式
signalq1:
std_logic_vector(7downto0);
signalcount:
std_logic_vector(3downto0);
begin
process(rst,clk)
begin
if(rst='0')then--――系统初始化
present<=s0;
q1<=(others=>'0');
elsif(clk'eventandclk='1')then
casepresentis
whens0=>if(q1="00000000")then--――S0模式:
从左到右逐个点亮LED
q1<="10000000";
elseif(count="0111")then
count<=(others=>'0');
q1<="00000001";
present<=s1;
elseq1<=q1(0)&q1(7downto1);
count<=count+1;
present<=s0;
endif;
endif;
whens1=>if(count="0111")then--S1模式:
从右到左逐个点亮LED
count<=(others=>'0');
q1<="10000001";
present<=s2;
elseq1<=q1(6downto0)&q1(7);
count<=count+1;
present<=s1;
endif;
whens2=>if(count="0011")then--S2模式:
从两边到中间逐个点亮LED
count<=(others=>'0');
q1<="00011000";
present<=s3;
elseq1(7downto4)<=q1(4)&q1(7downto5);
q1(3downto0)<=q1(2downto0)&q1(3);
count<=count+1;
present<=s2;
endif;
whens3=>if(count="0011")then--S3模式:
从中间到两边逐个点亮LED
count<=(others=>'0');
q1<="10000000";
present<=s0;
elseq1(7downto4)<=q1(6downto4)&q1(7);
q1(3downto0)<=q1(0)&q1(3downto1);
count<=count+1;
present<=s3;
endif;
endcase;
endif;
endprocess;
q<=q1;
end;
3.乒乓球
Libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
entitycornais
port(
clr,af,aj,bf,bj,clk:
instd_logic;
shift:
outstd_logic_vector(15downto0);
ah,al,bh,bl:
outstd_logic_vector(3downto0)
);
endcorna;
architecturecorna_arcofcornais
signalamark,bmark:
integer;
signalclk1:
std_logic;
signalfen:
std_logic_vector(9downto0);
begin
process
begin
waituntilclk='1';
fen<=fen+1;
clk1<=fen(7);
endprocess;
process(clr,clk1)
variablea,b:
std_logic;
variableshe:
std_logic_vector(15downto0);
begin
ifclr='0'then
a:
='0';
b:
='0';
she:
="0000000000000000";
amark<=0;
bmark<=0;
elsifclk1'eventandclk1='1'then
ifa='0'andb='0'andaf='1'then
a:
='1';
she:
="1000000000000000";
elsifa='0'andb='0'andbf='1'then
b:
='1';
she:
="0000000000000001";
elsifa='1'andb='0'then
ifshe>128then
ifbj='1'then
amark<=amark+1;
a:
='0';
b:
='0';
she:
="0000000000000000";
else
she:
='0'&she(15downto1);
endif;
elsifshe=0then
amark<=amark+1;
a:
='0';
b:
='0';
else
ifbj='1'then
a:
='0';
b:
='1';
else
she:
='0'&she(15downto1);
endif;
endif;
elsifa='0'andb='1'then
ifshe<256andshe/=0then
ifaj='1'then
bmark<=bmark+1;
a:
='0';
b:
='0';
she:
="0000000000000000";
else
she:
=she(14downto0)&'0';
endif;
elsifshe=0then
bmark<=bmark+1;
a:
='0';
b:
='0';
else
ifaj='1'then
a:
='1';
b:
='0';
else
she:
=she(14downto0)&'0';
endif;
endif;
endif;
endif;
shift<=she;
endprocess;
process(clk,clr,amark,bmark)
variableaha,ala,bha,bla:
std_logic_vector(3downto0);
variabletmp1,tmp2:
integer;
begin
ifclr='0'then
aha:
="0000";
ala:
="0000";
bha:
="0000";
bla:
="0000";
tmp1:
=0;
tmp2:
=0;
elsifclk'eventandclk='1'then
ifamark>tmp1then
ifala="1001"then
ala:
="0000";
aha:
=aha+1;
tmp1:
=tmp1+1;
else
ala:
=ala+1;
tmp1:
=tmp1+1;
endif;
endif;
ifbmark>tmp2then
ifbla="1001"then
bla:
="0000";
bha:
=bha+1;
tmp2:
=tmp2+1;
else
bla:
=bla+1;
tmp2:
=tmp2+1;
endif;
endif;
endif;
al<=ala;
bl<=bla;
ah<=aha;
bh<=bha;
endprocess;
endcorna_arc;
Libraryieee;
useieee.std_logic_1164.all;
entitych41ais
port(
sel:
instd_logic_vector(2downto0);
d0,d1,d2,d3:
instd_logic_vector(3downto0);
q:
outstd_logic_vector(3downto0)
);
endch41a;
architecturech41_arcofch41ais
begin
process(sel)
begin
caseselis
when"100"=>q<=d2;
when"101"=>q<=d3;
when"000"=>q<=d0;
whenothers=>q<=d1;
endcase;
endprocess;
endch41_arc;
Libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
entityselis
port(
clk:
instd_logic;
sell:
outstd_logic_vector(2downto0)
);
endsel;
architecturesel_arcofselis
begin
process(clk)
variabletmp:
std_logic_vector(2downto0);
begin
ifclk'eventandclk='1'then
iftmp="000"then
tmp:
="001";
elsiftmp="001"then
tmp:
="100";
elsiftmp="100"then
tmp:
="101";
elsiftmp="101"then
tmp:
="000";
endif;
endif;
sell<=tmp;
endprocess;
endsel_arc;
Libraryieee;
useieee.std_logic_1164.all;
entitydispis
port(
d:
instd_logic_vector(3downto0);
q:
outstd_logic_vector(6downto0)
);
enddisp;
architecturedisp_arcofdispis
begin
process(d)
begin
casedis
when"0000"=>q<="0111111";
when"0001"=>q<="0000110";
when"0010"=>q<="1011011";
when"0011"=>q<="1001111";
when"0100"=>q<="1100110";
when"0101"=>q<="1101101";
when"0110"=>q<="1111101";
when"0111"=>q<="0100111";
when"1000"=>q<="1111111";
whenothers=>q<="1101111";
endcase;
endprocess;
enddisp_arc;
Libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
entitypingpongis
port(
clr,af,aj,bf,bj,clk:
instd_logic;
shift:
outstd_logic_vector(15downto0);
q:
outstd_logic_vector(6downto0);
net:
outstd_logic_vector(1downto0);
sellread:
outstd_logic_vector(3downto0)
);
endpingpong;
architecturepingpongofpingpongis
componentcornais
port(
clr,af,aj,bf,bj,clk:
instd_logic;
shift:
outstd_logic_vector(15downto0);
ah,al,bh,bl:
outstd_logic_vector(3downto0)
);
endcomponent;
componentch41ais
port(
sel:
instd_logic_vector(2downto0);
d0,d1,d2,d3:
instd_logic_vector(3downto0);
q:
outstd_logic_vector(3downto0)
);
endcomponent;
componentselis
port(
clk:
instd_logic;
sell:
outstd_logic_vector(2downto0)
);
endcomponent;
componentdispis
port(
d:
instd_logic_vector(3downto0);
q:
outstd_logic_vector(6downto0)
);
endcomponent;
signali1,i2,i3,i4,i5:
std_logic_vector(3downto0);
signali6:
std_logic_vector(2downto0);
begin
u1:
cornaportmap(clr,af,aj,bf,bj,clk,shift,i1,i2,i3,i4);
u2:
ch41aportmap(i6,i2,i1,i4,i3,i5);
u3:
selportmap(clk,i6);
u4:
dispportmap(i5,q);
net<="11";
--withi6select
--sellread<="1000"when"000"
--"0100"when"001"
--"0010"when"100"
--"0001"whenothers;
sellread<="1000"wheni6="000"else
"0100"wheni6="001"else
"0010"wheni6="100"else
"0001";
--sellread<=i6;
endpingpong;
4.指令译码器
libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
entitydecoderis
port(a,b,c:
instd_logic;
data1,data2:
instd_logic;
dataout:
outstd_logic);
enddecoder;