EDA 课程设计 ——拔河游戏机?
一、总体设计思想
电子拔河游戏机是一种能容纳甲乙双方参赛游戏电路。由一排发光二极管表示拔河的“电子绳”。由甲乙双方通过按纽开关使发光二极管向一方的终点延伸,当延伸到某方的最后一个发光二极管时, 则该方获胜,连续比赛多局以定胜负。
1.基本原理
本电路要求使用9个发光二极管,开机后只有中间一个发亮,此即拔河的中心点。游戏双方各持一个按钮,迅速地、不断地按动,产生脉冲,谁按得快,亮点就向谁的方向移动,每按一次,亮点移动一次。亮点移到任一方终端二极管时,这一方就获胜,此时双方按钮均无作用,输出保持,只有复位后才使亮点恢复到中心。最后用数码管显示获胜者的盘数。
由设计内容可知,首先需要一个十进制的计数器,用于对双方按钮的次数计数,并通过译码器显示在数码管上。设计要求用50MHz的频率,而设计用到的是1K Hz的频率,所以要设计一个程序进行分频。其次,显视控制部分设计要求在发光二极管上显示游戏状态,双方每按十次,亮点向先按十次移动一次,对脉冲进行计数,每十次移一位。需接入一个清零端 ,用于复位。再次,运用VHDL程序语言进行各个模块的程序编写,控制电路的正常运行。最后,将以上程序组装起来,就可得到所需要的拔河游戏机
library ieee
use ieee.std_logic_1164.all
use ieee.std_logic_unsigned.all
entity bahe is
port (a,b,rst,clk:in std_logic
sg,led:out std_logic_vector(8 downto 0)
bt:out std_logic_vector(7 downto 0))
end bahe
----------------------------------
architecture one of bahe is
component cnt10
port (clk,rst,en:std_logic
cout:out std_logic
cq:out std_logic_vector(3 downto 0))
end component
component scan
port (clk :in std_logic
a1, a2,a3,b1,b2,b3:in std_logic_vector(3 downto 0)
sg:out std_logic_vector(8 downto 0)
bt: out std_logic_vector(7 downto 0))
end component
component lmov
port (kl ,kr:in std_logic_vector(3 downto 0)
led:out std_logic_vector(8 downto 0)
en : out std_logic
rst:in std_logic)
end component
signal e,f,ca1,ca2,cb1,cb2:std_logic
signal cqa1,cqa2,cqa3,cqb1,cqb2,cqb3:std_logic_vector(3 downto 0)
begin
u1: cnt10 port map (en=>e,rst=>rst,clk=>a,cout=>ca1,cq=>cqa1)
u2: cnt10 port map (en=>e,rst=>rst,clk=>ca1,cout=>ca2,cq=>cqa2)
u3: cnt10 port map (en=>e,rst=>rst,clk=>ca2,cq=>cqa3)
u4: cnt10 port map (en=>e,rst=>rst,clk=>b,cout=>cb1,cq=>cqb1)
u5: cnt10 port map (en=>e,rst=>rst,clk=>cb1,cout=>cb2,cq=>cqb2)
u6: cnt10 port map (en=>e,rst=>rst,clk=>cb2,cq=>cqb3)
u7: scan port map (a1=>cqa1,a2=>cqa2,a3=>cqa3,b1=>cqb1,
b2=>cqb2,b3=>cqb3,clk=>clk,sg=>sg,bt=>bt)
u8:lmov port map (en=>e,kl=>cqa2,kr=>cqb2,rst=>rst,led=>led)
end architecture one
library ieee
use ieee.std_logic_1164.all
use ieee.std_logic_unsigned.all
entity cnt10 is
port(clk,rst,en:std_logic
cout:out std_logic
cq:out std_logic_vector(3 downto 0))
end
architecture one of cnt10 is
begin
process(clk,rst,en)
variable cqi:std_logic_vector(3 downto 0)
begin
if rst='1' then
cqi:=(others=>'0')
elsif clk'event and clk='1' then
if en='1' then
if cqi<9 then cqi:=cqi+1
else cqi :=(others=>'0')
end if
end if
end if
if c qi=9 then cout<='0'
else cout<='1'
end if
cq<=cqi
end process
end
电路的VHDL程序如下:
library ieee
use ieee.std_logic_1164.all
use ieee.std_logic_unsigned.all
entity scan is
port (clk :in std_logic
a1,a2,a3,b1,b2,b3:in std_logic_vector(3 downto 0)
sg:out std_logic_vector(8 downto 0)
bt: out std_logic_vector(7 downto 0))
end
architecture one of scan is
signal cnt4:std_logic_vector(2 downto 0)
signal a:std_logic_vector(3 downto 0)
signal clk1:std_logic
begin
p1:process(cnt4)
begin
case cnt4 is
when "000"=>bt<="10000000"a<=a1
when "001"=>bt<="01000000"a<=a2
when "010"=>bt<="00100000"a<=a3
when "011"=>bt<="00000100"a<=b1
when "100"=>bt<="00000010"a<=b2
when "101"=>bt<="00000001"a<=b3
when others=>bt<="00000000"
end case
end process p1
---------------------------------
p2:process (clk)
variable ct:integer range 0 to 50000
begin
if clk'event and clk='1' then --1000HZ
if ct<49999 then
ct:=ct+1
clk1<='0'
else
ct:=0
clk1<='1'
end if
end if
end process p2
process(clk1)
begin
if clk1'event an d clk1='1' then
if cnt4<5 then
cnt4<=cnt4+1
else
cnt4<="000"
end if
end if
end process
------------------------------------
process (a)
begin
case a is
when "0000"=>sg<="100000000"
when "0001"=>sg<="111110001"
when "0010"=>sg<="001001000"
when "0011"=>sg<="001100000"
when "0100"=>sg<="000110010"
when "0101"=>sg<="000100100"
when "0110"=>sg<="000000100"
when "0111"=>sg<="111110000"
when "1000"=>sg<="000000000"
when "1001"=>sg<="100011111"
when "1010"=>sg<="000100100"
when "1011"=>sg<="000011000"
when "1100"=>sg<="010001100"
when "1101"=>sg<="001001000"
when "1110"=>sg<="001000000"
when "1111"=>sg<="000011111"
when others=>null
end case
end process
end
⑸ 胜负显示
将双方终端二极管正极经非门后的输出分别接到二个CC4518计数器的EN端,CC4518的两组4位BCD码分别接到实验装置的两组译码显示器的A、B、C、D插口处。当一方取胜时,该方终端二极管发亮,产生一个上升沿,使相应的计数器进行加一计数,于是就得到了双方取胜次数的显示,若一位数不够,则进行二位数的级联。
⑹ 复位
其VHDL程序如下:
library ieee
use ieee.std_logic_1164.all
use ieee.std_logic_unsigned.all
entity lmov is
port (kl ,kr:in std_logic_vector(3 downto 0)
led:out std_logic_vector(8 downto 0)
en : out std_logic
rst:in std_logic)
end
architecture one of lmov is
begin
process(rst,kl,kr)
begin
if rst='1' then led<="111101111"en<='1'
elsif kl-kr=1 then led<="111011111"en<='1'
elsif kl-kr=2 then led<="110111111"en<='1'
elsif kl-kr=3 then led<="101111111"en<='1'
elsif kl-kr=4 then led<="011111111"en<='0'
elsif kr-kl=1 then led<="111110111"en<='1'
elsif kr-kl=2 then led<="111111011"en<='1'
elsif kr-kl=3 then led<="111111101"en<='1'
elsif kl-kr=4 then led<="111111110"en<='0'
elsif kr-kl=0 then led<="111101111"en<='1'
else null
end if
end process
end
可以原理图输入,也可以文本输入目前的有verilog hdl 、vhdl 、system verilog、system C等硬件描述语言,当然主流的是原理图、verilog hdl、vhdl 三者混合输入,可以用Quartus II实现altera器件的设计输入
1,用开关按键表示脉冲,每个脉冲代表100米,10个脉冲1公里,每公里1.4元,能同步显示里程和费用;
2,低于2公里5元计费,高于2公里总费用=起步费用+(里程-2公里)*里程单价+
等候时间*等后单价;
3,等候时间大于2分钟,按每分钟1.3元计费;
4,可以设定起步价和里程单价。
一、设计原理与技术方法:
包括:电路工作原理分析与原理图、元器件选择与参数计算、电路调试方法与结果说明;
软件设计说明书与流程图、软件源程序代码、软件调试方法与运行结果说明。
根据设计要求,系统的输入信号clk,计价开始信号start,等待信号stop,里程脉冲信号fin。系统的输出信号有:总费用数C0—c3,行驶距离k0—k1,等待时间m0—m1等。系统有两个脉冲输入信号clk_750k,fin,其中clk_750k将根据设计要求分频成14hz,15hz和1hz分别作为公里计费和超时计费的脉冲。两个控制输入开关start,stop;控制过程为:start作为计费开始的开关,当start为高电平时,系统开始根据输入的情况计费。当有乘客上车并开始行驶时,fin脉冲到来,进行行驶计费,此时的stop需要置为0;如需停车等待,就把stop变为高电平,
并去除fin输入脉冲,进行等待计费;当乘客下车且不等待时,直接将start置为0,系统停止工作;价格开始归为起步价5.0元。
整个设计由分频模块,计量模块,计费模块,控制模块和显示模块五个部分组成。
其中计量模块是整个系统实现里程计数和时间计数的重要部分;控制模块是实现不同计费方式的选择部分,根据所设计的使能端选择是根据里程计费还是根据等待时间计费,同时设计通过分频模块产生不同频率的脉冲信号来实现系统的计费。计量模块采用1hz的驱动信号,计费模块采用14hz,13hz的驱动信号;计量模块每计数一次,计量模块就实现14次或者13次计数,即为实现计时的1.3元/min,计程时的1.4元/km的收费。组成框图如下所示:
1.百进制模块:
实现百米脉冲的驱动信号,元件框图如图3所示:
图3 百进制模块框图
源程序如下:
library ieee
use ieee.std_logic_1164.all
use ieee.std_logic_unsigned.all
entity baijinzhi is
port(start,clk2: in std_logic --秒脉冲
a: out std_logic_vector(3 downto 0))
end baijinzhi
architecture rt1 of baijinzhi is
signal count_1:std_logic_vector(3 downto 0)
begin
a<=count_1
process(start,clk2)
begin
if(start='0')then
count_1<="0000"
elsif(clk2'event and clk2='1')then
if(count_1="0111")then
count_1<="0000"
else
count_1<=count_1+'1'
end if
end if
end process
end rt1
2.计费模块
实现里程和等候时间的计费并输出到显示,元件框图4如下:
图4 计费模块框图
源程序如下:
Library IEEE
use IEEE.std_logic_1164.all
use IEEE.std_logic_arith.all
use IEEE.std_logic_unsigned.all
entity jifei is
port(clk2:in std_logic --计费驱动信号
start:in std_logic --计费开始信号
c0,c1,c2,c3:buffer std_logic_vector(3 downto 0))
end jifei
architecture rt1 of jifei is
begin
process(clk2,start)
begin
if start='0'then c3<="0000"c2<="0000"c1<="0101"c0<="0000"--起步价5元
elsif clk2'event and clk2='1'then
if c0="1001" then c0<="0000"
if c1="1001" then c1<="0000"
if c2="1001" then c2<="0000"
if c3="1001" then c3<="0000"
else c3<=c3+1
end if
else c2<=c2+1
end if
else c1<=c1+1
end if
else c0<=c0+1
end if
end if
end process
end rt1
3.公里模块
实现历程的计数和输出计费脉冲,元件框图5如下:
图5 公里模块框图
源程序如下:
library ieee
use ieee.std_logic_1164.all
use ieee.std_logic_unsigned.all
entity gongli is
port(clk1,start: in std_logic --百米脉冲
k1,k2,k3,k4: out std_logic_vector(3 downto 0) --里程显示
temp2 : out std_logic)
end gongli
architecture rt1 of gongli is
signal count_1: std_logic_vector(3 downto 0)
signal count_2: std_logic_vector(3 downto 0)
signal count_3: std_logic_vector(3 downto 0)
signal count_4: std_logic_vector(3 downto 0)
begin
k1<=count_1
k2<=count_2
k3<=count_3
k4<=count_4
process(start,clk1)
begin
if(start='0')then
count_1<="0000"
count_2<="0000"
count_3<="0000"
count_4<="0000" ---公里清零
elsif(clk1'event and clk1='1')then
if(count_1="1001")then --公里计数器
count_1<="0000"count_2<=count_2+1temp2<='1'
if(count_2="1001")then
count_2<="0000"count_3<=count_3+'1'
if(count_3="1001")then
count_3<="0000"count_4<=count_4+'1'
end if
end if
else
count_1<=count_1+'1'temp2<='0'
end if
end if
end process
end rt1
4.输出模块
实现所有数据的输出,元件框图6如下:
图6 输出模块框图
源程序如下:
library ieee
use ieee.std_logic_1164.all
use ieee.std_logic_unsigned.all
entity shuchu is
port(y: in std_logic_vector(3 downto 0)
e: out std_logic_vector(6 downto 0))
end shuchu
architecture rt1of shuchu is
begin
process
begin
case y is
when"0000"=>e<="0111111"
when"0001"=>e<="0000110"
when"0010"=>e<="1011011"
when"0011"=>e<="1001111"
when"0100"=>e<="1100110"
when"0101"=>e<="1101101"
when"0110"=>e<="1111101"
when"0111"=>e<="0000111"
when"1000"=>e<="1111111"
when"1001"=>e<="1100111"
when others=>e<="0000000"
end case
end process
end rt1
5.显示模块
实现所有数据的显示,元件框图7如下:
图7 显示模块框图
源程序如下:
library ieee
use ieee.std_logic_1164.all
use ieee.std_logic_unsigned.all
entity xianshi is
port(start: in std_logic
a:in std_logic_vector(3 downto 0) --选择信号
c1,c2,c3,c4,out1,out2,out3,out4:in std_logic_vector(3 downto 0) --里程显示,时间显示输入
y:out std_logic_vector(3 downto 0)) --里程显示,时间显示输出
end xianshi
architecture rt1 of xianshi is
begin
process
begin
if(start='0')then
y<="0000"
else case a is
when "0000"=>y<=c1
when "0001"=>y<=c2
when "0010"=>y<=c3
when "0011"=>y<=c4
when "0100"=>y<=out1
when "0101"=>y<=out2
when "0110"=>y<=out3
when "0111"=>y<=out4
when others =>y<= "0000"
end case
end if
end process
end rt1
6.dian模块
图8 dian模块框图
源程序如下:
library ieee
use ieee.std_logic_1164.all
use ieee.std_logic_unsigned.all
entity dian is
port(a: in std_logic_vector(3 downto 0)
e: out std_logic)
end dian
architecture rt1 of dian is
begin
process
begin
case a is
when "0001"=>e<='1'
when "0101"=>e<='1'
when others=>e<='0'
end case
end process
end rt1
三、中各个模块设计分析
系统总体顶层框图如下:
系统总体顶层框图
程序最终功能实现波形仿真
1. 分频模块
由于实验箱上没有14hz和13hz的整数倍时钟信号,因此采用频率较大的750khz进行分频,以近似得到14hz,13hz和1hz的时钟频率。通过以上三种不同频率的脉冲信号实行出租车行驶,等待两种情况下的不同计费。模块元件如下:
分频模块框图
源程序如下:
Library IEEE
use IEEE.std_logic_1164.all
use IEEE.std_logic_arith.all
use IEEE.std_logic_unsigned.all
entity fenpin is
port(clk_750k:in std_logic--系统时钟
clk_14:buffer std_logic --14分频
clk_13:buffer std_logic --13分频
clk_1 : buffer std_logic) --1分频
end fenpin
architecture rt1 of fenpin is
signal q_14:integer range 0 to 53570--定义中间信号量
signal q_13:integer range 0 to 57691
signal q_1:integer range 0 to 749999
begin
process(clk_750k)
begin
If(clk_750k' event and clk_750k='1')then
If q_14=53570 then q_14<=0clk_14<=not clk_14
else q_14<=q_14+1
end if --得14hz频率信号
If q_13=57691 then q_13<=0clk_13<=not clk_13
else q_13<=q_13+1
end if --得13hz频率信号
If q_1=749999 then q_1<=0clk_1<=not clk_1
else q_1<=q_1+1
end if --得1hz频率信号
end if
end process
end rt1;
2. 计量模块
计量模块主要完成计时和计程功能。
计时部分:计算乘客的等待累积时间,当等待时间大于2min时,本模块中en1使能信号变为1;当clk1每来一个上升沿,计时器就自增1,计时器的量程为59min,满量程后自动归零。
计程部分:计算乘客所行驶的公里数,当行驶里程大于2km时,本模块中en0使能信号变为1;当clk每来一个上升沿,计程器就自增1,计程器的量程为99km,满量程后自动归零。
元件框图为:
计量模块框图
计量模块仿真波形为:
源程序如下:
library ieee
use ieee.std_logic_1164.all
use ieee.std_logic_arith.all
use ieee.std_logic_unsigned.all
entity jiliang is
port(start:in std_logic --计费开始信号
fin:in std_logic --里程脉冲信号
stop:in std_logic--行驶中途等待信号
clk1:in std_logic--驱动脉冲
en1,en0:buffer std_logic --计费单价使能信号
k1,k0:buffer std_logic_vector(3 downto 0) --行驶公里计数
m1,m0:buffer std_logic_vector(3 downto 0))--等待时间计数
end jiliang
architecture rt2 of jiliang is
signal w:integer range 0 to 59 --计时范围0~59
begin
process(clk1)
begin
if(clk1'event and clk1='1')then
if start='0' then
w<=0en1<='0'en0<='0'm1<="0000"
m0<="0000"k1<="0000"k0<="0000"
elsif stop='1' then --计时开始信号
if w=59 then
w<=0
else w<=w+1
end if
if m0="1001" then
m0<="0000"
if m1="0101" then
m1<="0000"
else m1<=m1+1
end if
else m0<=m0+1
end if
if stop='1' then en0<='0'
if m1&m0>"00000001" then en1<='1' --若等待时间大于2min则en1置1
else en1<='0'
end if
end if
elsif fin='1' then --里程计数开始
if k0="1001" then k0<="0000"
if k1="1001" then k1<="0000"--计程范围0~99
else k1<=k1+1
end if
else k0<=k0+1
end if
if stop='0' then
en1<='0'
if k1&k0>"00000001" then
en0<='1' --若行使里程大于2km,则en0置1
else en0<='0'
end if
end if
end if
end if
end process
end rt2
3. 控制模块
本模块主要是通过计量模块产生的两个不同的输入使能信号en0,en1,对每个分频模块输出的14hz,13hz的脉冲进行选择输出的过程;本模块实现了双脉冲的二选一;最终目的为了计费模块中对行驶过程中不同的时段进行计价。
模块元件如下:
控制模块框图
控制模块仿真波形为:
源程序如下:
Library IEEE
use IEEE.std_logic_1164.all
use IEEE.std_logic_arith.all
use IEEE.std_logic_unsigned.all
entity kongzhi is
port(en0,en1:in std_logic --使能选择信号
clk_in1:in std_logic --14分频输入信号
clk_in2:in std_logic --13分频输入信号
clk_out:out std_logic) --输出信号
end kongzhi
architecture rt3 of kongzhi is
begin
process(en0,en1)
begin
if en0='1' then --实现二选一功能
clk_out<=clk_in1
elsif en1='1' then
clk_out<=clk_in2
end if
end process
end rt3
4.计费模块
当计费信号start一直处于高电平即计费状态时,本模块根据控制模块选择出的信号从而对不同的单价时段进行计费。即行程在2km内,而且等待累计时间小于2min则为起步价5元;2km外以每公里1.4.元计费,等待累积时间超过2min则按每分钟1.3元计费。c0,c1,c2,c3分别表示费用的显示。
模块元件为:
计费模块框图
计费模块仿真波形为:
源程序如下:
Library IEEE
use IEEE.std_logic_1164.all
use IEEE.std_logic_arith.all
use IEEE.std_logic_unsigned.all
entity jifei is
port(clk2:in std_logic --计费驱动信号
start:in std_logic --计费开始信号
c0,c1,c2,c3:buffer std_logic_vector(3 downto 0))
end jifei
architecture rt4 of jifei is
begin
process(clk2,start)
begin
if start='0'then c3<="0000"c2<="0000"c1<="0101"c0<="0000"--起步价5元
elsif clk2'event and clk2='1'then
if c0="1001" then c0<="0000"
if c1="1001" then c1<="0000"
if c2="1001" then c2<="0000"
if c3="1001" then c3<="0000"--计价范围0~999.9
else c3<=c3+1
end if
else c2<=c2+1
end if
else c1<=c1+1
end if
else c0<=c0+1
end if
end if
end process
end rt4
5.显示模块
显示模块完成计价,计时和计程数据显示。计费数据送入显示模块进行译码,最后送至以百元,十元,元,角为单位对应的数码管上显示。计时数据送入显示模块进行译码,最后送至以分为单位对应的数码管上显示。计程数据送入显示模块进行译码,最后送至以km为单位的数码管上显示。
模块元件为:
显示模块框图
源程序如下:
library ieee
use ieee.std_logic_1164.all
use ieee.std_logic_unsigned.all--定义库包
entity xianshi is --定义实体
port(
clk_scan:in std_logic--扫描时钟信号端口设置
c3,c2,c1,c0:in std_logic_vector(3 downto 0)--总费用输入端口
k0,k1:in std_logic_vector(3 downto 0)--里程输入端口
m0,m1:in std_logic_vector(3 downto 0)--等待时间输入端口
sel:out std_logic_vector(2 downto 0)--控制数码管位选信号的扫描信号输出端口
led:out std_logic_vector(6 downto 0)--数码管的控制端口
led_dp:out std_logic --数码管的小数点输出端口
)
end xianshi
architecture rt5 of xianshi is
signal duan:std_logic_vector(6 downto 0)--数码显示管中间变量
signal shuju:std_logic_vector(3 downto 0)--选择输入端的中间变量
signal cnt:std_logic_vector(2 downto 0)--控制数码管的中间变量
signal xiaodian:std_logic--小数点的中间变量
begin
process(clk_scan) --开始进程
begin
if clk_scan'event and clk_scan='1' then
cnt<=cnt+1--每有一个扫描信号上升沿实现加1扫描
end if
end process--结束进程
process(cnt) --开始进程(选择扫描显示数码管)
begin
case cnt is --扫描时给每个数码管赋值
when "000"=>shuju<=c0
when "001"=>shuju<=c1
when "010"=>shuju<=c2
when "011"=>shuju<=c3
when "100"=>shuju<=k0
when "101"=>shuju<=k1
when "110"=>shuju<=m0
when "111"=>shuju<=m1
when others=>null
end case
if (cnt="001" or cnt="110")
then xiaodian<='1'--在里程和总费用的个位处显示小数点
else xiaodian<='0'
end if
end process--结束进程
process(shuju) --开始进程(译码显示)
begin
case shuju is
when "0000"=>duan<="0111111"--0
when "0001"=>duan<="0000110"--1
when "0010"=>duan<="1011011"--2
when "0011"=>duan<="1001111"--3
when "0100"=>duan<="1100110"--4
when "0101"=>duan<="1101101"--5
when "0110"=>duan<="1111101"--6
when "0111"=>duan<="0000111"--7
when "1000"=>duan<="1111111"--8
when "1001"=>duan<="1101111"--9
when others=>null
end case
end process
sel<=cnt
led<=duan
led_dp<=xiaodian
end rt5
二、课程设计工作记录:
包括:设计步骤与时间安排、调试步骤与时间安排、课题完成结果说明
2.课题完成结果说明:
此计费器能实现起步价是5元;实现实验要求的1公里计费一次单价,行驶公里大于2km时每公里按1.4元计费并能显示里程和总共的费用。当行驶了6公里,等待了4分钟时,费用显示为15.8元。与计算公式总费用=起步费用+(里程-2公里)*里程单价+等候时间*等后单价;即15.8=5+(6-2)*1.4+4*1.3。实验结果与理论结果完全一致,实验设计成功。
可以跟老师求求情,有的差的不多就给过了。要不就申请下是否可以补考,提前看看,跟老师打个招呼,基本就过了。实在不行的话就得来年重修,这个是最后的了。另外,所有的挂科最后都会过的,毕竟学校不会让人不毕业。
其实也别担心,既然挂了,重新学习也算补充知识了。学校里的东西有些还是挺重要的,毕业工作了就知道了。所以好好学习吧,毕竟有这个机会还是不错的。