您好,欢迎来到华佗小知识。
搜索
您的当前位置:首页自动售货机器

自动售货机器

来源:华佗小知识


题目

自动售货机的研究

TITLE

THE RESEARCH OF AUTOMATIC VENDING

MACHINE

摘要

电子技术的发展,特别是专用集成电路(ASIC)设计技术的日趋进步和完善,推动了数字系统设计的迅猛发展,电子设计自动化(EDA)工具给电子设计带来了巨大的变革,尤其是硬件描述语言的出现和发展,解决了传统用电路原理图设计大系统工程时的诸多不便。

本系统是利用数字控制系统即使用VHDL语言设计的自动售货机。VHDL是目前最通用的硬件描述语言,它可描述一个数字电路的输入、输出以及相互间的行为与功能。本系统利用VHDL设计程序结构,以此来控制所有的买卖行为,实现其功能。

在设计该系统时,将其分解为若干个子系统。子系统再分解为一个个功能的模块,这些模块经过设计、仿真、功能正确后,像拼积木似的被根据需要实现不同的拼接,完成特定的功能。

关键词: VHDL 模块 仿真

ABSTRACT

With the development of electronic technology, especially the improvements of ASIC design technology greatly impulse the digital system design .EDA tool brings the large reformation. The appearance and development of hardware description language, resolve a lot of problems that lie in the large system process with traditional circuit principle designing.

The text is automatic vending machine which is designed with digital control system----VHDL. At present, VHDL is the current hardware description

Language which can description the input, output and behavior and function one another of a digital circuit.

The system use VHDL to design program structure, so that it can control all of the behaviors of sale and realize its functions. When designing this system , they are divided into several subsystems . Then the subsystem is divided into some blocks which can execute function independently. After designing , simulating, function, these blocks are linked together according to demands to realize the special functions.

KEYWORDS: VHDL block simulate

前言

电子技术的发展,特别是专用集成电路(ASIC)设计技术的日趋进步和完善,推动了数字系统设计的迅猛发展。电子设计自动化(EDA)工具给电子设计带来了巨大的变革,尤其是硬件描述语言的出现和发展,解决了传统用电路原理设计系统工程时的诸多不便,成为电子电路设计人员的最得力助手。学习VHDL已日益成为我国高等学校学生和工程技术人员的迫切需要。

由于CPLD/FPGA等IC硬件与VHDL/Verilog等硬件语言的发展,传统数字逻辑与数字系统设计的课程应全面简化与调整。不必再将大量的时间用于逻辑函数的推导以及烦琐的卡诺图化简上,因为VHDL与Verilog等硬件描述语言(HDL)能帮你完成全部工作,只需具有基本逻辑概念与时间序列分析概念即可。既不必去考虑各式各样的TTL IC,也不必用拿着数据手册按照引脚在印刷电路板上一根一根地画线。这些简单的劳动, Xilinx/Altera公司的EDA软件环境都会在CPLD/FPGA 芯片上帮用户完成,而且随时可以更改设计。这两大CPLD/FPGA厂商免费提供EDA软件设计环境,可以很容易地利用他们开发数字集成电路。

本设计是利用VHDL,用模块合成的方式来完成的。根据自动售货机的功能以及顾客的各种买卖行为,将其分为几大模块来处理。

本文在设计过程中得到冯涛等老师的大力帮助,在此表示感谢。由于本人理论水平和实践经验有限,文中难免存在错误与不妥之处,敬请批评指正。

目录

摘要

ABSTRACT 前言

第一章 VHDL 语言概述……………………………………………1

§1.1 概述………………………………………………………………….1 §1.2 语言特性……………………………………………………………1

第二章 EPM7128芯片简介…………………………………………3 第三章 MAX+plus概

述……………………………………………..5

第四章 自动售货

机……………………………………………………6

§4.1 功能概述…………………………………………………………..6 §4.2 外观……………………………………………………………….6 §4.3 系统设计说明……………………………………………………..7 §4.3.1 entity 定义模块…………………………………………..8 §4.3.2 architecture 模块…………………………………………10 §4.3.3 产生退币闪烁信号的电路模块…………………………11 §4.3.4 投入10元硬币的处理电路模块………………………..13 §4.3.5 投入5元硬币的处理电路模块………………………....15 §4.3.6 饮料选择处理电路模块…………………………………17 §4.3.7 确认与取消处理电路模块……………………………....19 §4.3.8 退币处理电路模块……………………………………….20 §4.3.9 出货并计算存货电路模块……………………………….24 §4.4 debouncing 电路模块………………………………………….27

结束

语…………………………………………………………………..…32

