交通灯控制器
一、设计任务要求
1、控制器按东西、南北两个方向控制两组交通灯(红、绿、黄)
2、两组灯亮的顺序满足交通安全的规则要求.
3、东西向绿灯每次亮30S,接着黄灯亮2S,红灯亮20S;南北向绿灯每次亮28S,接着黄灯亮2S,红灯亮30S.
4、有两组数码管给出灯亮的时间倒计时显示。
二、设计思路及总体结构框图
设计思路:
1.硬件:由设计任务要求可知,总体输入电路有:
(1)在开始计时之前的等待状态,复位键reset接低电位,接通电源后,首先要将它接高电位,表示计时开始。
(2)当按一下(on_off)键,表示紧急情况发生,两个方向均为红灯亮,计时停止,当再次按下(on_off)键时,控制器恢复原来状态,正常工作。
输出电路:
(1)由于东西和南北方向都要显示时间,因此需要4个数码管,这样在设计中就需要四条输出线choose4,用来选通指定一个LED七段显示数码管。
(2)显示器的每一位都采用LED七段显示数码管进行显示,每一个LED七段显示数码管都要有七条输出线控制,一共使用4个七段数码管,故输出电路使用四个七位输出信号:showtime1,showtime2,showtime3,showtime4。
(3)东西和南北方向都有交通灯亮的情况,故输出电路中要有两个状态控制信号state1,state2分别控制东西和南北的灯,每个方向上有4个灯(增加了左、右转弯显示控制功能),所以state1,state2的类型应该是4位数组型的。
外部电路图如下:
clk 4/
stas
reset
on_off 4/
7/
7/
7/
7/
4/
2.软件:
(1)在VHDL设计描述中,采用自顶向下的设计思路,该思路,首先要描述顶层的接口,上面的描述已经规定了交通灯控制的输入输出信号:
输入信号:复位开关信号reset;
紧急情况控制信号on_off;
外部时钟信号clk。
输出信号:LED七段显示数码管的选通信号choose4(3 downto 0);
LED七段显示数码管的输出信号showtime1(6 downto 0),showntime2(6 downto 0),showtime3(6 downto 0),showtome4(6 downto 0);
交通灯状态控制信号state1(3 downto 0),state2(3 downto 0)。
(2)在自顶向下的VHDL设计描述中,通常把整个设计的系统划分为几个模块,然后采用结构描述方式对整个系统进行描述。根据实验设计的结构功能,来确定使用哪些模块以及这些模块之间的关系。
由于紧急情况控制信号是采用按键的输入方式,其产生时刻和持续时间的长短是随机不定的,且存在因开关簧片反弹引起的电平抖动现象,因此必须在每个开关后面安排一个消抖和同步化电路模块,以保证系统能捕捉到输入脉冲,故需要有防抖动的模块。
由于外部时钟信号clk的频率为1MHz,而实际需要的内部计时时钟频率为1Hz,提供给消抖同步电路的频率为50Hz(满足按键)和提供给产生选通信号电路的时钟频率为200Hz(满足视觉暂留效应)。
当正常计时开始后,需要进行定时计数操作,由于东西和南北两个方向上的时间显示器是由两个LED七段显示数码管组成的,因此需要产生两个2位的计时信息:2个十位信号,2个个位信号,这个定时计数操作可以由一个定时计数器来完成,又因为交通灯的状态变化是在计时为0的情况下才能进行的,因此需要一个计时电路来产生使能信号,因此定时计数的功能就是用来产生2个2位计时信息和使能信号。
另外还需要将时间显示出来,为了节省资源,我采用了循环点亮LED七段显示数码管的方法来显示计时输出。通过信号choose4(3 downto 0)来对4个LED七段显示数码管进行选择。
由于不能使用7448自动译码集成电路,故在LED七段显示数码管显示时间时,要把计时结果转换为七段码输出到相应的LED七段显示数码管上,因此还需要一个转换电路。
交通灯状态控制也需要一个电路,当有使能信号及无紧急情况下,交通灯状态不发生变化,有紧急情况时,两个方向上均为红灯亮,紧急情况消除后,回到原来状态,无使能信号时,交通灯状态不变。
通过上面的分析,不难得知可以把交通灯控制系统划分为6个模块:键输入模块,时钟分频模块,计时模块,选通模块,显示模块,控制模块
三、总体结构框图
No
yes
Yes
No
No
Yes
No
Yes Yes
No
No
Yes
Yes
No
No
Yes
Yes
No
Yes
六、仿真波形
状态变化仿真图:以下显示选通信号的仿真波形
七、源程序
去抖模块:--------keyin module-----
library ieee;
use ieee.std_logic_11.all;
entity keyin is
port(A,B :in std_logic;
C :out std_logic);
end keyin;
architecture keyin_arc of keyin is
component kand2
port(A,B :in std_logic;
C :out std_logic);
end component;
component kdf
port(A,B :in std_logic;
C,D :out std_logic);
end component;
component knand2
port(A,B :in std_logic;
C :out std_logic);
end component;
signal TMP1,TMP2,TMP3,TMP4,TMP5,TMP6:std_logic;
begin
u0: knand2 port map(A,TMP1,TMP2);
u1: knand2 port map(TMP2,TMP3,TMP1);
U2: kdf port map(TMP2,B,TMP4,TMP3);
U3: kdf port map(TMP4,B,TMP6,TMP5);
u4: kand2 port map(TMP4,TMP5,C);
end keyin_arc;
library ieee; ---two inputs and gate description
use ieee.std_logic_11.all;
entity kand2 is
port(A,B :in std_logic;
C :out std_logic);
end kand2;
architecture kand2_arc of kand2 is
begin
C<=A and B;
end kand2_arc;---end of two inputs and gate description
library ieee; ---two inputs and_not gate description
use ieee.std_logic_11.all;
entity knand2 is
port(A,B :in std_logic;
C :out std_logic);
end knand2;
architecture knand2_arc of knand2 is
begin
C<=not(A and B);
end knand2_arc;---end of two inputs and_not gate description
library ieee; ---D trigger description
use ieee.std_logic_11.all;
entity kdf is
port(A,B :in std_logic;
C,D :out std_logic);
end kdf;
architecture kdf_arc of kdf is
begin
process(B)
begin
if(B'event and B='1')then C<=A;D<=not A;
end if;
end process;
end kdf_arc;
控制模块:---------keep module---------
library ieee;
use ieee.std_logic_11.all;
entity keep is
port( a,b,c,g,j: in std_logic;
d,e : out std_logic_vector(3 downto 0);
f : out std_logic_vector(1 downto 0));
end keep;
architecture keep_arc of keep is
type state_type is (s0,s1,s2,s3,s4);
signal current_state:state_type;
signal new_state:state_type;
begin
newstate_logic:
process(current_state,b,a,g)
variable m:integer range 0 to 3;
begin
if(g='0')then new_state<=s1;--------通电后给交通灯状态赋初值
else case current_state is
when s1 =>m:=0;
if(b='1'and j='0'and a='0')then--当计数到20s且无紧急情况时,交通灯跳至下一状态
new_state<=s2;
elsif(a='1')then new_state<=s0;-有紧急情况时,交通灯变为紧急状态
else new_state<=s1;-------------计时未到的时候保持原状态不变
end if;
when s2 =>m:=1;
if(b='1'and j='1'and a='0')then new_state<=s3;-当记数到5s且无紧急情况时,交通灯变为下一状态
elsif(a='1')then new_state<=s0;---------有紧急情况时,交通灯变为紧急状态
else new_state<=s2; --------------------计时未到的时候保持原状态不变
end if;
when s3 =>m:=2;
if(j='1'and b='0'and a='0')then new_state<=s4;-当记数到20s且无紧急情况时,交通灯变为下一状态
elsif(a='1')then new_state<=s0;----有紧急情况时,交通灯变为紧急状态
else new_state<=s3; ---------------计时未到的时候保持原状态不变
end if;
when s4 =>m:=3;
if(j='1'and b='1'and a='0')then new_state<=s1;--当记数到5s且无紧急情况时,交通灯变为下一状态
elsif(a='1')then new_state<=s0;-----有紧急情况时,交通灯变为紧急状态
else new_state<=s4; ----------------计时未到的时候保持原状态不变
end if;
when s0=>if(a='0')then---------------紧急情况消除后,回到原来状态
case m is when 0 =>new_state<=s1;
when 1 =>new_state<=s2;
when 2 =>new_state<=s3;
when 3 =>new_state<=s4;
end case;
else new_state<=s0;
end if;
end case;
end if;
end process;
state_register: process(c)
begin
if(c'event and c='1')then current_state<=new_state;--当时钟上升沿触发时,交通灯状态发生改变
end if;
end process;
output_logic:
process(current_state)---------------产生与交通灯状态相对应的提示信号,以控制计时模块的不同状态下的不同赋植
begin
case current_state is when s0 => d<="0100";e<="0100";
when s1 => d<="0100";e<="1001";f<="00";
when s2 => d<="0100";e<="0010";f<="01";
when s3 => d<="1001";e<="0100";f<="10";
when s4 => d<="0010";e<="0100";f<="11";
end case;
end process;
end keep_arc;
计时模块:-------time module-------
library ieee;
use ieee.std_logic_11.all;
use ieee.std_logic_unsigned.all;
entity time is
port(a,b,k:in std_logic;
c :in std_logic_vector(1 downto 0);
d,e,f,g :out std_logic_vector(3 downto 0);
j,h :out std_logic);
end time;
architecture time_arc of time is
signal l,m,n,p:std_logic_vector(3 downto 0);
begin
process(a,b,k)
begin
if(k='0')then l<="0101";m<="0010";n<="0000";p<="0010";-通电后赋初值
else if(a'event and a='1')then
if(b='1')then l<=l;m<=m;n<=n;p<=p;-----------------有紧急情况时,计时停止
else
if(n="0001" and p="0000")then case c is ----当东西方向倒计时到0时,重新赋值,并产生使能信号控制交通灯的状态
when "00" =>n<="0101";p<="0000";j<='1';
when "01"=>n<="0101";p<="0010";j<='1';
when "11"=>n<="0000";p<="0010";j<='1';
when "10"=>n<="0000";p<="0010";j<='1';
when others =>null;
end case;
else j<='0';-----------------------计时未到0时,输出使能信号为0
if(n="0000"and p/="0000")then n<="1001";p<=p-1;---当各位倒计时到0,而十位没到0时十位减一,个位赋9
else n<=n-1; ---------------------否则,各位减一
end if;
end if;
if(l="0001"and m="0000")then--当南北方向倒计时到0时,重新赋值,并产生使能信号控制交通灯的状态
case c is when "00"=>l<="0000";m<="0010";h<='1';
when "01"=>l<="0000";m<="0010";h<='1';
when "10"=>l<="0101";m<="0000";h<='1';
when "11"=>l<="0101";m<="0010";h<='1';
when others =>null;
end case;
else h<='0'; ---------------------计时未到0时,输出使能信号为0
if(l="0000"and m/="0000")then l<="1001";m<=m-1;-当各位倒计时到0,而十位没到0时十位减一,个位赋9
else l<=l-1; -----------------------------否则,各位减一
end if;
end if;
end if;
end if;
end if;
f<=n;g<=p;d<=l;e<=m;
end process;
end time_arc;