本文为大家介绍电子计时器的vhdl设计方法。
设计要求
设计一个电子计时器,给定时钟信号为512hz,要求系统达到以下功能:
(1)用6个数码管分别显示时、分、秒,计时范围为00:00:00~23:59:59。
(2)计时精度是1s。
(3)具有启/ 停开关, 复位开关。
总体方框图
内部各功能模块
本系统由六十进制计数器模块、二十四进制计数器模块、分频模块执行计时功能, 输入信号是512hz,通过分频后为1hz,时钟信号是1hz作为计时器的秒输入,秒为60进制计数器,分也为60进制计数器,小时采用二十四进制计数器, 各级进位作为高位的使能控制。
六十进制计数器模块
设计一个八位的六十进制计数器模块,输入信号为en、reset、clk,分别为使能、复位和时钟信号,输出信号为qa[3„0]、qb[3„0]、rco,分别为低4位输出、高4位输出和进位位。
六十进制计数器
示波形分析
秒计数器的仿真波形图
利用60进制计数器完成00到59的循环计数功能,当秒计数至59时,再来一个时钟脉冲则产生进位输出,即enmin=1;reset作为复位信号低电平有效,即高电平时正常循环计数,低电平清零。因为这种60进制的vhdl语言是很好写的,它并不复杂,再说我们必须要学会这些基本的硬件语言的描写。
分钟计数器的仿真波形图
vhdl源程序
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity count60 is
port( en,reset,clk: in std_logic;
qa: out std_logic_vector(3 downto 0);
qb: out std_logic_vector(3 downto 0);
rco: out std_logic); end count60;
architecture a of count60 is
begin
process(clk)
variable tma: std_logic_vector(3 downto 0);
variable tmb: std_logic_vector(3 downto 0); begin
if reset =‘0’then tma:=“0000”;
tmb:=“0000”;
elsif clk‘event and clk=’1‘ then
if en=’1‘ then
rco<=tmb(2)and tmb(0)and tma(3)and tma(0);
if tma=“1001” then
tma:=“0000”;
if tmb=“0101” then
tmb:=“0000”;
else tmb:=tmb+1;
end if;
else tma:=tma+1;
end if;
end if;
end if;
qa<=tma;qb<=tmb; end process; end a;
二十四进制计数器模块
设计一个八位的二十四进制计数器模块,输入信号为en、reset、clk,分别为使能、复位和时钟信号,输出信号为qa[3„0]、qb[3„0],分别为低4位输出、高4位输出。
二十四进制计数器示意图
波形分析
小时计数器的仿真波形图
vhdl源程序
小时计数模块利用24进制计数器,通过分钟的进位信号的输入可实现从00到23的循环计数。
该模块部分vhdl 源程序如下:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity count24 is
port( en,reset,clk: in std_logic;
qa: out std_logic_vector(3 downto 0);
qb: out std_logic_vector(3 downto 0));
end count24;
architecture a1 of count24 is
begin
process(clk)
variable tma: std_logic_vector(3 downto 0);
variable tmb: std_logic_vector(3 downto 0);
begin
if reset = ‘0’then tma:=“0000”;
tmb:=“0000”; else
if clk‘event and clk=’1‘ then
if en=’1‘ then
if tma=“1001” then
tma:=“0000”;
tmb:=tmb+1;
elsif tmb=“0010” and tma=“0011” then
tma:=“0000”;
tmb:=“0000”;
else tma:=tma+1;
end if;
end if;
end if;
end if;
qa<=tma;
qb<=tmb;
end process;
end a1;
分频器模块
设计一个分频器,要求将输入512hz的时钟信号分频为1hz的时钟信号作为计时器的秒输入。输入信号为clk和rst,分别为时钟信号和复位信号,输出信号为clk_out,为分频器1hz的时钟信号输出。
分频器示意图
vhdl 源程序
该模块部分vhdl 源程序如下:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity fenpinqi is
port (clk,rst:in std_logic;
clk_out:out std_logic);
end fenpinqi;
architecture behav of fenpinqi is
signal clk_data:std_logic;
signal cnt6 :
integer := 0;
begin
process(clk)
begin
if rst = ‘0’ then cnt6<=0
elsif clk‘event and clk=’1‘ then
if cnt6=255 then
clk_data<=not clk_data;
cnt6<=0;
else cnt6<=cnt6+1;
end if;
end if;
clk_out<=clk_data;
end process;
end behav;
led显示模块
led有着显示亮度高,响应速度快的特点,最常用的是七段式led显示器,又称数码管。七段led显示器内部由七个条形发光二极管和一个小圆点发光二极管组成,根据各管的亮暗组合成字符。
led数码管的g~a七个发光二极管因加正电压而发亮,因加零电压而不能发亮,不同亮暗的组合就能形成不同的字形,这种组合称之为字形码(段码),如显示”0”,字形码为3fh。
led数码管结构图
数码管的接口有静态接口和动态接口。动态接口采用各数码管循环轮流显示的方法,当循环显示频率较高时,利用人眼的暂留特性,看不出闪烁显示现象,这种显示需要一个接口完成字形码的输出(字形选择),另一接口完成各数码管的轮流点亮(数位选择)。
将二十四进制计数器和2个六十进制计数器的输出作为led显示模块的输入,在时钟信号的控制下通过此模块完成6个led数码管的显示,输出信号为wei[2…0]和led[6…0],分别为位选信号和段码输出。
led显示示意图
vhdl 源程序
该模块部分vhdl 源程序如下:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity clock1 is
port(clk: in std_logic;
s1, s2, s3, s4, s5, s6: in std_logic_vector(3 downto 0);
wei: out std_logic_vector(2 downto 0);
led: out std_logic_vector(6 downto 0));
end entity;
architecture behave of clock1 is
signal cnt6 : integer range 0 to 5 := 0;
signal shuju: std_logic_vector(3 downto 0);
begin
pro1:process(clk)
begin
if clk‘event and clk = ’1‘ then
cnt6 wei <= “000”; shuju wei <= “001”; shuju wei <= “010”; shuju wei <= “011”; shuju wei <= “100”; shuju wei <= “101”; shuju <= s6;
cnt6 null;
end case;
end if;
end process;
pro2: process(shuju)
begin
case shuju is
when “0000” => led led led led led led led led led led led<= “0000000”
end case;
end process;
end
顶层系统联调
通过上面的分频器,两个60进制的计数器,一个12/24进制的计数器,6选1扫描器,7段数码显示器,设计如图所示的顶层。规定每一模块的功能和各模块之间的接口。同时整个计数器有清零。 设计思想,利用脉冲时钟产生一个1hz的信号来实现一秒钟的控制,要产生1hz的信号就要用到分频器,实验中用512分频器把512hz的信号变成1hz。然后信号进入控制秒的计数器,当第60个脉冲时钟到来时,产生一个进位信号, 送到控制分的计数器,同理,当第60个脉冲时钟到来时,产生一个进位信号,送到控制小时的计数器。当小时计数器计数到12/24时,完成一个周期,跳转到零。输出是由动态扫描器来完成的。扫描器时钟取至前面分频未结束时的一个512hz的信号。这样就能够在7段数码显示管上,以512hz的频率扫描显示出时钟的数字变化。
通过元件例化将各个模块连接起来,组成一个整体。
元件例化就是将预先设计好的设计实体定义为一个元件,然后利用特定的语句将此元件与当前的设计实体中的指定端口相连接,从而为当前设计实体引入一个新的低一级的设计层次。所定义的例化元件相当于一个要插在这个电路系统板上的芯片,而当前设计实体中指定的端口则相当于这块电路板上准备接受此芯片的一个插座。
vhdl 源程序
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_signed.all;
use ieee.std_logic_arith.all;
entity dzjsq is
port(en,clk,reset:in std_logic;
wei:out std_logic_vector(2 downto 0);
led:out std_logic_vector(7 downto 0));
end entity dzjsq;
architecture abc of dzjsq is
component count60
port( en,reset,clk: in std_logic;
qa: out std_logic_vector(3 downto 0);
qb: out std_logic_vector(3 downto 0);
rco: out std_logic);
end component;
component count24
port( en,reset,clk: in std_logic;
qa: out std_logic_vector(3 downto 0);
qb: out std_logic_vector(3 downto 0));
end component; component fenpinqi
port (clk,rst:in std_logic; clk_out:out std_logic);
end component; component clock1
port(clk: in std_logic;
s1, s2, s3, s4, s5, s6: in std_logic_vector(3 downto 0);
wei: out std_logic_vector(2 downto 0);
led: out std_logic_vector(7 downto 0));
end component;
signal a1,a2,a3,a4,a5,a6:std_logic_vector(3 downto 0);
signal b1,b2,b3: std_logic; begin
u1: fenpinqi port map(clk,reset,b1);
u2:count60 port map(en,reset,b1,a1,a2,b2);
u3:count60 port map(en,reset,b2,a3,a4,b3);
u4:count24 port map(en,reset,b3,a5,a6);
u5:clock1 port map(clk,a1,a2,a3,a4,a5,a6,wei,led);
end architecture abc;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity count60 is
port( en,reset,clk: in std_logic;
qa: out std_logic_vector(3 downto 0);
qb: out std_logic_vector(3 downto 0);
rco: out std_logic);
end count60;
architecture a of count60 is
begin
process(clk)
variable tma: std_logic_vector(3 downto 0);
variable tmb: std_logic_vector(3 downto 0);
begin
if reset =‘0’then tma:=“0000”;
tmb:=“0000”;
elsif clk‘event and clk=’1‘ then
if en=’1‘ then
rco<=tmb(2)and tmb(0)and tma(3)and tma(0);
if tma=“1001” then
tma:=“0000”;
if tmb=“0101” then
tmb:=“0000”;
else tmb:=tmb+1;
end if;
else tma:=tma+1;
end if;
end if;
end if;
qa<=tma;
qb<=tmb;
end process;
end a;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity count24 is
port( en,reset,clk: in std_logic;
qa: out std_logic_vector(3 downto 0);
qb: out std_logic_vector(3 downto 0));
end count24;
architecture a1 of count24 is
begin
process(clk)
variable tma: std_logic_vector(3 downto 0);
variable tmb: std_logic_vector(3 downto 0);
begin
if reset = ‘0’then tma:=“0000”;
tmb:=“0000”;
else if clk‘event and clk=’1‘ then
if en=’1‘ then
if tma=“1001” then
tma:=“0000”;
tmb:=tmb+1;
elsif tmb=“0010” and tma=“0011” then
tma:=“0000”;
tmb:=“0000”;
else tma:=tma+1;
end if;
end if;
end if;
end if;
qa<=tma;
qb<=tmb;
end process;
end a1; library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity fenpinqi is
port (clk,rst:in std_logic;
clk_out:out std_logic);
end fenpinqi;
architecture behav of fenpinqi is signal clk_data:std_logic;
signal cnt6 : integer := 0;
begin
process(clk) begin
if rst = ‘0’ then
cnt6<=0
elsif clk‘event and clk=’1‘ then
if cnt6=512 then
clk_data<=not clk_data;
cnt6<=0;
else cnt6<=cnt6+1;
end if;
end if;
clk_out<=clk_data;
end process;
end behav;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity clock1 is
port(clk: in std_logic;
s1, s2, s3, s4, s5, s6: in std_logic_vector(3 downto 0); wei: out std_logic_vector(2 downto 0);
led: out std_logic_vector(7 downto 0));
end entity;
architecture behave of clock1 is signal cnt6 : integer range 0 to 5 := 0;
signal shuju: std_logic_vector(3 downto 0);
begin pro1:process(clk)
begin
if clk‘event and clk = ’1‘ then
cnt6 wei <= “000”;
shuju wei <= “001”;
shuju wei <= “010”;
shuju wei <= “011”;
shuju wei <= “100”;
shuju wei <= “101”;
shuju <= s6;
cnt6 null;
end case;
end if;
end process;
pro2: process(shuju)
begin case shuju is when “0000” => led led led led led led led led led led led<= x“00”
end case;
end process;
end
电子计时器的功能仿真结果
口罩模拟穿戴试验机的技术参数是怎样的
机器人Iroi和Patrovor
小米6最新消息:小米6陶瓷版明天首卖,雷军不耍猴,小米6全款预定不用抢啦!
采埃孚的两座工厂面临关厂危机
为工业 4.0 工厂的大规模定制、高质量和可持续运营提供支持
基于VHDL的电子计时器的设计方法详解
美国倍捷连接器东南亚区域总部办公室开幕庆典在新加坡隆重举行
dfrobot数字继电器模块简介
10月上市纯电动车型大盘点 看看有没有你喜欢的吧
数字隔离器原理及应用
降压型μModule稳压器LTM8053
R&S CMW WLAN和蓝牙芯片测试平台通过博通认证
华为表示5G不是原子弹不伤害人5G是用来造福于所有老百姓的
MiniLED将进入高成长期 聚积9月营收创下单月历史新高
虹科教您 | 虹科RELY-TSN-KIT操作指南(3)——基于Linux系统进行TSN协议测试
物联网技术在工业领域中的应用解析
在第三届中国电子信息博览会上杀出一匹黑马
当数字孪生技术为超级工厂建设按下“加速键”
闪烁灯实验
三星闪开!小米6再曝黑科技:使用LG G6同款超级LCD屏幕