从零开始动手做Verilog实验--03--自动售卖机

一、背景知识

1.接受的只有两种面值: 0.5美元和1美元纸币

2.商品定价为1.5美元

二、代码

sale_machine.v代码如下:

c 复制代码
`timescale 1ns / 1ps

module sale_machine(
    input clk                  ,
    input rst                  ,
    input half_dollar          ,
    input one_dollar           ,
    output reg half_out            ,
    output reg [1:0] sell                ,
    output reg fetch               
    
    );
    
    reg [2:0] status;
    localparam idle = 3'b000;
    localparam half = 3'b001;
    localparam one = 3'b010;
    localparam one_half = 3'b011;
    localparam two = 3'b100;
    
    always @(posedge clk)
    begin
            if(!rst)
            begin
                half_out    = 0;
                sell        = 0;
                fetch       = 0;
                status = 0;
            end
            else
                case(status)
                idle: 
                        if(half_dollar)   begin
                        status = half;   
                        half_out  = 0;      
                        sell        = 2'b00;
                        fetch    =1'b0;     
                        end                                       
                        else if(one_dollar) begin
                        half_out  = 0;            
                        sell        = 2'b00;      
                        fetch    =1'b0;                               
                        status = one;
                        end
                half: 
                          if(half_dollar) begin
                          status = one;
                          half_out  = 0;            
                          sell        = 2'b00;      
                          fetch    =1'b0;           
                          end                       
                                               
                          else if(one_dollar) begin
                          status =one_half;
                          half_out  = 0;            
                          sell        = 2'b00;      
                          fetch    =1'b0;           
                          end                       
                     
                one_half:
                        if(half_dollar) begin
                        status = two;
                        half_out  = 0;            
                        sell        = 2'b00;      
                        fetch    =1'b0;           
                        end                       
                                            
                        else if(one_dollar) 
                            begin
                                half_out  = 0;
                                sell        = 2'b01;   
                                fetch    =1'b1;   
                                status = idle;
                             end
                 two: 
                        if(half_dollar)
                           begin                        
                               half_out  = 1'b1;               
                               sell        = 2'b01;     
                               fetch    =1'b1;         
                               status = idle;          
                            end                        
                        else if( one_dollar )
                                begin                              
                                    half_out  = 1'b1;              
                                    sell        = 2'b10;           
                                    fetch    =1'b1;                
                                    status = idle;                 
                                 end                               
                   default: begin
                            half_out  = 0;           
                            sell        = 2'b01;     
                            fetch    =1'b1;    
                            status = idle;                                   
                            end
                     endcase       
                        
    end                     
 
endmodule

仿真文件sale_machine_tb.v:

c 复制代码
`timescale 1ns / 1ps


module sale_machine_tb;

reg       clk                ;
reg       rst                ;
reg       half_dollar        ;
reg       one_dollar         ;
wire      half_out      ;
wire      [1:0] sell    ;
wire      fetch         ;     

parameter cycle = 20;
always #(cycle/2) clk =~clk;



sale_machine u_machine(
    .clk         (clk        ) ,
    .rst         (rst        ) ,
    .half_dollar (half_dollar) ,
    .one_dollar  (one_dollar ) ,
    .half_out   (half_out    )      ,
    .sell       (sell        )      ,
    .fetch      (fetch       )      
    
    );
    
    
    initial begin
    
    clk =0; 
    rst=0;
    half_dollar  =0;
    one_dollar   =0;

    #cycle rst = 1'b1; half_dollar  = 1'b1;
   #(cycle * 10  )   $finish;
     
    end
endmodule

三、仿真图

四、原理图

相关推荐
传感器与混合集成电路17 小时前
伺服数据采集控制模块系统集成实战手册:接口设计要点、上电顺序与开发环境配置全解析
fpga开发
xxLearn1 天前
Vivado 2025.2 下载程序时提示:“ERROR : invalid command name ps7_init“
fpga开发
科恒盛远1 天前
【无标题】
fpga开发·硬件工程·信号处理
千寻xun2 天前
一、理论篇-NVME协议学习笔记
笔记·学习·fpga开发·nvme ssd·nvme协议
AndyHeee2 天前
【PCIe中的BAR、MMIO、MMU、mmap函数与页表】
fpga开发
nuoxin1143 天前
HR4988替代A4988-富利威
网络·人工智能·嵌入式硬件·fpga开发·dsp开发
一口一口吃成大V3 天前
vivado的bit 和 bin的区别
fpga开发
尤老师FPGA4 天前
HDMI数据的接收发送实验(十八)
fpga开发
北京青翼科技4 天前
青翼科技 JFM7K325T FPGA+FT-M6678 DSP 的全国产化信号处理平台丨FPGA开发板
fpga开发·数据采集卡·fmc子卡·fpga开发板·ad采集卡·图像处理卡·dsp信号处理
zlinear数据采集卡4 天前
从0到1硬核拆解:工业级数据采集卡的隔离设计与Modbus通信实战
arm开发·单片机·嵌入式硬件·fpga开发·开源