verilog设计饮料机

基于Verilog,设计一个自动售卖饮料机。2.5元/瓶,可投1元硬币和5角硬币,可找零。

设计思路:基于有限状态机。
引入5个状态:IDLE,S1,S2,S3,S4;
分别代表空闲状态,已投币0.5元,已投币1.0元,已投币1.5元,已投币2.0元。

verilog代码:

module vendor(clk, rst_n, half_in, one_in, collect, half_out);
input clk;
input rst_n;
input half_in;
input one_in;
output collect;
output half_out;
parameter IDLE=0;
parameter S1=1;
parameter S2=2;
parameter S3=3;
parameter S4=4;
reg [2:0] state_c;
reg [2:0] state_n;
reg collect;
reg [1:0] half_out;
always @(posedge clk or negedge rst_n) begin
 if (!rst_n) begin
  state_c<=IDLE;
 end
 else begin
  state_c<=state_n;
 end
end

always @(*) begin 
 if (!rst_n) begin
  state_n=IDLE;
 end
 else begin
  case (state_c)
   IDLE: begin
    if (half_in==0&&one_in==0)
     state_n=state_c;
    else if (half_in==0&&one_in==1)
     state_n=S2;
    else if (half_in==1&&one_in==0)
     state_n=S1;
    else if (half_in==1&&one_in==1)
     state_n=S3;
    else
     state_n=state_c;
    end

   S1: begin
    if (half_in==0&&one_in==0)
     state_n=state_c;
    else if (half_in==0&&one_in==1)
     state_n=S3;
    else if (half_in==1&&one_in==0)
     state_n=S2;
    else if (half_in==1&&one_in==1)
     state_n=S4;
    else
     state_n=state_c;
   end
   
   S2: begin
    if (half_in==0&&one_in==0)
     state_n=state_c;
    else if (half_in==0&&one_in==1)
     state_n=S4;
    else if (half_in==1&&one_in==0)
     state_n=S3;
    else if (half_in==1&&one_in==1)
     state_n=S4;
    else
     state_n=state_c;
   end
   
    S3: begin // 1.5 多投就找回
    if (half_in==0&&one_in==0)
     state_n=state_c;
    else if (half_in==0&&one_in==1)
     state_n=IDLE;
    else if (half_in==1&&one_in==0)
     state_n=S4;
    else if (half_in==1&&one_in==1)
     state_n=IDLE;
    else
     state_n=state_c;
   end
   
    S4: begin // S4多投了就全找回 不考虑一次性买2瓶饮料 一瓶一瓶地买
    if (half_in==0&&one_in==0)
     state_n=state_c;
    else if (half_in==0&&one_in==1)
     state_n=IDLE;
    else if (half_in==1&&one_in==0)
     state_n=IDLE;
    else if (half_in==1&&one_in==1)
     state_n=IDLE;
    else
     state_n=state_c;
   end
   
   default: begin
    state_n=state_c;
   end
endcase
end
end
   
always @(posedge clk or negedge rst_n) begin
 if (!rst_n) begin
  collect<=1'b0;
  half_out<=2'b00;
 end
 
 else begin
 
  if (state_c==IDLE) begin
     collect<=1'b0;
     half_out<=2'b00;
  end
  
  
  else if (state_c==S1) begin
     collect<=1'b0;
     half_out<=2'b00;
  end
  
  else if (state_c==S2) begin
    collect<=1'b0;
    half_out<=2'b00;
  end
  
  else if (state_c==S4) begin
   if (half_in==1&&one_in==1) begin
     collect<=1'b1;
     half_out<=2'b10;// 
   end
   else if (half_in==0&&one_in==1) begin
     collect<=1'b1;
     half_out<=2'b01;
   end
   else if (half_in==1&&one_in==0) begin
     collect<=1'b1;
     half_out<=2'b00;
   end 
   else begin
    collect<=1'b0;
    half_out<=2'b00;
   end
  end
  
   else if (state_c==S3) begin
   if (half_in==1&&one_in==1) begin
     collect<=1'b1;
     half_out<=2'b01;
   end
   else if (half_in==0&&one_in==1) begin
     collect<=1'b1;
     half_out<=2'b00;
   end
   else begin
    collect<=1'b0;
    half_out<=2'b00;
   end
  end
  
  else begin
   collect<=1'b0;
   half_out<=2'b00;
  end   
 
 end
end            
   
endmodule

testbench文件:

`timescale 1ns / 1ns

module sim_vendor();
reg clk;
reg rst_n;
reg half_in;
reg one_in;
wire [1:0] half_out;
wire collect;

vendor sim_vendor(
.clk(clk),
.rst_n(rst_n),
.half_in(half_in),
.one_in(one_in),
.collect(collect),
.half_out(half_out)
);

initial begin
 rst_n =1'b0;
 clk = 1'b1;
 half_in = 1'b0;
 one_in = 1'b0;
 # 2  rst_n = 1'b1;
end  
        
always #1 clk = ~clk;
always # 2  half_in = ~half_in;       
always # 3 one_in = ~one_in;   
endmodule

vivado仿真结果:
在这里插入图片描述

  • 6
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
饮料状态机是一种用Verilog语言实现的状态机,用于模拟自动饮料卖机或卖报机的行为。根据引用\[3\]中的代码示例,我们可以看到状态机的基本结构和实现方式。 首先,状态机使用参数定义了不同的状态,比如IDLE和S0。这些状态可以根据具体需求进行定义。 然后,使用寄存器(curr_state和next_state)来存储当前状态和下一个状态。在时钟的上升沿或异步复位信号的上升沿触发时,当前状态会更新为下一个状态。 接下来,使用组合逻辑来确定下一个状态。根据当前状态和输入信号,使用case语句来确定下一个状态的值。每个状态都可以根据具体需求进行定义。 最后,根据需要,可以选择使用时序逻辑或组合逻辑来输出结果。时序逻辑的输出在时钟的上升沿触发后更新,而组合逻辑的输出则是根据当前状态和输入信号的组合逻辑计算得出。 总结来说,饮料状态机是一种用Verilog语言实现的状态机,用于模拟自动饮料卖机或卖报机的行为。它通过定义不同的状态、使用寄存器存储当前状态和下一个状态、使用组合逻辑确定下一个状态以及选择时序逻辑或组合逻辑输出结果来实现。 #### 引用[.reference_title] - *1* *3* [Verilog实现状态机与状态机经典示例——序列检测器、自动饮料卖机](https://blog.csdn.net/qq_34070723/article/details/100737225)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【FPGA】Verilog语言通过状态机实现可乐机系统](https://blog.csdn.net/lzh1415926/article/details/124365489)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值