实例六 自动售饮料机

实例六 自动售饮料机

3.2.1. 本章导读

了解自动售货机的工作流程以及各个工作状态,以及其testbench,最后在Robei可视化仿真软件经行功能实现和仿真验证。
设计原理
自动售货机的信号定义:clk:时钟输入;reset:系统复位信号;half_dollar:代表投入5角硬币;one_dollar:代表投入1元硬币;half_out:表示找零信号;dispense:表示机器售出一瓶饮料。
当reset=0时,售货机处于工作状态,此时连续往售货机中投硬币(可以是5角也可以是一元),投入最后一枚硬币时,如果之前投入的硬币总和为2.5元,则可以取走一瓶饮料,如果少于2.5元则继续投币,如果为3元则显示可以取出一瓶饮料,而且找零显示信号为高电平。
在这里插入图片描述

3.2.2. 设计流程

1. sell模块的设计
(1)新建一个模型命名为sell,类型为module,同时具备4个输入和2个输出,每个引脚的属性和名称参照下图3-2-1经行对应的修改。
在这里插入图片描述
(2)添加代码。点击模型下方的 Code添加代码。
代码:
parameter idle=0,half=1,one=2,one_half=3,two=4;
reg[2:0] D;
always @(posedge clk)
begin
if(rst)
begin
dispense=0;
half_out=0;
D=idle;
end
case(D)
idle:
if(half_dollar)
D=half;
else if(one_dollar)
D=one;
half:
if(half_dollar)
D=one;
else if(one_dollar)
D=one_half;
one:
if(half_dollar)
D=one_half;
else if(one_dollar)
D=two;
one_half:
if(half_dollar)
D=two;
else if(one_dollar)
begin
dispense=1;
D=idle;
end
two:
if(half_dollar)
begin
dispense=1;
D=idle;
end
else if(one_dollar)
begin
dispense=1;
half_out=1;
D=idle;
end
endcase
end

(3)保存模型到一个文件夹(文件夹路径不能有空格和中文)中,运行并检查有无错误。

2. sell_test测试文件设计

(1)新建一个具有4个输入2个输出的sell_test测试文件,记得将Module Type设置为 “testbench”,各个引脚配置如图3-2-3所示。

  • 3
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 下面是一个简单的 Python 代码实现自动饮料机功能的示例: ```python # 自动饮料机 # 饮料清单 drinks = { "可乐": 3.0, "雪碧": 3.0, "奶茶": 5.0, "绿茶": 4.0, "红茶": 4.0, "咖啡": 7.0 } # 打印饮料清单 def print_drinks(): print("==== 饮 料 清 单 ====") for drink, price in drinks.items(): print("{} \t ¥{}".format(drink, price)) print("=====================") # 选择饮料 def select_drink(): while True: choice = input("请选择饮料:") if choice in drinks: return choice else: print("抱歉,没有这种饮料,请重新选择。") # 投币 def insert_coin(): while True: coin = input("请投入硬币(1元、5角、1角):") if coin in ["1", "0.5", "0.1"]: return float(coin) else: print("无法识别的硬币,请重新投币。") # 出饮料 def dispense_drink(drink): print("正在出饮料,请稍等...") print("您得到了一杯{}。".format(drink)) # 计算找零 def calculate_change(price, coin): change = coin - price if change > 0: print("找零:¥{}".format(change)) else: print("请取走您的硬币。") # 主程序 def main(): print_drinks() drink = select_drink() price = drinks[drink] coin = insert_coin() if coin >= price: dispense_drink(drink) calculate_change(price, coin) else: print("您投入的金额不足,请取走您的硬币。") # 运行主程序 main() ``` 代码中定义了一个饮料清单(`drinks`),包含多种饮料及其价格。主程序通过调用不同的函数实现了选择饮料、投币、出饮料和计算找零等功能。在运行主程序时,用户可以根据提示进行操作,最终得到自己所选饮料并获得找零。 ### 回答2: 自动饮料机是一种通过按键选择、投币、卖饮料等功能来提供自动化服务的机器。下面是一个简单的Python代码来模拟自动饮料机的功能: ```python class BeverageMachine: def __init__(self): self.menu = { '1': {'name': '可乐', 'price': 2}, '2': {'name': '雪碧', 'price': 2}, '3': {'name': '矿泉水', 'price': 1.5} } self.total_money = 0 def display_menu(self): print('饮料机菜单:') for key, value in self.menu.items(): print(f'{key}. {value["name"]}: {value["price"]}元') print('-------------------------') def select_product(self): while True: choice = input('请选择饮料(输入对应编号):') if choice in self.menu: product = self.menu[choice] if self.total_money >= product['price']: self.total_money -= product['price'] print(f'您选择了{product["name"]}, 找零{self.total_money}元') else: print('金额不足,请投入足够的金额') break else: print('输入无效,请重新选择') def insert_coin(self): coin = float(input('请投币:')) if coin > 0: self.total_money += coin def run(self): self.display_menu() self.insert_coin() self.select_product() if __name__ == '__main__': machine = BeverageMachine() machine.run() ``` 这段代码创建了一个名为BeverageMachine的类,其中包含了菜单和总金额等属性,以及展示菜单、选择饮料、投币等相关方法。在主程序中,实例化了BeverageMachine类的对象machine,并调用run方法来模拟整个自动饮料机的运行过程。 通过输入编号选择饮料,并通过投币来支付饮料的价格,在投币金额足够时卖饮料,并返回找零金额。如果输入的编号或金额无效,则会有相应的提示信息。 ### 回答3: 以下是一个用Python编写的简单的自动饮料机模拟代码: ```python # 定义饮料机类 class BeverageMachine: def __init__(self): self.drinks = {"1": {"name": "可乐", "price": 2}, "2": {"name": "雪碧", "price": 2.5}, "3": {"name": "果汁", "price": 3}} self.balance = 0 # 显示菜单 def display_menu(self): print("欢迎使用自动饮料机!") print("请选择您要购买的饮料:") for key, value in self.drinks.items(): print(f"{key}. {value['name']}(¥{value['price']})") print("0. 退出") # 插入金额 def insert_money(self, amount): self.balance += amount # 购买饮料 def buy_drink(self, selection): if selection in self.drinks.keys(): if self.balance >= self.drinks[selection]["price"]: item = self.drinks[selection]["name"] price = self.drinks[selection]["price"] self.balance -= price print(f"购买成功!您购买的是【{item}】,找零:¥{self.balance}") else: print("余额不足,请继续投币!") elif selection == "0": print("退出购买。") else: print("请选择正确的饮料编号!") # 实例饮料机对象 machine = BeverageMachine() # 显示菜单 machine.display_menu() # 开始购买 while True: choice = input("请输入您的选择:") machine.buy_drink(choice) if choice == "0": break ``` 注意:此代码只是一个简单的模拟,没有实际的硬件交互,仅用于演示。实际的自动饮料机代码可能需要更复杂的逻辑和用户界面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值