描述
题目描述:
设计一个自动贩售机,输入货币有两种,为0.5/1元,饮料价格是1.5/2.5元,要求进行找零,找零只会支付0.5元。
ps:
1、投入的货币会自动经过边沿检测并输出一个在时钟上升沿到1,在下降沿到0的脉冲信号
2、此题忽略出饮料后才能切换饮料的问题
注意rst为低电平复位
信号示意图:
d1 0.5
d2 1
sel 选择饮料
out1 饮料1
out2 饮料2
out3 零钱
波形示意图:
输入描述:
输入信号 clk rst d1 d2 sel
类型 wire
输出描述:
输出信号 out1 out2 out3
类型 reg
`timescale 1ns/1ns
module seller2(
input wire clk ,
input wire rst ,
input wire d1 ,
input wire d2 ,
input wire sel ,
output reg out1,
output reg out2,
output reg out3
);
reg [3:0] cnt;
//*************code***********//
always@(posedge clk or negedge rst)begin
if(!rst)begin
cnt <= 0;
end
else begin
if(!sel && cnt >= 3)
cnt <= 0;
else if(sel && cnt >= 5)
cnt <= 0;
else
cnt <= cnt + {d2,d1};
end
end
always@(posedge clk or negedge rst)begin
if(!rst)begin
out1 <= 0;
out2 <= 0;
out3 <= 0;
end
else begin
if(!sel && cnt >= 3)begin
out1 <= 1;
out2 <= 0;
out3 <= cnt - 3;
end
else if(sel && cnt >= 5)begin
out1 <= 0;
out2 <= 1;
out3 <= cnt - 5;
end
else begin
out1 <= 0;
out2 <= 0;
out3 <= 0;
end
end
end
//*************code***********//
endmodule
没啥需要注意的