- 贝贝
-
这个我自己写的。
module shoumaiji (clk,a,b,duanxuan,weixuan,out,out1);
input clk;
input a,b; //a为投入0.5元信号,b为投入1元信号
output [8:1]duanxuan;//输出8位段选信号
output weixuan;//输出1位位选信号
output out; //out为高电平时售卖机闸门打开,汽水掉出来
output out1; //out1为高电平时候找0.5元
reg weixuan;
reg [8:1]duanxuan;
reg [31:0]count; //数码管动态扫描计数
reg [31:0]count1; //用于计算售卖机闸门打开时间
reg [4:1]a1,b1; //用a1,b1寄存投入的0.5元和1元硬币数目
reg out,out1;
always @(posedge a or posedge out)
begin
if(out==1) a1<=0;//汽水出来后计数值清零
else a1<=a1+4"b1;
end
always @(posedge b or posedge out)
begin
if(out==1) b1<=0;//汽水出来后计数值清零
else b1<=b1+4"b1;
end
always@(posedge clk) //1KHz时钟
begin
if(a1==5) out<=1; //因为汽水为2.5,可以给5个0.5元
else if(a1==3&&b1==1) out<=1; //可以给3个0.5元和1个1元
else if(a1==1&&b1==2) out<=1; //,可以给1个0.5元和2个1元
else if(b1==3)begin out<=1;out1<=1;end //可以给3个一块的,系统找回0.5元
else if(a1==0&&b1==0) //如果没人投币,则两位数码管显示0
begin
if(count==50) //如果实际运行中发现数码管有闪烁,可以将此数值调小
begin
weixuan<=1"b0;
duanxuan<=8"b00111111; //0
end
if(count==100) //如果实际运行中发现数码管有闪烁,可以将此数值调小
begin
weixuan<=1"b1;
duanxuan<=8"b00111111; //0
count<=0;
end
end
else if(a==1) //投币0.5元,显示0.5,假设数码管为共阴极的
begin
count<=count+1;
if(count==50) //如果实际运行中发现数码管有闪烁,可以将此数值调小
begin
weixuan<=1"b0;
duanxuan<=8"b01101101; //5
end
if(count==100) //如果实际运行中发现数码管有闪烁,可以将此数值调小
begin
weixuan<=1"b1;
duanxuan<=8"b10111111; //0.
count<=0;
end
end
else if(b==1) //投币1元,显示01
begin
count<=count+1;
if(count==50) //如果实际运行中发现数码管有闪烁,可以将此数值调小
begin
weixuan<=1"b0;
duanxuan<=8"b00000110; //1
end
if(count==100) //如果实际运行中发现数码管有闪烁,可以将此数值调小
begin
weixuan<=1"b1;
duanxuan<=8"b00111111; //0
count<=0;
end
end
if(out==1)
begin
if(count1==3000)//让闸门打开3秒,由于为1KHz时钟,故要数3000下
begin
out<=0;
out1<=0;
count1<=0;
end
else count1<=count1+1;
end
end
endmodule
- 蓓蓓
-
module sale_machine(
clk ,
rstn ,
set_ok , //BTN
set_cancel , //BTN
set_up , //BTN
set_down , //BTN
set_commodity_sel , //DATA DISP
set_commodity_price , //DATA DISP
money_in_05 , //PULSE
money_in_10 , //PULSE
buy_in_ok , //BTN
buy_in_cancel , //BTN
buy_out_ok_flag , //LED
buy_out_ng_flag , //LED
commodity_out , //PULSE
coin_out_en , //PULSE
coin_out_num //PULSE
);
//-------------------------------------------
//signal
//-------------------------------------------
input clk ;
input rstn ;
input set_ok ;
input set_cancel ;
input set_up ;
input set_down ;
output [1:0] set_commodity_sel ;
output [15:0] set_commodity_price ;
input money_in_05 ;
input money_in_10 ;
input [3:0] buy_in_ok ;
input buy_in_cancel ;
output [3:0] buy_out_ok_flag ;
output buy_out_ng_flag ;
output [3:0] commodity_out ;
output coin_out_en ;
output [31:0] coin_out_num ;
//-------------------------
//set mode signal
//-------------------------
//pulse check out
wire n_set_ok_p ;
wire n_set_cancel_p ;
wire n_set_up_p ;
wire n_set_down_p ;
reg r_set_ok ;
reg r_set_cancel ;
reg r_set_up ;
reg r_set_down ;
//set state
reg r_set_idle_st ;
reg r_set_select_st ;
reg r_set_price_st ;
//set datain
reg [1:0] r_sel ;
reg [15:0] r_price ;
reg [15:0] r_commodity_A_pr ;
reg [15:0] r_commodity_B_pr ;
reg [15:0] r_commodity_C_pr ;
reg [15:0] r_commodity_D_pr ;
//-------------------------
//buy commodity signal
//-------------------------
//pulse check out
wire [3:0] n_buy_in_ok_p ;
wire n_buy_in_cancel_p ;
reg [3:0] r_buy_in_ok ;
reg r_buy_in_cancel ;
//buy out flag
wire n_buy_bought ;
wire n_buy_in_ok_all ;
wire n_buy_out_ok_all ;
wire [3:0] buy_out_ok_flag ;
wire buy_out_ng_flag_clr ;
reg [3:0] r_buy_out_ng_flag_cnt;
reg buy_out_ng_flag ;
//money cnt
reg clr ;
reg [31:0] r_money_cnt ;
//-------------------------
//coin out signal
//-------------------------
reg coin_out_en ;
reg [31:0] coin_out_num ;
//-------------------------------------------
//rtl
//-------------------------------------------
//-------------------------
//set mode rtl
//-------------------------
//pulse check out
assign n_set_ok_p = set_ok & ~r_set_ok ;
assign n_set_cancel_p = set_cancel & ~r_set_cancel ;
assign n_set_up_p = set_up & ~r_set_up ;
assign n_set_down_p = set_down & ~r_set_down ;
always @(posedge clk or negedge rstn) begin
if(!rstn) begin
r_set_ok <= 1"b0;
r_set_cancel <= 1"b0;
r_set_up <= 1"b0;
r_set_down <= 1"b0;
end
else
begin
r_set_ok <= set_ok ;
r_set_cancel <= set_cancel ;
r_set_up <= set_up ;
r_set_down <= set_down ;
end
end
//set state
always @(posedge clk or negedge rstn) begin
if(!rstn) begin
r_set_idle_st <= 1"b1;
end
else begin
if(n_set_ok_p) begin
if(r_set_price_st) r_set_idle_st <= 1"b1;
else if(r_set_idle_st) r_set_idle_st <= 1"b0;
else r_set_idle_st <= r_set_idle_st;
end
else if(n_set_cancel_p) r_set_idle_st <= 1"b1;
end
end
always @(posedge clk or negedge rstn) begin
if(!rstn) begin
r_set_select_st <= 1"b0;
end
else begin
if(n_set_ok_p) begin
if(r_set_idle_st) r_set_select_st <= 1"b1;
else if(r_set_select_st) r_set_select_st <= 1"b0;
else r_set_select_st <= r_set_select_st;
end
else if(n_set_cancel_p) r_set_select_st <= 1"b0;
end
end
always @(posedge clk or negedge rstn) begin
if(!rstn) begin
r_set_price_st <= 1"b0;
end
else begin
if(n_set_ok_p) begin
if(r_set_select_st) r_set_price_st <= 1"b1;
else if(r_set_price_st) r_set_price_st <= 1"b0;
else r_set_price_st <= r_set_price_st;
end
else if(n_set_cancel_p) r_set_price_st <= 1"b0;
end
end
//set datain
assign set_commodity_sel = r_sel;
assign set_commodity_price = r_price;
always @(posedge clk or negedge rstn) begin
if(!rstn) begin
r_sel <= 2"b00;
end
else begin
if(r_set_idle_st) r_sel <= 2"b00;
else if(r_set_select_st) begin
if(n_set_up_p) r_sel <= r_sel + 2"b01;
else if(n_set_down_p) r_sel <= r_sel - 2"b01;
else r_sel <= r_sel;
end
end
end
always @(posedge clk or negedge rstn) begin
if(!rstn) begin
r_price <= 16"h0000;
end
else begin
if(r_set_idle_st) r_price <= 16"h0000;
else if(r_set_select_st) begin
if(n_set_up_p) begin
if(r_sel == 2"b00) r_price <= r_commodity_B_pr;
else if(r_sel == 2"b01) r_price <= r_commodity_C_pr;
else if(r_sel == 2"b10) r_price <= r_commodity_D_pr;
else if(r_sel == 2"b11) r_price <= r_commodity_A_pr;
end
else if(n_set_down_p) begin
if(r_sel == 2"b00) r_price <= r_commodity_D_pr;
else if(r_sel == 2"b01) r_price <= r_commodity_A_pr;
else if(r_sel == 2"b10) r_price <= r_commodity_B_pr;
else if(r_sel == 2"b11) r_price <= r_commodity_C_pr;
end
else
r_price <= r_price;
end
else if(r_set_price_st) begin
if(n_set_up_p) r_price <= r_price + 2"b01;
else if(n_set_down_p) r_price <= r_price - 2"b01;
else r_price <= r_price;
end
end
end
always @(posedge clk or negedge rstn) begin
if(!rstn) begin
r_commodity_A_pr <= 16"h0000;
r_commodity_B_pr <= 16"h0000;
r_commodity_C_pr <= 16"h0000;
r_commodity_D_pr <= 16"h0000;
end
else begin
if((r_set_price_st) && (n_set_ok_p)) begin
if(r_sel == 2"b00) r_commodity_A_pr <= r_price;
else if(r_sel == 2"b01) r_commodity_B_pr <= r_price;
else if(r_sel == 2"b10) r_commodity_C_pr <= r_price;
else if(r_sel == 2"b11) r_commodity_D_pr <= r_price;
end
else begin
r_commodity_A_pr <= r_commodity_A_pr;
r_commodity_B_pr <= r_commodity_B_pr;
r_commodity_C_pr <= r_commodity_C_pr;
r_commodity_D_pr <= r_commodity_D_pr;
end
end
end
//-------------------------
//buy commodity rtl
//-------------------------
//pulse check out
assign n_buy_in_ok_p[0] = buy_in_ok[0] & ~r_buy_in_ok[0] ;
assign n_buy_in_ok_p[1] = buy_in_ok[0] & ~r_buy_in_ok[1] ;
assign n_buy_in_ok_p[2] = buy_in_ok[0] & ~r_buy_in_ok[2] ;
assign n_buy_in_ok_p[3] = buy_in_ok[0] & ~r_buy_in_ok[3] ;
assign n_buy_in_cancel_p = buy_in_cancel & ~r_buy_in_cancel ;
always @(posedge clk or negedge rstn) begin
if(!rstn) begin
r_buy_in_ok <= 4"b0000;
r_buy_in_cancel <= 1"b0;
end
else if(r_set_idle_st) //in idle
begin
r_buy_in_ok <= buy_in_ok ;
r_buy_in_cancel <= buy_in_cancel;
end
else begin
r_buy_in_ok <= 4"b0000;
r_buy_in_cancel <= 1"b0;
end
end
//buy out flag
assign commodity_out[0] = (n_buy_in_ok_p[0]) && (buy_out_ok_flag[0]);
assign commodity_out[1] = (n_buy_in_ok_p[1]) && (buy_out_ok_flag[1]);
assign commodity_out[2] = (n_buy_in_ok_p[2]) && (buy_out_ok_flag[2]);
assign commodity_out[3] = (n_buy_in_ok_p[3]) && (buy_out_ok_flag[3]);
assign n_buy_bought = ( ((n_buy_in_ok_p[0]) && (buy_out_ok_flag[0])) ||
((n_buy_in_ok_p[1]) && (buy_out_ok_flag[1])) ||
((n_buy_in_ok_p[2]) && (buy_out_ok_flag[2])) ||
((n_buy_in_ok_p[3]) && (buy_out_ok_flag[3])) );
assign n_buy_in_ok_all = | n_buy_in_ok_p; //BTN IN ALL
assign n_buy_out_ok_all = | buy_out_ok_flag; //LED OUT ALL
assign buy_out_ok_flag[0] = (r_money_cnt >= r_commodity_A_pr) ? 1"b1 : 1"b0;
assign buy_out_ok_flag[1] = (r_money_cnt >= r_commodity_B_pr) ? 1"b1 : 1"b0;
assign buy_out_ok_flag[2] = (r_money_cnt >= r_commodity_C_pr) ? 1"b1 : 1"b0;
assign buy_out_ok_flag[3] = (r_money_cnt >= r_commodity_D_pr) ? 1"b1 : 1"b0;
assign buy_out_ng_flag_clr = (r_buy_out_ng_flag_cnt == 4"hF) ? 1"b1 : 1"b0;
always @(posedge clk or negedge rstn) begin
if(!rstn) r_buy_out_ng_flag_cnt <= 4"h0;
else if(buy_out_ng_flag_clr)
r_buy_out_ng_flag_cnt <= 4"h0;
else if(buy_out_ng_flag)
r_buy_out_ng_flag_cnt <= r_buy_out_ng_flag_cnt + 4"h1;
else
r_buy_out_ng_flag_cnt <= 4"h0;
end
always @(posedge clk or negedge rstn) begin
if(!rstn) buy_out_ng_flag <= 1"b0;
else if((~n_buy_out_ok_all) && (n_buy_in_ok_all))
buy_out_ng_flag <= 1"b1;
else if(buy_out_ng_flag_clr)
buy_out_ng_flag <= 1"b0;
end
//money cnt
always @(posedge clk or negedge rstn) begin
if(!rstn) clr <= 1"b0;
else if(n_buy_bought || buy_in_cancel)
clr <= 1"b1;
else
clr <= 1"b0;
end
always @(posedge clk or negedge rstn) begin
if(!rstn) begin
r_money_cnt <= 32"h00000000;
end
else begin
if(money_in_05) r_money_cnt <= r_money_cnt + 32"h00000001;
else if(money_in_10) r_money_cnt <= r_money_cnt + 32"h00000002;
else if((n_buy_in_ok_p[0]) && (buy_out_ok_flag[0]))
r_money_cnt <= r_money_cnt - r_commodity_A_pr;
else if((n_buy_in_ok_p[1]) && (buy_out_ok_flag[1]))
r_money_cnt <= r_money_cnt - r_commodity_B_pr;
else if((n_buy_in_ok_p[2]) && (buy_out_ok_flag[2]))
r_money_cnt <= r_money_cnt - r_commodity_C_pr;
else if((n_buy_in_ok_p[3]) && (buy_out_ok_flag[3]))
r_money_cnt <= r_money_cnt - r_commodity_D_pr;
else if(clr)
r_money_cnt <= 32"h00000000;
end
end
//-------------------------
//coin out rtl
//-------------------------
always @(posedge clk or negedge rstn) begin
if(!rstn) coin_out_en <= 1"b0;
else if(clr)
coin_out_en <= 1"b1;
else
coin_out_en <= 1"b0;
end
always @(posedge clk or negedge rstn) begin
if(!rstn) coin_out_num <= 32"h00000000;
else if(clr)
coin_out_num <= r_money_cnt;
else
coin_out_num <= 32"h00000000;
end
endmodule
- gitcloud
-
* 回复内容中包含的链接未经审核,可能存在风险,暂不予完整展示!http://x.s**.com/cgi-bin/show_detail?Hash=5837AE21070DE484A6B69A7FB16D2289DA8CF28E
这是QQ资源下载,进去直接下载就好了,这个没有你说的这么繁琐,
但是足够你参考的了,希望可以帮到你。