half_div_n<='1'WHEN(count_n<(F_DIV/2)-1)ELSE'0';
PROCESS(clock)--上升沿脉冲计数
--VARIABLEi:
IntegerRANGE0TO31;
BEGIN
IFRISING_EDGE(clock)THEN
IFfull_div_p='1'THEN
count_p<=count_p+1;
IF(half_div_p='1')THEN
clk_p_r<='0';
ELSE
clk_p_r<='1';
ENDIF;
ELSE
count_p<=(OTHERS=>'0');
clk_p_r<='0';
ENDIF;
ENDIF;
ENDPROCESS;
PROCESS(clock)--下降沿脉冲计数
BEGIN
IFFALLING_EDGE(clock)THEN
IFfull_div_n='1'THEN
count_n<=count_n+1;
IFhalf_div_n='1'THEN
clk_n_r<='0';
ELSE
clk_n_r<='1';
ENDIF;
ELSE
count_n<=(OTHERS=>'0');
clk_n_r<='0';
ENDIF;
ENDIF;
ENDPROCESS;
PROCESS(clock)
BEGIN
IFRISING_EDGE(clock)THEN
IFF_DIV=1THEN
clock_out_r<=clock;
ELSE
IF(F_DIVREM2)=1THEN
clock_out_r<=clk_p_rANDclk_n_r;
ELSE
clock_out_r<=clk_p_r;
ENDIF;
ENDIF;
ENDIF;
ENDPROCESS;
END;
3.主控电路(状态机)
主控电路是一个单进程的Moore型有限状态机,状态转换图如图3-1所示。
图3-1状态转换图
每种状态说明如下:
原状态
目的状态
转换条件
状态输出
s0
s1
s=’1’且s45=’1’
ledsn<="100001";c45<='1';c25<='0';c545<='0';c525<='0';
s1
s2
s545=’1’
ledsn<="010001";c45<='0';c25<='0';c545<='1';c525<='0';
s2
s3
s25=’1’
ledsn<="001100";c45<='0';c25<='1';c545<='0';c525<='0';
s3
s0
s525=’1’
ledsn<="001010";c45<='0';c25<='0';c545<='0';c525<='1';
状态机原理图如图3-2所示。
图3-2状态机原理图
状态机仿真结果如图3-3所示。
图3-3状态机仿真结果
从仿真结果可以看到,满足状态转换条件后,状态机就会从现在的状态转移到下一个状态,具体的转换结果与上表相同。
其VHDL代码如下:
libraryieee;
useieee.std_logic_1164.all;
entitytrafficis
port(CLK,RST,s,s45,s25,s545,s525:
instd_logic;
c45,c25,c545,c525:
outstd_logic;
ledsn:
outstd_logic_vector(5downto0));
endtraffic;
architectureoneoftrafficis
typesxis(s0,s1,s2,s3);
signalcurrent_state:
sx;
begin
process(RST,CLK)
begin
ifRST='1'then
current_state<=s0;ledsn<="100001";c45<='1';c25<='0';c545<='0';c525<='0';
elsifCLK'eventandCLK='1'then
casecurrent_stateis
whens0=>ifs='1'thencurrent_state<=s0;
elsifs45='1'thencurrent_state<=s1;
elsecurrent_state<=s0;endif;
ledsn<="100001";c45<='1';c25<='0';c545<='0';c525<='0';
whens1=>ifs545='1'thencurrent_state<=s2;
elsecurrent_state<=s1;endif;
ledsn<="010001";c45<='0';c25<='0';c545<='1';c525<='0';
whens2=>ifs25='1'thencurrent_state<=s3;
elsecurrent_state<=s2;endif;
ledsn<="001100";c45<='0';c25<='1';c545<='0';c525<='0';
whens3=>ifs525='1'thencurrent_state<=s0;
elsecurrent_state<=s3;endif;
ledsn<="001010";c45<='0';c25<='0';c545<='0';c525<='1';
endcase;
endif;
endprocess;
endone;
4.45秒倒计时电路
45秒倒计时电路是一个45秒的减一计数器,当减到0时输出进位信号作为状态机的状态转换信号。
其原理图如图4-1所示。
图4-145秒计数器原理图
45秒倒计时电路的仿真结果如图4-2所示。
图4-245秒计数器仿真结果
其VHDL代码如下:
libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
entitycnt45is
port(CLK,RST,EN:
instd_logic;
CQ:
outstd_logic_vector(5downto0);
cout:
outstd_logic);
end;
architecturebehavofcnt45is
begin
process(CLK,RST,EN)
variableCQI:
std_logic_vector(5downto0);
begin
ifCLK'eventandCLK='1'then
ifRST='1'thenCQI:
="101100";
elsifEN='1'then
ifCQI>0thenCQI:
=CQI-1;cout<='0';
elseCQI:
="101100";cout<='1';
endif;
endif;
endif;
CQ<=CQI;
endprocess;
endbehav;
5.25秒倒计时电路
25秒倒计时电路是一个25秒的减一计数器,当减到0时输出进位信号作为状态机的状态转换信号。
其原理图如图5-1所示。
图5-125秒计数器原理图
25秒倒计时电路的仿真结果如图5-2所示。
图5-225秒计数器仿真结果
其VHDL代码如下:
libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
entitycnt25is
port(CLK,RST,EN:
instd_logic;
CQ:
outstd_logic_vector(4downto0);
cout:
outstd_logic);
end;
architecturebehavofcnt25is
begin
process(CLK,RST,EN)
variableCQI:
std_logic_vector(4downto0);
begin
ifCLK'eventandCLK='1'then
ifRST='1'thenCQI:
="11000";
elsifEN='1'then
ifCQI>0thenCQI:
=CQI-1;cout<='0';
elseCQI:
="11000";cout<='1';
endif;
endif;
endif;
CQ<=CQI;
endprocess;
endbehav;
6.5秒倒计时电路
5秒倒计时电路是一个5秒的减一计数器,当减到0时输出进位信号作为状态机的状态转换信号。
其原理图如图6-1所示。
图6-15秒计数器原理图
5秒倒计时电路的仿真结果如图6-2所示,由于交通灯没有对黄灯5秒的倒计时进行显示,所以没有添加计数输出的端口,只用到了进位端口,在仿真图中可以看到,每次5个计数脉冲之后计数器就会输出一个进位信号。
图6-25秒计数器仿真结果
其VHDL代码如下:
libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
entitycnt5is
port(CLK,RST,EN:
instd_logic;
cout:
outstd_logic);
end;
architecturebehavofcnt5is
begin
process(CLK,RST,EN)
variableCQI:
std_logic_vector(2downto0);
begin
ifCLK'eventandCLK='1'then
ifRST='1'thenCQI:
="100";
elsifEN='1'then
ifCQI>0thenCQI:
=CQI-1;cout<='0';
elseCQI:
="100";cout<='1';
endif;
endif;
endif;
endprocess;
endbehav;
7.数码管动态扫描译码电路
数码管动态扫描译码电路如图7-1所示,此电路负责将主干道和支干道的45秒和25秒倒计时进行译码然后显示输出。
考虑到实验箱上的数码管位8位数码管,这里采用动态扫描的显示方式。
由于仿真结果并不能正确的看到数码管的输出结果,这里没有给出仿真图。
图7-1数码管动态扫描译码电路原理图
其VHDL代码如下:
libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
entitydecl7s4is
port(clk:
instd_logic;
a45:
instd_logic_vector(5downto0);
a25:
instd_logic_vector(4downto0);
sg:
outstd_logic_vector(6downto0);
bt:
outstd_logic_vector(3downto0));
end;
architectureoneofdecl7s4is
signalcnt4:
std_logic_vector(2downto0);
signala:
std_logic_vector(5downto0);
signala1:
std_logic_vector(5downto0);
signala2:
std_logic_vector(5downto0);
signala3:
std_logic_vector(5downto0);
signala4:
std_logic_vector(5downto0);
begin
p0:
process(a45)
begin
casea45is
when"000000"=>a2<="000000";a1<="000000";---0
when"000001"=>a2<="000000";a1<="000001";---1
when"000010"=>a2<="000000";a1<="000010";---2
when"000011"=>a2<="000000";a1<="000011";---3
when"000100"=>a2<="000000";a1<="000100";---4
when"000101"=>a2<="000000";a1<="000101";---5
when"000110"=>a2<="000000";a1<="000110";---6
when"000111"=>a2<="000000";a1<="000111";---7
when"001000"=>a2<="000000";a1<="001000";---8
when"001001"=>a2<="000000";a1<="001001";---9
when"001010"=>a2<="000001";a1<="000000";---10
when"001011"=>a2<="000001";a1<="000001";---11
when"001100"=>a2<="000001";a1<="000010";---12
when"001101"=>a2<="000001";a1<="000011";---13
when"001110"=>a2<="000001";a1<="000100";---14
when"001111"=>a2<="000001";a1<="000101";---15
when"010000"=>a2<="000001";a1<="000110";---16
when