资源描述
本报告共包含5个VHDL程序的设计:
VHDL程序1:计时器.
VHDL程序2:序列计数器.
VHDL程序3:脉冲宽度处理电路设计.
VHDL程序4:01011序列检测器.
VHDL程序5:赛跑计时秒表.
一 计时器
1. 设计任务和原理介绍:
假定输入时钟周期为1秒,我们根据这个时钟周期进行计数,设立了3个计数器,分别是秒计数器,分钟计数器,小时计数器。每次输入的时钟上升沿来临,直接驱动秒计数器。如果秒计数器值为59(二进制为111011),则秒计数器恢复为0,否则则秒计数器加1;在此情况下接着查看分钟计数器的值,如果此时分钟计数器值也为59(二进制为111011),则分钟计数器值恢复为0,否则分钟计数器加1;在秒计数器与分钟计数器都为59的情况下,还需查看小时计数器的值,如果此时小时计数器的值为23(二进制10111),则小时计数器的值恢复为0,否则小时计数器的值加1.
2.VHDL源程序:
Library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity counter is
port (
reset : in std_logic;
clk_sec : in std_logic;
seconds : out std_logic_vector (5 downto 0);
minutes : out std_logic_vector (5 downto 0);
hours : out std_logic_vector (4 downto 0)
);
end counter;
architecture behavior of counter is
signal count_sec : std_logic_vector (5 downto 0);
signal count_min : std_logic_vector (5 downto 0);
signal count_hour : std_logic_vector (4 downto 0);
begin
process (reset,clk_sec)
begin
if reset='0' then
count_sec (5 downto 0) <="000000";
count_min (5 downto 0) <="000000";
count_hour (4 downto 0) <="00000";
else
if clk_sec'event and clk_sec='1' then
if count_sec="111011" then
count_sec<="000000";
if count_min="111011" then
count_min<="000000";
if count_hour="10111" then
count_hour<="00000";
else
count_hour<=count_hour+1;
end if;
else
count_min<=count_min+1;
end if;
else
count_sec<=count_sec+1;
end if;
else
null;
end if;
end if;
end process;
seconds (5 downto 0) <=count_sec (5 downto 0);
minutes (5 downto 0) <=count_min (5 downto 0);
hours (4 downto 0) <=count_hour (4 downto 0);
end behavior;
3.程序主要部分介绍及流程图:
port (
reset : in std_logic;
clk_sec : in std_logic;
seconds : out std_logic_vector (5 downto 0);
minutes : out std_logic_vector (5 downto 0);
hours : out std_logic_vector (4 downto 0)
);
定义端口,从上往下分别为复位键,时钟信号输入端,秒输出端(0—59),分钟输出端(0—59),小时输出端(0—24).
signal count_sec : std_logic_vector (5 downto 0);
signal count_min : std_logic_vector (5 downto 0);
signal count_hour : std_logic_vector (4 downto 0);
定义内部信号变量,分别对应秒,分钟,小时信号。
其流程图如下:
4.系统模块图:
5.仿真图:
本图是系统在特殊的时间:0小时59分59秒时的仿真图。观察可以发现,下一个时钟沿到达后,秒信号由3B(十进制59)变为00并产生进位,分钟信号由3B变为00并产生进位,小时信号由00变为01,从而使此时的时间为1小时0分0秒。从而验证了本程序的正确性。
二 序列计数器.
1. 设计任务和原理介绍:
序列计数器是经常出现在通信协议编译码器中的器件。其基本功能是对一个8bit位宽的二进制数中的连续为0的个数进行统计。具体规则为:在单个时钟脉冲时间内,完成对8bit的二进制数的连0个数进行统计,并且要求在整个序列中只能有一串连0出现,即8bit中的0是相邻的,此时,我们认为输出有效,并且输出连0的个数,否则无效,并且连0计数器清0,同时输出错误指示信号。
这里规定全1的序列为有效序列,其连0的个数为0个。例如:“00000000”合法,其计数值为8;“11111111”合法,其计数值为0;“11100011”合法,其计数值为3;“11011001”不合法,计数值清零,并且输出错误指示。
2. VHDL源程序:
Library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity count_zero is
port (
data : in std_logic_vector (7 downto 0);
count : out integer range 0 to 8;
error : out boolean
);
end count_zero;
architecture behavior of count_zero is
begin
process (data)
variable seen_zero, seen_trailing : boolean;
variable temp_count : integer range 0 to 8;
begin
error<=false;
seen_zero:=false;
seen_trailing:=false;
temp_count:=0;
for i in 0 to 7 loop
if (seen_trailing and data(i)='0') then
temp_count:=0;
error<=true;
exit;
elsif (seen_zero and data(i)= '1') then
seen_trailing:=true;
elsif (data(i)= '0') then
seen_zero:=true;
temp_count:=temp_count+1;
end if;
end loop;
count<=temp_count;
end process;
end behavior;
3.程序主要部分介绍及流程图:
port (
data : in std_logic_vector (7 downto 0);
count : out integer range 0 to 8;
error : out boolean
);
定义端口,从上往下分别为输入数据端(并行8bit),计数输出端,错误输出端。
variable seen_zero, seen_trailing : boolean;
variable temp_count : integer range 0 to 8;
定义了3个变量:
temp_count:完成单个时钟周期内对连零的计数。
seen_zero:指示序列中已经有0出现。
seen_trailing:指示序列中连零的状态已经出现一次。
其流程图如下:
4.系统模块图:
5.仿真图:
仿真图输入data分别为FF,00,01,02,EE,观察图所得对于输入FFH,即二进制11111111,输出的0的个数count为0 ,error为0;对于输入00H,即二进制00000000,输出的0的个数count为8 ,error为0;对于输入01H,即二进制00000001,输出的0的个数count为7 ,error为0;对于输入EEH,即二进制11101110,输出的0的个数count为0 ,error为1。以上满足设计要求,验证了程序的正确性。
三 脉冲宽度处理电路设计
1.设计任务和原理介绍:
本设计利用VHDL实现一个按键脉冲宽度处理电路。假设按键的高电平脉冲宽度可能为10-100个时钟宽度,每次按键在按键松开(释放)时输出一个时钟周期的高电平脉冲。
本程序利用状态机实现。共分为三个状态:
S0:初始状态。当接收到input端为1时,表明高电平脉冲到来,转为S1状态;当接收到input端为0时,表明高电平脉冲未来,依然保持S0状态。
S1:高电平脉冲持续状态。接收到input端为0时表明高电平脉冲结束,将check输出一个周期的高电平;接收到input端为1时,表明高电平脉冲持续,依然维持S1状态。
其状态转移图如下:
2.VHDL源程序:
Library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity check_pulse is
port (clock,reset,input: in std_logic;
check: out std_logic );
end entity check_pulse;
architecture asm of check_pulse is
type state is (s0,s1);
signal present_state, next_state : state;
begin
seq: process (reset, clock) is
begin
if reset ='1' then
present_state <=s0;
elsif rising_edge(clock) then
present_state <=next_state;
end if;
end process seq;
com: process( present_state ,input) is
begin
check<='0';
case present_state is
when s0 =>
if input='1' then
next_state <=s1;
else
next_state <=s0;
end if;
when s1 =>
if input='0' then
next_state <=s0;
check<= '1';
else
next_state <=s1;
end if;
end case;
end process com;
end architecture asm;
3.程序主要部分介绍:
程序主要分为2个进程来完成:seq进程和com进程。
Seq进程可以作为状态机的一个通用进程。它负责处理reset及随着时钟的到达将次状态变化为现状态。
seq: process (reset, clock) is
begin
if reset ='1' then
present_state <=s0;
elsif rising_edge(clock) then
present_state <=next_state;
end if;
end process seq;
com进程则负责处理状态机的逻辑变化,其原理等同于前面所介绍的状态机图。
com: process( present_state ,input) is
begin
check<='0';
case present_state is
when s0 =>
if input='1' then
next_state <=s1;
else
next_state <=s0;
end if;
when s1 =>
if input='0' then
next_state <=s0;
check<= '1';
else
next_state <=s1;
end if;
end case;
end process com;
end architecture asm;
4.系统模块图:
5.仿真图:
本图为系统的仿真图。由观察可以发现,在input端高电平脉冲降为低电平时,check端就会输出一个时钟周期的高电平脉冲。以上满足设计要求,验证了程序的正确性。
四 01011序列检测器
1.设计任务和原理介绍:
本设计为帧同步检测电路,输入位宽1位的二进制序列及时钟,输出高电平脉冲的检测结果。对输入的二进制序列检测帧同步序列“01011”,即当输入的二进制序列中出现帧同步序列时,输出一个高电平脉冲。
本设计利用状态机完成,共分为5个状态。
S0:初始状态。此时接收到input端输入0,则转入状态S1,表明检测到序列“0”;接收到input端输入1,则转入状态S0。
S1:检测到序列“0”的状态。此时接收到input端输入1,则转入状态S2,表明检测到序列“01”;接收到input端输入0,则转入状态S1。
S2:检测到序列“01”的状态。此时接收到input端输入0,则转入状态S3,表明检测到序列“010”;接收到input端输入1,则转入状态S0。
S3:检测到序列“010”的状态。此时接收到input端输入1,则转入状态S4,表明检测到序列“0101”;接收到input端输入0,则转入状态S1。
S4:检测到序列“0101”的状态。此时接收到input端输入1,则将found端输出一周期的高电平,表明检测到序列“01011”;接收到input端输入0,则转入状态S1。
其状态转移图如下:
2.VHDL源程序:
Library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity check_01011 is
port (clock,reset,input: in std_logic;
found : out std_logic );
end entity check_01011;
architecture asm of check_01011 is
type state is (s0,s1,s2,s3,s4);
signal present_state, next_state : state;
begin
seq: process (reset, clock) is
begin
if reset ='1' then
present_state <=s0;
elsif rising_edge(clock) then
present_state <=next_state;
end if;
end process seq;
com: process( present_state ,input) is
begin
found <= '0';
case present_state is
when s0 =>
if input='0' then
next_state <=s1;
else
next_state <=s0;
end if;
when s1 =>
if input='1' then
next_state <=s2;
else
next_state <=s1;
end if;
when s2 =>
if input='0' then
next_state <=s3;
else
next_state <=s0;
end if;
when s3 =>
if input='1' then
next_state <=s4;
else
next_state <=s1;
end if;
when s4 =>
if input='1' then
found<='1';
next_state <=s0;
else
next_state <=s1;
end if;
end case;
end process com;
end architecture asm;
3.程序主要部分介绍:
程序主要分为2个进程来完成:seq进程和com进程。
Seq进程可以作为状态机的一个通用进程。它负责处理reset及随着时钟的到达将次状态变化为现状态。
seq: process (reset, clock) is
begin
if reset ='1' then
present_state <=s0;
elsif rising_edge(clock) then
present_state <=next_state;
end if;
end process seq;
com进程则负责处理状态机的逻辑变化,其原理等同于前面所介绍的状态机图。
4.系统模块图:
5.仿真图:
本图为系统的仿真图。由观察可以发现,当input端两次输入“01011”序列时,found端输出一个时钟周期的高电平,表明已检测到要求的序列。以上满足设计要求,验证了程序的正确性。
五 赛跑计时秒表
1.设计任务和原理介绍:
本设计实现一个可以对两个运动员赛跑计时的秒表。其细节如下:
⑴ 秒表的输入有时钟(clk),一个按键(key)和reset键,假设key已经经过防抖动和脉冲宽度处理,每按一次key产生持续一个时钟周期的高电平脉冲,不需要对key再做任何处理。
⑵ 秒表输出用0-59的整数表示,不需要对十位和个位分别计数,不需要7段译码。
⑶ 按第一下key,开始计数,并输出计数值。
⑷ 第一个运动员到终点时按第二下key,秒表记住第一个运动员到终点的时间,但还在继续计数并输出计数值。
⑸ 第二个运动员到终点时按第三下key,停止计数,这时输出的计数值就是第二个运动员用的时间。
⑹ 按第四下key,秒表输出第一个运动员到终点的时间,即按第二下key时记住的计数值。
⑺ 最后按reset键,系统恢复为初试状态。
本设计采用状态机来实现。其状态转移图如下:
共包含5个状态,现分述如下:
S0:初试状态。当key为1时,进入状态S1,启动秒表计时。
S1:按下第一次key键后的状态。当key为1时,进入状态S2,记录下带一个运动员的到达时间,秒表照常输出运行。
S2:按下第二次key键后的状态。当key为1时,进入状态S3,
停止计数,输出第二个运动员的到达时间。
S3:按下第三次key键后的状态。当key为1时,进入状态S4,
输出第一个运动员的到达时间。
S4: 按下第四次key键后的状态。当key为1时,返回到状态S0,并初试化。
2.VHDL源程序:
Library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity sec_clock is
port ( clk, key,reset : in std_logic;
seconds : out std_logic_vector (5 downto 0));
end entity sec_clock;
architecture asm of sec_clock is
type state is (s0,s1,s2,s3,s4);
signal q: std_logic_vector(5 downto 0);
signal arrive1: std_logic_vector(5 downto 0);
signal present_state, next_state : state;
signal count_begin:integer range 0 to 1;
signal is_arrive1:integer range 0 to 1;
begin
seq: process (reset, clk) is
begin
if reset ='1' then
present_state <=s0;
elsif rising_edge(clk) then
present_state <=next_state;
end if;
end process seq;
clock_count:process (clk,reset,is_arrive1) is
begin
if reset='1' then
q <="000000";
else if rising_edge(clk) then
if count_begin=1 then
if q = "111011" then
q <="000000";
else
q<=q+ 1;
end if;
end if;
end if;
end if;
end process clock_count;
com:process(present_state,key,reset)
begin
if reset='1'then
arrive1 <="000000";
count_begin<=0;
is_arrive1<=0;
end if;
case present_state is
when s0 =>
if key='1' then
next_state <=s1;
count_begin<=1;
else
next_state <=s0;
end if;
when s1 =>
if key='1' then
arrive1 (5 downto 0) <=q (5 downto 0);
next_state <=s2;
else
next_state <=s1;
end if;
when s2 =>
if key='1' then
count_begin<=0;
next_state <=s3;
else
next_state <=s2;
end if;
when s3 =>
if key='1' then
is_arrive1<=1;
next_state <=s4;
else
next_state <=s3;
end if;
when s4 =>
if key='1' then
next_state <=s0;
else
next_state <=s4;
end if;
end case;
end process com;
show:process(is_arrive1,clk)
begin
if is_arrive1=0 then
seconds (5 downto 0) <=q (5 downto 0);
else
seconds (5 downto 0) <= arrive1(5 downto 0);
end if;
end process show;
end architecture asm;
3.程序主要部分介绍:
程序主要由4个进程组成
seq进程可以作为状态机的一个通用进程。它负责处理reset及随着时钟的到达将次状态变化为现状态。
clock_count进程来实现秒表功能。
com进程来实现状态机的操作。
show进程用来将信号变量经处理送到seconds输出端。
4.系统模块图:
5.仿真图:
由观察可以发现,此满足设计要求,验证了程序的正确性。
- 24 -
展开阅读全文