致谢………………………………………………………………………..33 参考文献……………………………………………………………….....34

第一章 VHDL语言的概述

§1.1 VHDL语言的概述

目前最通用的硬件描述语言有VHDL与Verilog两种。1982年美国国防部的一个分支专案要求所有的数字电路必须用VHDL语言设计。1983年的IBM及TI等公司在此专案规定下,开始开发VHDL。随后IEEE也认可此成果为1076号IEEE标准,1993年又修订为93年版的IEEE1076号标准。后来将一种可配合集成工具的VHDL程序包,特别命名为IEEE1076.3,并成为1076号IEEE标准的一部分。最近,新标准包IEEE1076.4被开发出来,成为建立ASIC 及FPGA的模型函数库。VHDL的设计层面可以划分为系统层、算法层、寄存器传输层、逻辑层以及电路层。另一支HDL语言的主流是Verilog,其建模能力可以涵盖所有范围。

§1.2 语言特性

VHDL主要用于描述数字系统的结构、行为、功能和接口。除了含有许多硬件特征的语句外,VHDL的语言形式和描述风格与句法是十分类似于一般的计算机高级语言。VHDL的程序结构特点是将一项工程设计,或称设计实体(可以是一个元件,一个电路模块或一个系统)分成外部(或称可视部分及端口)和内部(或称不可视部分),既涉及实体的内部功能和算法完成部分。在对一个设计实体定义了外部界面后,一旦其内部开发完成后,其他的设计就可以直接调用这个实体。这种将设计实体分成内外部分的概念是VHDL系统设计的基本点。应用VHDL进行工程设计的优点是多方面的。

VHDL语言可描述一个数字电路的输入、输出以及相互间的行为与功能。而其硬件关联性的语法与形式虽类似于一般程序语言,但是涵盖许多与硬件关联的语法构造。其特有的层次性——由上而下的结构式语法结构适合大型设计项目的分包下去,各自运行。从抽象的层次而言,VHDL的语句分成以下4个大类。

1 行为式

采用语言逻辑方式直接描述硬件电路的工作,表示一个设计的功能或算法,描述IC内部电路行为。在此结构定义中可以同时包含并行描述与顺序语句。

2 数据流

从数据输入与输出的观点,大部分的并行语句都用于数据转换工作。

3 结构式

允许设计者以树状形式调用内置电路组件。通常以引脚图方式调用并连接。从硬件的角度说,调用组件就像在组合与连接电路元器件一样。

4 寄存器传输式

VHDL是一种类型化的语言,一种数据类型的数据内容不能指定给其他类型的数据,而且不同数据类型的数据需经过转换才能相互运算。每一种电路的VHDL码都是实体与结构的成对组合,先用实体来定义一个IC电路引脚规格及基本参数,然后在用结构定义IC内部电路的功能运做,即构成一个完整的电路模块。

第二章 EPM7128S芯片简介

本设计采用EPM7128S芯片,下面将对其进行简单介绍。 ALTERA的EPM7128S系列CPLD是基于第二代MAX结构体系地高性能EEPROM结构的CPLD。完全符合IEEE 1149.1 JTAG边界扫描标准,具有5V ISP的功能。具有最小5ns的引脚到引脚的逻辑时延,最高可175.4MHz的计数频率。引脚可配置为开漏输出。每个宏单元都有的可编程电源控制,最多可以节省50%的功耗。宏单元内的寄存器具有单独的时钟和复位等信号。支持多种电压接口。

MAX7128S是一种复杂可编程逻辑器件,IC管脚参阅原理图2.1,是84pinPLCC封装,另外还有其它类型的管脚和封装,选择性强,该IC具有以下主要性能:

以第二代多阵列矩阵(MAX)结构为基础,是一种高性能CMOS EEPROM器件;

通过JTAG(Joint Text Action Group联合测试组)接口可实现在线编程(ISP),本实验板提供JTAG接口;

逻辑密度为2500个可用编程门电路,128个宏单元; 6可编程I/O口,TTL逻辑电平为5V或3V;

引脚到引脚的逻辑延时为5.0ns,计数器工作频率可达到151.5MHz;有集电极开路选择,可编程宏单元触发器,具有专有的清除(clear)、时钟(clock)、输出使能(OE)控制;

与不同电源电压的系统接口,VCCIO引脚用于输出缓冲器接到5V

电源时,输出电平与5V电源兼容,VCCIO引脚用于输出缓冲器接到3.3V电源时,输出电平与3.3V电源兼容,VCCINT用于内部电路和输入缓冲

器;

包括一个可编程的程序加密位,全面保护专利设计,防止程序被复制和读出。

