赞
踩
名称:自动售货机设计Verilog代码Quartus仿真(文末获取)
软件:Quartus
语言:Verilog
代码功能:
2.设计内容及基本要求
设计一个小商品自动售货机,要求如下
(1)基本功能
①售货机只售出价值为5角、1元、1.5元和2元的小商品。购买者每次通过开关选择一种小商品进行购买
②用开关分别模拟5角、1元硬币和5元纸币投入(一次只能投入一个硬币或张纸币)。当所投面值达到或超出购买者所选面值,并确认购买时,购买成功:小商品对应的灯亮表示售出。投入确认和购买确认可用按钮
③当所投币值不足面值时,可通过一个复位按钮退回所投币值,回到初始状
④若退回或找零时,用几组灯分别表示找零数量:一组灯表示找回的5元纸币的数量,一组灯表示找回的1元的数量,一组表示找回的角的数量
⑤售出货物或退回后,都回到初始状态。
1. 工程文件
2. 程序运行
3. 管脚约束
4. 程序仿真
部分代码展示:
module auto_sell( input clk,//50M input reset_n,//复位 //开关 input select_5,//选中5角商品 input select_10,//选中1元商品 input select_15,//选中1.5元商品 input select_20,//选中2元商品 input money_5,//投入5角 input money_10,//投入1元 input money_15,//投入1.5元 input money_50,//投入5元 //按键 input confirm_purchase,//确认购买 input refund_money,//退钱 //LED output reg purchase_5,//购买成功指示灯 output reg purchase_10,//购买成功指示灯 output reg purchase_15,//购买成功指示灯 output reg purchase_20,//购买成功指示灯 output [2:0]give_change_5,//找零5角 output [2:0]give_change_10,//找零1元 output [2:0]give_change_15,//找零1.5元 output [2:0]give_change_20//找零2元 ); parameter s_idle=4'd0; parameter s_select=4'd1; parameter s_incoin=4'd2; parameter s_refund=4'd3; parameter s_wait5s=4'd4;//找零时间5秒 parameter s_refund_20=4'd5; parameter s_refund_15=4'd6; parameter s_refund_10=4'd7; parameter s_refund_5=4'd8; parameter s_end=4'd9; reg [3:0] state=4'd0; reg [7:0] input_money=8'd0;//投入总钱币 reg [7:0] select_need_money=8'd0;//选中商品价钱 reg [7:0] refund_money_out=8'd0;//找零金额 //选中商品价钱 always@(posedge clk) if(reset_n==0 || state==s_end) select_need_money<=8'd0; else if(state==s_idle) if(select_5==1) select_need_money<=8'd5; else if(select_10==1) select_need_money<=8'd10; else if(select_15==1) select_need_money<=8'd15; else if(select_20==1) select_need_money<=8'd20; else select_need_money<=select_need_money; else select_need_money<=select_need_money; reg money_5_reg=0; reg money_10_reg=0; reg money_15_reg=0; reg money_50_reg=0; always@(posedge clk) begin money_5_reg<=money_5; money_10_reg<=money_10; money_15_reg<=money_15; money_50_reg<=money_50; end wire money_5_down; wire money_10_down; wire money_15_down; wire money_50_down; assign money_5_down=~money_5 & money_5_reg; assign money_10_down=~money_10 & money_10_reg; assign money_15_down=~money_15 & money_15_reg; assign money_50_down=~money_50 & money_50_reg; //投入总钱币 always@(posedge clk) if(reset_n==0 || state==s_end) input_money<=8'd0; else if(state==s_select) if(money_5_down) input_money<=input_money+8'd5; else if(money_10_down) input_money<=input_money+8'd10; else if(money_15_down) input_money<=input_money+8'd15; else if(money_50_down) input_money<=input_money+8'd50; else input_money<=input_money; else input_money<=input_money; //计算找零金额 always@(posedge clk) if(reset_n==0 || state==s_end) refund_money_out<=8'd0; else if(state==s_select) if(input_money<select_need_money)//投入不足 if(refund_money==0)//不买,退钱 refund_money_out<=input_money;//找零 else refund_money_out<=refund_money_out; else//投入足够 if(confirm_purchase==0)//确认购买 refund_money_out<=input_money-select_need_money;//找零 else if(refund_money==0)//不买,退钱 refund_money_out<=input_money;//找零 else refund_money_out<=refund_money_out; else if(state==s_refund_20) refund_money_out<=refund_money_out-8'd20; else if(state==s_refund_15) refund_money_out<=refund_money_out-8'd15; else if(state==s_refund_10) refund_money_out<=refund_money_out-8'd10; else if(state==s_refund_5) refund_money_out<=refund_money_out-8'd5; //对应面值数量 reg [2:0] money_5_cnt=3'd0; reg [2:0] money_10_cnt=3'd0; reg [2:0] money_15_cnt=3'd0; reg [2:0] money_20_cnt=3'd0; assign give_change_5=money_5_cnt;//找零5角 assign give_change_10=money_10_cnt;//找零1元 assign give_change_15=money_15_cnt;//找零1.5元 assign give_change_20=money_20_cnt;//找零2元 always@(posedge clk) if(reset_n==0 || state==s_end) money_20_cnt<=3'd0; else if(state==s_refund_20) money_20_cnt<=money_20_cnt+3'd1; else money_20_cnt<=money_20_cnt; always@(posedge clk) if(reset_n==0 || state==s_end) money_15_cnt<=3'd0; else if(state==s_refund_15) money_15_cnt<=money_15_cnt+3'd1; else money_15_cnt<=money_15_cnt; always@(posedge clk) if(reset_n==0 || state==s_end) money_10_cnt<=3'd0; else if(state==s_refund_10) money_10_cnt<=money_10_cnt+3'd1; else money_10_cnt<=money_10_cnt; always@(posedge clk) if(reset_n==0 || state==s_end) money_5_cnt<=3'd0; else if(state==s_refund_5) money_5_cnt<=money_5_cnt+3'd1; else money_5_cnt<=money_5_cnt;
扫描文章末尾的公众号二维码
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。