f).第三次投入钱币
图12
第三次投入钱币,Money=“010“代表投入5元,钱币足够(6*3=18<5+10+5=20),判断Castmoney>Totalmoney,MoneyInputAlarm=‘0‘代表钱币已足,系统进入第四个状态,state=01000,Ticketout,出票状态;
g).系统自动出票
系统自动出票,Tout=‘1‘显示在3个周期代表出票3张,系统进入第五个状态,state=10000,Givechagne,找零状态。
h).系统自动找零
系统自动找零,Mout[0]=’1’持续两个周期代表检测restmoney>1,依次找出2张1元的纸币,找零结束,6个数码管显示632032,State=”10000”代表状态停留在找零阶段。
i).再次购买
图13
之后,Rebuy=‘1‘,代表再次购票,进入第一个状态state=00001,系统数据清零,Selstyle,等待选择票种状态,6个数码管显示000000;
j).取消操作
图14
在c),d),e)之后,如果想取消交易,cancelkey=’1’,系统进入找零状态,如,在d)之后取消,自动找零,Mout[1]=‘1‘代表检测到restmoney>5,找出一张5元的纸币,找零结束,6个数码管显示630505;
k).整个购票过程结束。
四、源程序
1.主程序SubwayAutoTicketSellSystem.vhd
---------------------------------------------
--FileName:
SubwayAutoTicketSellSystem.vhd
--Description:
Achievethefunctionofautoticketselling
--Limition:
None
--System:
Vhdl9.0
--Soft:
Quartus2-9.0
--Author:
ChenDawen
--Revision:
Version1.0,2012-10-31
--------------------------------------------
libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
entitySubwayAutoTicketSellSystemis
port(
Restart:
instd_logic;--系统复位信号
Rebuy:
instd_logic;--开始购票操作
Clk:
instd_logic;--系统时钟
Cancelkey:
instd_logic;--取消操作键
Style:
instd_logic_vector(2downto0);--车票种类
Tnum:
instd_logic_vector(2downto0);--所购票数
Money:
instd_logic_vector(2downto0);--投币口
MoneyInputAlarm:
outstd_logic;--投币不足报警信号
Tout:
outstd_logic;--出票
Mout:
outstd_logic_vector(1downto0);--余额找出
State:
outstd_logic_vector(4downto0);--系统状态指示
--beep:
outstd_logic;--蜂鸣器信号
SG:
outstd_logic_vector(6downto0);--段码
BT:
outstd_logic_vector(5downto0)--位码
);endSubwayAutoTicketSellSystem;
architecturebehaveofSubwayAutoTicketSellSystemis
componentdiv_2K--引入div_2k分频模块
port(clk_in:
instd_logic;
clk_out:
outstd_logic);
endcomponent;
componentdiv_500K--引入div_500k分频模块
port(clk_in2:
instd_logic;
clk_out2:
outstd_logic);
endcomponent;
componentkeyin--引入keyin防抖模块
port(clk_in1:
instd_logic;
reset:
instd_logic;
resetn:
outstd_logic);
endcomponent;
typeTTis(selstyle,selticket,inputmoney,ticketout,givechange);
--定义系统转换状态(等待选择票种状态,等待选择购票数量状态,等待放入钱币状态,出票状态,找零状态)
signalclk_tmp:
std_logic;--临时时钟
signalclk_tmp2:
std_logic;--临时时钟2
signalCNT6:
INTEGERRANGE0TO5;
signalA:
INTEGERRANGE0TO5;
signalTstyle:
std_logic_vector(6downto0);--车票单价显示
signalTnumber:
std_logic_vector(6downto0);--车票数量显示
signalMinput1:
std_logic_vector(6downto0);--投入金额十位显示
signalMinput2:
std_logic_vector(6downto0);--投入金额个位显示
signalMinput:
std_logic_vector(4downto0);--投入金额计数
signalToutnum0:
std_logic_vector(6downto0);--出票数量显示
signalToutnum:
std_logic_vector(1downto0);--出票数量计数
signalChangeout0:
std_logic_vector(6downto0);--输出零钱数显示
signalChangeout:
std_logic_vector(3downto0);--输出零钱数量计数signalMoneyn:
std_logic_vector(2downto0);--防抖之后实际投币输入
begin
u1:
div_2kportmap(clk_in=>CLK,clk_out=>clk_tmp);--各种映射
u2:
div_500kportmap(clk_in2=>CLK,clk_out2=>clk_tmp2);
u4:
keyinportmap(clk_in1=>CLK_tmp2,reset=>money(0),resetn=>moneyn(0));
u5:
keyinportmap(clk_in1=>clk_tmp2,reset=>money
(1),resetn=>moneyn
(1));
u6:
keyinportmap(clk_in1=>clk_tmp2,reset=>money
(2),resetn=>moneyn
(2));
--------------------以下为数码管显示程序模块-----------
process(CNT6)
begin
caseCNT6is--3线至6线译码器
when0=>BT<="011111";A<=0;--A为位码
when1=>BT<="101111";A<=1;
when2=>BT<="110111";A<=2;
when3=>BT<="111011";A<=3;
when4=>BT<="111101";A<=4;
when5=>BT<="111110";A<=5;
whenothers=>NULL;
endcase;
endprocess;
process(clk_tmp)
begin
ifclk_tmp'eventandclk_tmp='1'then--实现模6计数器
ifCNT6=5then
CNT6<=0;
else
CNT6<=CNT6+1;
endif;
endif;
endprocess;
process(A)
begin
caseAis--实现数码管的显示功能
when0=>SG<=Tstyle;
when1=>SG<=Tnumber;
when2=>SG<=Minput1;
when3=>SG<=Minput2;
when4=>SG<=Toutnum0;
when5=>SG<=Changeout0;
whenothers=>NULL;
endcase;
endprocess;
process(Minput)--投入金额数码管显示
begin
caseMinputis
when"11011"=>Minput1<="1101101";Minput2<="1110000";--27
when"11010"=>Minput1<="1101101";Minput2<="1011111";--26
when"11001"=>Minput1<="1101101";Minput2<="1011011";--25
when"11000"=>Minput1<="1101101";Minput2<="0110011";--24
when"10111"=>Minput1<="1101101";Minput2<="1111001";--23
when"10110"=>Minput1<="1101101";Minput2<="1101101";--22
when"10101"=>Minput1<="1101101";Minput2<="0110000";--21
when"10100"=>Minput1<="1101101";Minput2<="1111110";--20
when"10011"=>Minput1<="0110000";Minput2<="1111011";--19
when"10010"=>Minput1<="0110000";Minput2<="1111111";--18
when"10001"=>Minput1<="0110000";Minput2<="1110000";--17
when"10000"=>Minput1<="0110000";Minput2<="1011111";--16
when"01111"=>Minput1<="0110000";Minput2<="1011011";--15
when"01110"=>Minput1<="0110000";Minput2<="0110011";--14
when"01101"=>Minput1<="0110000";Minput2<="1111001";--13
when"01100"=>Minput1<="1111110";Minput2<="1101101";--12
when"01011"=>Minput1<="0110000";Minput2<="0110000";--11
when"01010"=>Minput1<="0110000";Minput2<="1111110";--10
when"01001"=>Minput1<="1111110";Minput2<="1111011";--09
when"01000"=>Minput1<="1111110";Minput2<="1111111";--08
when"00111"=>Minput1<="1111110";Minput2<="1110000";--07
when"00110"=>Minput1<="1111110";Minput2<="1011111";--06
when"00101"=>Minput1<="1111110";Minput2<="1011011";--05
when"00100"=>Minput1<="1111110";Minput2<="0110011";--04
when"00011"=>Minput1<="1111110";Minput2<="1111001";--03
when"00010"=>Minput1<="1111110";Minput2<="1101101";--02
when"00001"=>Minput1<="1111110";Minput2<="0110000";--01
when"00000"=>Minput1<="1111110";Minput2<="1111110";--00
whenothers=>null;
endcase;
endprocess;
process(Changeout)--找零金额数码管显示
begin
caseChangeoutis
when"1001"=>