在本设计中,电源接5V,系统输入/出端口信号接I/O引脚,CLK接1024KHZ频率,RESET接外部复位电路。

图2.1 EPM7128S芯片引脚图

第三章 MAX+plusII 概述

本系统采用MAX+plusII进行仿真。下面将对该软件进行简单概述。 ALTERA公司的MAX+plusII软件是易学、易用的可编程逻辑器件开发软件。其界面友好,集成化程度高。有如下功能:

支持的器件:

所支持的器件有EPF10K10,EPF10K10A,EPF10K20,EPF10K30A, 以及MAX700系列(含MAX7000A,MAX7000AE,MAX7000E,MAX7000S),EPM9320,EPM9320A,EPF8452A,EPF8282A,FLEX6000/A系列,MAX5000系列和ClassicTM系列。

设计输入:

常用的设计输入的方法有:通过MAX+plusII图形编译器,创建图 形设计文件(.gdf);通过MAX+plusII文本编辑器,使用AHDL语言,创建文本设计文件(.tdf);使用VHDL语言,创建文本设计文件(.vhd);使用Verilog HDL语言,创建文本设计文件(.v)。还可以通过MAX+plusII波形编辑器,创建波形设计文件(.wdf)等。

设计编译:

通过MAX+plusII编译器完成,可检查项目是否有错,并对项目进 行逻辑综合,然后配置到一个ALTERA器件中,同时产生报告文件、编程文件和用于时间仿真的输出文件。

设计验证:

通过MAX+plusII的定时分析器进行时序分析、功能仿真、时序仿 真和波形分析,生成一些标准文件为其他EDA工具使用。

器件编程和配置:

MAX+plusII软件对计算机的要求较低。操作系统:Windows95/98 或 Windows NT4.0,安装所占空间为80Mbytes。内存要求:可用内存48MB(物理内存及虚存和),其中物理内存至少16MB。

第四章 自动售货机

§4.1 自动售货机功能概述

本设计的自动售货机只销售传统型(cola)与减肥型 (diet) 两种罐装可乐,销售的价格均为15元。顾客可以由两个不同投币孔分别投入3个5元硬币或2个10元硬币。一次交易只能买一罐,且自动找零只要按下取消键就会马上无条件退币。本设计的硬件示范电路,用2个按键代替2个投币孔,以LED点亮的数量显示各币种的投入数量,再以4HZ闪烁的LED来显示退币种类和数量。因为投币是计次电路,应先经过抖动消除电路处理。本设计采用1024HZ的系统时钟信号来控制所有买卖行为。该电路为一台自动售货机的控制核心,可以直接用来驱动机械装置。

§4.2 自动售货机外观

自动售货机外观示意图如下:

4.3 统设计说明

本系统的VHDL程序设计结构如下图:

§

§4.3.1 entity 定义

此处,我们定义了系统的输入/出端口信号,考虑顾客只有4种操作行为,即投币、选择、确定与取消,所以定义以下系统输入信号。

Reset: 系统内部设置给其他顾客重新操作的复位信号; Clk: 由外接信号发生器提供1024b/s的系统时钟信号; Ok_buy: 购买确认的按键信号; Cancle: 购买取消的按键信号; Coin_5: 投入5元硬币的动作按键; Coin_10: 投入10元硬币的动作按键; Select_cola: 选择传统可乐的按键信号; Select_diet: 选择减肥可乐的按键信号;

另外,系统必须响应顾客的各种操作行为,以利于顾客的选择判断,因此定义下列系统输出信号。

Led_cola_ok: 灯亮显示还有传统可乐; Led_diet_ok: 灯亮显示还有减肥可乐;

Led_cola_sel: 灯亮显示传统可乐选择按键被按;与Led_diet-_sel 只有一个灯亮,后按着优先;

Led_diet_sel: 灯亮显示传统可乐选择按键被按;与Led_cola-_sel 只有一个灯亮,后按着优先;

Led_buy: 灯亮显示按了购买确认键; Led_cancle: 灯亮显示按了购买取消键;

Led_five(1 to 3): 3个LED,投入1个5元硬币亮一个LED; Led_ten(1 to 2): 2个LED,投入1个10元硬币亮一个LED; Led_five_return: 3个LED,以每秒4次的闪烁代表被退出的硬币;

Led_ten_return: 2个LED,以每秒4次的闪烁代表被退出的硬币;

Led_cola_out: 灯亮显示cola已出货; Led_diet_out: 灯亮显示diet已出货;

在Xilinx的FPGA Express设计环境中,对所有VHDL程序都加入下

列几行有关库及程序包的调用语句。 Library ieee;

Use ieee.std_logic_11.all; Use ieee.std_logic_arith.all; Use ieee.std_logic_unsigned.all;

