2021-09-01

FPGA的verilog实现自动售货机带找零。

   该售货机的商品每件都是2.5元,每次只能投入1元或者0.5元,若投入3元时,可设找零状态。
   首先设六个状态,分别为0状态,0.5元,1元,1.5元,2元,找0状态。
   代码如下:module fsm(

input wire rst_n,
input wire clk,
input wire [1:0]money,
output reg cola,
output reg _five );
reg [5:0]state;
parameter IDLE = 6’b00_0001;
parameter F_IVE = 6’b00_0010;
parameter ONE = 6’b00_0100;
parameter ONE_FIVE = 6’b00_1000;
parameter TWO = 6’b01_0000;
parameter ZHAO = 6’b10_0000;
always @(posedge clk or negedge rst_n) begin if (rst_n == 1’b0) begin
state<=IDLE;
end else begin
case(state)
IDLE:if(money == 'd1)
state<=F_IVE;
else if(money == 'd2)
state<=ONE;
F_IVE:if(money == 'd1)
state<=ONE;
else if(money == 'd2)
state<=ONE_FIVE;
ONE:if(money == 'd1)
state<=ONE_FIVE;
else if(money == 'd2)
state<=TWO;
ONE_FIVE:if(money == 'd1)
state<=TWO;
else if(money == 'd2)
state<=IDLE;
TWO:if(money == 'd1)
state<=IDLE;
else if(money == 'd2)
state<=ZHAO;
ZHAO:state<=IDLE;
default:state<=IDLE;
endcase
end
end
always @(posedge clk or negedge rst_n) begin
if (rst_n == 1’b0) begin
cola<=1’b0;
end
else if (state == ONE_FIVE && money == 'd2)begin
cola<=1’b1;
end
else if (state == TWO && money == 'd1) begin cola<=1’b1;
end
else if (state == ZHAO) begin
cola<=1’b1; _five<=1’b1;
end
else begin
cola<=1’b0;
_five<=1’b0;
end
end
endmodule

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值