另外,应将抖动消除电路debounce放在my_pkg的程序包中,故在主程序中应加上一行:

use work.my_pkg.all 下面列出entity模块的程序代码。

entity模块的VHDL码

----library and package declaraction library ieee;

use ieee.std_logic_11.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all;

use work.my_pkg.all;--package including ‗debounce‘ component ----input and output pins declaraction entity vendor is

port( reset: in std_logic;--power reset to another buying clk: in std_logic;--system clock 1khz

ok_buy: in std_logic;--button to confirm buying cancel_buy: in std_logic;--button to cancel buying coin_5: in std_logic;--button to throw coin $5 con_10: in std_logic;--button to throw coin $10 select_cola: in std_logic;--button to choose drink cola

select_diet: in std_logic;--button to choose drink diet led_cola_ok:out std_logic;--led for cola available

led_diet_ok: out std_logic;--led for deit available led_cola_sel: out std_logic;--led to show cola chosen led_diet_sel: out std_logic;--led to show diet chosen led_buy: out std_logic;--led to show buying confirmed led_cancle: out std_logic;--led for cancel

led_five: out std_logic_vector (2to0);--leds to show coin_five numbers led_ten: out std_logic_vector (1 to 0);--leds to show coin ten numbers led_five_return: out std_logic_vector (2to0);--leds to show coin_five returned led_ten_return: out std_logic_vector (1 to 0);--leds to show coin ten returned led_cola_out: out std_logic;--led for cola delivery led_diet_out: out std_logic;--led for diet delivery end;

§4.3.2 architecture 模块

此处,我们必须定义一些功能块之间整体共享的传递信号,以整合所有块的功能。这些信号将成为各个块外部输入或输出信号,诸如:

ok: 用来维持ok_buy的状态; cancel: 用来维持cancel状态; money_ok: 投入金额正确的指针; return_clk: 退币的闪烁警示信号;

cola_choice: 用来维持cola_selection的状态; diet_choice: 用来维持diet_selection的状态;

total_amount_five: 5元硬币的累计投入金额(最高值为15元);

total_amount_ten: 10元硬币的累计投入金额(最高值为20元); cola_out: cola 已经出货的信号; diet_out: diet 已经出货的信号; 下面列出architecture 模块的程序代码

architecture 模块的VHDL码

----define the signal_structure and_flow of the device architecture arch of vendor is

--global signal flowing among different circuit blocks signal ok:std_logic;--edged ok_buy signal cancel: std_logic;--edged cancel_buy

signal money_ok: std_logic;--signal for sufficient money signal return_clk: std_logic;--twinkling signal for coin returned signal cola_choice: std_logic;--to maintain the cola_selection status signal diet_choice: std_logic;--to maintain the diet_selection status signal total_amount-five: integer range 0 to 15; --total amount of coin five signal total_amount_ten: integer range 0 to 20; --total amount of coin ten signal cola_out:std_logic;--signal for cola delivery signal diet_out:std_logic;--signal for diet delivery begin

§4.3.3 产生退币闪烁信号的电路模块(return_clk)

为了提醒退币,设计输出4HZ闪烁信号给对应的LED。本模块的输入信号reset与clk均为系统输入信号,输出信号return_clk为内部传

递用的全局信号。本模块采用一个8位自动计数器,将1024HZ的系统时钟除以2^8=256,可以得到4HZ的闪烁信号。其中8位的计数信号为此模块的局部信号,该信号并不传递到模块外。 下面列出return_clk模块的程序代码。

return_clk模块的VHDL码

----to generate 4hz return_clk by dividing 1024hz clock by 256 return_clk : block --input:clk,reset --ouput: return_clk

signal count: std_logic_vector(7downto 0);--free count from 0 to 255 begin

process(reset,clk) begin

if reset=‘1‘ then count<=‖000000000‖; return_clk<=‘0‘; elsif rising_edge(clk) then count<=count+‖00000001‖;

if count(7)=‘1‘ then return_clk<=‘1‘; else return_clk<=‘0‘;

end if;

end if; end process; end block;

设定1KHZ的系统输入信号clk 以及reset,可以得到除以256之后

的4HZ(256ms)的整体共享信号return_clk,信号仿真结果如图4.1所示:

图4.1 退币闪烁信号电路仿真图

§4.3.4 投入10元硬币的处理电路模块(coin_10_counting)

本模块计算10元硬币的投入数量及累计金额,并点亮对应数量的LED 。模块输入信号有clk与coin_10两个系统输入信号,led_ten 为系统输出信号,输出total_amount_ten为全局信号。考虑到投币动作为计次行为,故将coin_ten信号进行抖动消除处理,以利于计数。no_coin_ten信号用来累计10元硬币的投入数量,这两个信号都是局部信号,不向外传递。下面列出coin_10_counting模块的程序代码。

coin_10_counting模块的VHDL

----to count the number and amount of coin 10 and light the leds

coin_10_counting:block --input: led_ten, total_amount_10 signal coin10:std_logic;

--cleared coin_10 signal to count push_button frequency signal mo_coin_ten:integer range 0 to 2;

--no.of thrown coin ten

begin

u1: debounce port map(clk>=clk,touch>=coin_10,push_out>=coin10); process(reset,coin10) begin

if reset = ‗1‘ then total_amount_ten<=0; no_coin_ten<=0; led_ten<=‖00‖;

elsif rising_dege(coin10) then –triggered by coin10 total_amount_ten<=total_amount_ten+10;

if no_coin_ten<2 then led_ten(no_coin_ten)<=‘1‘; no_coin_ten<=no_coin_ten+1; else no_coin_ten<=2; end if; end if; end process; end block;

设定clk ,reset,与coin_10系统输入信号后,可观察到系统输出信号led_ten以及全局信号total_amount_ten和no_coin_ten,如图4.2所示:

图4.2 10元硬币处理电路仿真图

§4.3.5 投入5元硬币的处理电路模块(coin_5_counting)

本模块计算5元硬币的投入数量及累计金额,并点亮对应数量的LED 。模块输入信号有clk与coin_5两个系统输入信号,led_five 为系统输出信号,输出total_amount_five为全局信号。考虑到投币动作为计次行为,故将coin_five信号进行抖动消除处理,以利于计数.no_coin_five信号用来累计5元硬币的投入数量,这两个信号都是局部信号,不向外传递。下面列出coin_5_counting模块的程序代码。

coin_5_counting模块的VHDL码

----to count the number and amount of coin 5 and light the leds coin_5_counting:block

--input: led_five, total_amount_5 signal coin5:std_logic;

--cleared coin_5 signal to count push_button frequency signal mo_coin_five:integer range 0 to 3;

--no.of thrown coin five

begin

u1:debounce port map(clk>=clk,touch>=coin_5,push_out>=coin5); process(reset,coin5) begin

if reset = ‗1‘ then total_amount_five<=0; no_coin_five<=0; led_five<=‖000‖; elsif rising_dege(coin5) then –triggered by coin5 total_amount_five<=total_amount_five+5;

if no_coin_five<3 then led_five(no_coin_five)<=‘1‘; no_coin_five<=no_coin_five+1; else no_coin_five<=3; end if; end if; end process; end block;

设定clk,reset与coin_5三个系统输入信号后,可观察到系统输出信号led_five以及全局信号total_amount_5与no_coin_five,3位led_five信号为“000”->“001” ->“011” ->“111”,3次投币点亮了3个LED,投入金额分别表示0,5元,10元和15元。仿真结果如图4.3所示:

图4.3 5元硬币处理电路仿真图

§4.3.6 饮料选择处理电路模块(selec_drink)

本模块处理饮料选择种类,并点亮对应种类的LED 。模块有clk,reset,select_cola与select_diet四个系统输入信号,led_cola_sel与led_diet_sel为系统输出信号,显示该饮料被选。输出cola_choice为全局信号,维持cola 与diet 被选取状态的指针,用做其他行为的判断依据。下面列出select_drink模块的程序代码。

select_drink模块的VHDL码

---- to select cola or diet select_drink:block

--input:clk,reset,select_cola,select_diet

--output: led_cola_sel,led_diet_sel,cola_choice,diet_choice begin

process(reset,clk) begin

if reset =‘1‘ then led_cola_sel<=‘0‘;

led_diet_sel<=‘0‘; elsif rising_edge(clk) then

if select_cola =‘1‘ then led_cola_sel<=‘1‘; cola_choice<=‘1‘; --to maintain the cola_selection status led_diet_sel<=‘0‘; --exclusive double choices end if;

if select_diet=‘1‘ then led_cola_sel<=‘0‘; --exclusive double choices

diet_chioce<=‘1‘; --to maintain the diet_selection status led_diet_sel<=‘1‘; end if; end if; end process; end block;

设定clk,reset,select_diet与select_cola四个系统输入信号后,可观察到系统输出信号led_cola_sel,led_diet_sel以及整体共享信号 cola_choice与diet_choice的波形。当按下select_cola键弹回时,让cola_choice维持1,作为选定cola的指针,同时led_cola_sel灯亮,表示cola被选中。但若随后按了select_diet 键,则以后一次的选择为准,所以不但让diet_choice及led_diet_sel维持1,而且将cola_choice与led_cola_sel还原为0。同理,下次的饮料选取按键总是可以取代前一次的选择。如图4.4所示。

图4.4 饮料选择处理电路仿真图

§4.3.7 确认与取消处理电路模块(ok_or_cancel)

按了购买确认键或取消键后,系统将据此处理交易与反应。但按键信号只是一个短脉冲,应该将其状态维持住,因此本模块需要定义并输出全局信号ok与cancel给其他模块。同时还需要输入系统信号reset,ok_buy与cancel_buy ,然后设定输出系统信号led_buy与led_cancel,用来对外显示购买或取消状态。下面列出ok_or_cancel模块的程序代码。

ok_or_cancel模块的VHDL代码

---- to check confirming or canceling buying ok_or_cancel:block

--input: reset,ok_buy,cancel_buy --output: led_buy,ok,led_cancel,cancel

begin

p1: process(reset,ok_buy)--to maintain the confirming status begin

if reset =‘1‘ then ok<=‘0‘; led_buy<=‘0‘; elsif rising_edge(ok_buy) then ok<=‘1‘; led_buy<=‘1‘; end if ; end process;

p2:process(reset,cancel_buy)--to maitain the canceling status begin

if reset<=‘1‘; then cancel<=‘0‘;

led_cancel<=‘0‘; elsif rising_edge(cancel_buy) then cancel<=‘1‘; led_cancel<=‘1‘; end if l end process; end block;

设定ok_buy,cancel_buy与reset三个系统输入信号,可观察到系统输出信号led_ok,led_cancel与输出全局信号ok与cancel的波形,ok与cancel信号用来维持ok_buy与 cancel_buy按键操作的状态。如图4.5所示:

图4.5 确认与取消处理电路仿真图

§4.3.8 退币处理电路模块(coin_returned)

本模块处理应退还的硬币种类与数量。模块输入有clk与reset 两个系统输入信号,led_five_return与led_ten_return为系统输出信号,显示

被退饮料种类对应的LED灯及数量。此模块需要输入全局信号total_amount_five与total_amount_ten来计算总投入金额total_amount。此信号total_amount为全局信号,仅供此处判断而不做输出,若投入金额超过15元,则必须输出全局信号money_ok,以便其他行为的判别。如果按了cancel_buy键,则系统必须立即退还所有硬币,所以还需要定义全局信号cancel用做判断。定义全局信号return_clk控制退币警示灯。在正常的情况下,只有在输入全局信号cola_out或diet_out出货信号为1时,系统才开始判断应该退何种硬币及数量。下面列出coin_rerurned模块的程序代码。

coin_rerurned模块的VHDL码

----to check total amount and decide returned coins and twinkle --the leds coin_returned : block

--input : clk,reset,total_amount_ten,total_amount_five, --return_clk,cancel‘ --------cola_out,diet_out

--output : money_ok,led_five_return,led_ten_return, --cola_choice,diet_choice signal total_amount : integer range 0 to 35; begin

process(reset,clk) begin

if reset =‘1‘ then total_amount<=0; money_ok<=‘0‘;

led_five_return<= (others =>‘0‘); led_ten_return<= (others =>‘0‘);

elsif rising_edge(clk) then

total_amount<=total_amount_ten+total_amount_five; if total_amount>=15 then money_ok<=‘1‘; else money_ok <=‘0‘; end if;

if (cancel=‘1‘) then

--if canceled,return all coins mmediately for i in 0 to 2 loop

led_five_return(i)<=return_clk; end loop;

for i in 0 to 1 loop

led_ten_return(i)<=return_clk; end loop;

elsif (diet_out =‘1‘ or cola_out =‘1‘) then --return coins after drink delivered case total_amount is

when 0 to 14 => for i in 0 to 2 loop led_five_return(i)<=return_clk;

end loop;

for i in 0 to 1 loop led_ten_return(i)<=return_clk;

end loop;

when15 => null;

when20=> led_five_return(2)<=return_clk;

when25=> led_ten_return(0)<=return_clk;

when30=> led_ten_return(1)<=return_clk; led_five_return(1)<=return_clk;

when others=> led_ten_return(0)<=return_clk;

led_ten_return(1)<=return_clk

end case ;

end if;

end process; end block;

设定clk与reset 等系统输入信号,以及输入全局信号cancel,total_amount_five,return_clk,cola_out与diet_ot信号后,可以观察到系统输出信号led_ten_return,led_five_return与输出全局信号money_ok的波形。另外,局部信号total_amount仅用来暂存投入硬币的总金额,并不输出到模块外,正常交易时的系统反应。投入20元,选择diet 并按确认键,则不但出货灯led_diet_out亮,而且也退出一枚5元硬币(led_five_return0 =‘1‘)。由于cola_out 与diet_out信号无法提出,故用led_cola_out 及led_diet_out 替代。如果操作期间按了取消键,则退币灯全亮,并立即退还所有硬币,信号仿真结果如图4.6,图4.7所示:

图4.6 退币处理电路仿真图

图4.7 找零处理电路仿真图

§4.3.9 出货并计算存货电路模块 (give_check)

当输入全局信号ok,money_ok 与cola_choice/diet_choice 均为1时,则确定输出全局信号cola_out/diet_out 表示cola/diet 可出货,并点亮系统输出信号 led_cola_out/led_diet_out。对于cola与diet 两种饮料的存货,用模块内部局部信号no_cola与 no_diet 来记录 cola 与diet 的存货数量,只要还有存货,则系统输出信号led_cola_ok与led_diet_ok便保持灯亮。系统输入信号reset为所有模块的重置信号。下面列出give_check模块的程序代码。

give_check模块的VHDL码

----to deliver the selected drink and book the remaining number

give_check:block

--input : money_ok,ok,cola_choice,diet_choice,reset --output

cola_out,led_cola_out,diet_out,led_diet_out,led_cola_ok,led_diet_ok signal no_cola : integer range 0 to 20; --bottle on.of remaining cola

signal no_diet : integer range 0 to 20; --bottle no.of remaining diet begin

cola_out <=‘1‘ when (money_ok =‘1‘ and cola_choice =‘1‘)

else ‗0‘; -- to deliver cola if necessary

led_cola_out <=cola_out;

diet_out <=‘1‘ when (money_ok =‘1‘ and diet_choice =‘1‘)

else ‗0‘; -- to deliver diet if necessary

led_diet_out <=diet_out;

cola: process (reset,cola_out) –to book the bottle no.of remaining cola begin

if reset =‘1‘ then no_cola<=20;

led_cola_ok<=‘1‘;

elsif rising_edge (cola_out) then no_cola<=no_cola-1;

if no_cola=0 then led_cola_ok <=‘0‘; --to show cola empty status

else led_cola_ok <=‘1‘; end if; end if; end if ;

:

end process;

diet: process (reset,diet_out) –to book the bottle no.of remaining diet

begin

if reset =‘1‘ then no_diet<=20;

led_diet_ok<=‘1‘;

elsif rising_edge (diet_out) then no_diet<=no_diet-1;

if no_diet=0 then led_diet_ok <=‘0‘; --to show diet empty status

else led_diet_ok <=‘1‘; end if; end if; end if ; end process;

reset(重置信号)为本模块的唯一系统输入信号,输入全局信号money_ok,ok与 cola_choice/diet_choice 信号,可观察到系统输出信号led_cola_out,led_diet_out,led_cola_ok,led_diet_ok与输出全局信号cola_out/diet_out 的波形。另外,局部信号no_cola与no_diet 仅用来记录现存饮料数量,并不输出到模块外。每种饮料初始数量定为20罐,当存货量降为0时,则将供货灯led_cola_ok/led_diet_ok熄灭(系统输出信号)。信号仿真结果如图4-8所示:

图4.8 出货并计算存货电路仿真图

§4.4 debouncing 电路模块

抖动消除电路的程序debouncing被放在my_pkg的程序包中,当作组件调用,在连接合成时,必须同时附挂在cola主程序下才能正常运行。下面列出deboune模块的程序代码。

deboune模块的VHDL码

----content definition of component ―debounce‖ library ieee;

use ieee.std_logic_11.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all;

----i/o signal declaraction

entity debounce is

port (clk: in std_logicl; --basic clock input 1024hz

touch: std_logic ; --push button signal about 100ms period push_out: out std_logic ); --cleared push botton signal --end debounce; ----definition of signal_flow in entity debounce

我们假定一般按键信号的主脉冲宽度为100ms左右,而机械按键抖动噪声宽度不大于30ms,为了计算真正的按键次数,必须过滤机械抖动噪声。因此首先由1024HZ的时钟信号制作出周期为31.25ms的取样信号。下面列出sampling_signal模块的程序代码。

sampling_signal模块的VHDL码

architecture arch of debounce is signal sample : std_logic; --declaraction of self_built sampling pusle from clock begin

----32.768ms sample signal generation sampling_signal : block --input : clk --output : sample

signal delay : std_logic ; --one_step delay

signal q : std_logic_vector (4 downto 0) ; --osc/2^5=32hz (31.25ms) begin process (clk)

begin

if rising_edge ( clk ) then

delay <=q(4);

q<=q+1; end ifl end process;

sample <=q(4) and not delay ; --leading_edge differentiating end block;

设定clk信号的振荡频率为1024HZ,定义输出全局信号sample,并在block内定义一个局部信号q 为5为计数器,作为除2^5=32的分频器,另外,在定义一个局部信号delay,用来暂存计数器的最高位q(4)。因此delay信号与q(4)信号之间会延迟一个周期(1ms)。我们要的不是q(4)除32的对称时钟信号,而是每除32一次时的单一脉冲信号sample ,故利用信号delay与q(4)之间的时间延迟,用简单的组合逻辑电路求的所要的sample信号。仿真结果如图4.9所示:

图4.9 采样电路仿真图

cleared_push 模块的VHDL码

----cleared push botton signal generation

cleared_push : block --input : clk, sample, touch --output : push_out

signal dff0, dff1 : std_logic ; --two_step delay ff signal push : std_logic ; --output of rsff for push signal begin process (clk ) begin

if rising_edge (clk) then

if sample = ‗1‘ then dff1<=dff0; dff0<=touch; ----push=rsff (dff0, dff1) to get twice sample detection

push<=( ( dff0 and dff1) or push ) and ( dff0 or dff1 ); end if ; end if ; end process; push_out<=push; end block; end arch;

只有经过两次sample 取样才视为真正的按键信号,则任何按键信号必须持续62.5ms 以上,才视为一次完整的按键,而小于62.5ms的噪声则被忽略。当clk上升沿触发且信号sample 为1时,将按键信号touch传给dff0,信号dff0的变化比sample 延迟一个clk周期(1ms) 。此时虽然也将信号dff0传给dff1,但传的是dff0前一次的值。这次信号dff0的值必须在下一次sample为1时,才能传给dff1。换言之,信号dff1比dff0落后一个sample周期。因此,dff0与dff1可视为两次取样的按键信号,我们认为,当dff0与dff1同时为1时,输出按键信号push才为1;而当

dff0与dff1同时为0时,输出按键信号push才为0。RF触发器的功能可写成交换函数q=(s or q) and (not r).当s=1时输出q=1;当r=1时,输出q=0。因此只有经过连续两次sample ,才认定为真正的按键的条件,令s= dff0 and dff1, r =( not dff0)and (not dff1)=not (dff0 or dff1),所以,程序中取push 为RS触发器的输出。统计的按键输出虽然滞后真正按键92.5ms,但可得到正确的按键次数。仿真结果如图4.10所示:

图4.10 去抖电路仿真图

结 束 语

此次毕业设计全面锻炼了我自己的各方面的能力。虽然只有三个多

月的时间,但是学到了许多在书本中学不到的知识,使我的综合能力有了很大的提高。只要归结为以下几点:

1.掌握好规范的软件设计方法,对于VHDL语言,硬件资源要求,

工作原理须明确清晰,否则受到指令功能,数目的,其间有些程序并不是一次编写完成的,在相互衔接组合中,经过多次反复提高。

2.掌握电子工程中常用的设计软件,如用开发软件完成编译、仿真工作。

3.充分锻炼了自己思考问题的能力。实际工作中,许多问题具有很大的自由性,在众多的方案面前,必须通过权衡利弊之后才能确定。

4.深切体会到面临知识不断更新的条件下,青年学生急需开拓视野,扩大比较系统,建立的思维框架。

致 谢

在本文脱稿之即,首先我要感谢冯涛老师,她严谨的治学态度和深入浅出的指导使我收益匪浅,不断鼓励和启发我在创新性和实用性上展开科研工作。本文从选题到论文工作的开展直至最后的完成,都是和她的悉心教导分不开的。特别感谢杨光老师在论文工作中给予的极大支持和指导。

参 考 文 献

[1] 薛宏熙,边计年,苏明。数字系统设计自动化。北京:清华大学出版社,1996 (103—106)

[2] 王小军,VHDL简明教程,北京:清华大学出版社,1997 (65—) [3] 侯博亨,顾新,VHDL硬件描述语言与数字逻辑电路设计修订版。西安:西安电子科技大学出版社,1999 (78—83)

[4] 朱明程,孙谱,可编程逻辑系统的VHDL设计技术。南京:东南大学出版社,1998 (109—111)

[5] 潘松,王国东,VHDL实用教程。电子科技大学出版社,2000 (167—170) [6] 黄正瑾,在系统编程技术及其应用。东南大学出版社,1997 (147—150) [7] 薛宏熙,边计年,用VHDL设计电子线路。清华大学出版社,2000 (125—130)

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务