一、设计任务设计一个十字路口的红、绿、黄三色信号交通灯控制电路,具体要求如下:1)用红、绿、黄三色发光二极管作信号灯。
主干道为东西向,有红、绿、黄三个灯;另一支干道为南北向,也有红、绿、黄三个灯。
红灯亮禁止通行;绿灯亮允许通行;黄灯亮则给行驶中的车辆有时间停靠到禁行线之外。
2)东西和南北每次绿灯放行26s,红灯禁止30s。
在每次由亮绿灯变成亮红灯的转换过程中间,需要亮5s的黄灯作为过渡,以使行驶中的车辆有时间停靠到禁行线以外。
3)能实现正常的、即时显示功能,用实验箱上的4个七段数码管作为到计时显示器,分别显示东西、南北方向的红灯、绿灯、黄灯时间。
二、设计原理首先要对时钟进行分频。
由于系统时钟频率比较大,因此首先分频产生时钟,用于下面的电路的控制;然后是各种颜色之间的转换,在此在添加一个使能端en,当使能端en为1的时候,就开始进行状态循环以及倒计时,然后en就立即变为0;在状态机中一共有四个状态,如下图所示:然后,我们这里用了BCD码表示倒计时时间。
灯亮或闪烁时间(绿、黄、红分别为26s、130s、5s)用BCD码表示(分别为26h、30h、5h),倒计时的时候个位和十位分别是BCD码的高四位和低四位,首先是低四位倒数,当倒数到0时,给它重新赋值为9,且高四位减1,如此循环,直到这个数减到0,此时表示某一个灯亮的时间到,接着进行下一个状态,为了能使进入下一个状态,必须在时间减到0的时候,给使能端en 赋值1;由于用的BCD码,高四位和低四位就分别是我们要在译码模块的要用数码管显示的十位和个位。
用数据选择器来控制东西、南北的灯亮。
三、程序流程图1.1分频器的设计流程图1.2 5进制的设计流程图1.3 30进制的设计流程图1.4 26进制的设计流程图1.5 状态机的程序流程图四、程序设计1、5进制的设计library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity jinzhi5 isport(clk,en,rst:in std_logic;ge,shi: out std_logic_vector(3 downto 0);cout:out std_logic);end jinzhi5;architecture behav of jinzhi5 isbeginprocess(clk,en)variable a,b: std_logic_vector(3 downto 0);beginif(rst='0') then a:="0101";b:="0000"elsif clk'event and clk='1' thenif(en='1') thenif(a=0) then a:="0101";b:="0000",cout<='1';else a:=a-1;b:="0000",cout<='0';end if;end if;end if;ge<=a;shi<=b;end process;end behav;仿真结果2、26进制的程序library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity jinzhi26 isport(clk,en:in std_logic;ge: out std_logic_vector(3 downto 0);shi: out std_logic_vector(3 downto 0);cout:out std_logic);end jinzhi26;architecture behav of jinzhi26 isbeginprocess(clk,en)variable a: std_logic_vector(3 downto 0);variable b: std_logic_vector(3 downto 0);beginif(en='0') then a:="0010";b:="0101";elsif clk'event and clk='1' thenif(a=0 and b=0) then a:="0010";b:="0101";cout<='1';else if(b=0) then b:="1001";a:=a-1;else b:=b-1;cout<='0';end if;end if;end if;ge<=b;shi<=a;end process;end behav;仿真结果3、30进制的程序library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity jinzhi30 isport(clk,en,rst:in std_logic;ge: out std_logic_vector(3 downto 0);shi: out std_logic_vector(3 downto 0);cout:out std_logic);end jinzhi30;architecture behav of jinzhi30 isbeginprocess(clk,en)variable a: std_logic_vector(3 downto 0);variable b: std_logic_vector(3 downto 0);beginif(rst='0') then a:="0000";b:="0000";elsif clk'event and clk='1' thenif en='1' thenif(a=0 and b=0) then a:="0011";b:="0000";cout<='1';else if(b=0) then b:="1001";a:=a-1;else b:=b-1;cout<='0';end if;end if;end if;end if;ge<=b;shi<=a;end process;end behav;仿真结果4、10M分频器的设计library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity fenpin20 isport(clk:in std_logic;c:out std_logic);end fenpin10;architecture art of fenpin10 issignal m: std_logic_vector(31 downto 0);signal c1:std_logic;beginprocess(clk)beginif rising_edge(clk) thenif m<4999999 thenm<=m+1;else m<=(others=>'0');c1<=not c1;end if;end if;c<=c1;end process;end art;仿真结果5、译码器的程序设计电路中需要4个译码器来显示东西、南北的亮灯时间。
library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity yima isport( qi: in std_logic_vector(3 downto 0);qout:out std_logic_vector(6 downto 0));end yima;architecture behav of yima issignal q:std_logic_vector(6 downto 0);beginprocess(qi)begincase qi iswhen "0000" => q<="0111111";when "0001" => q<="0000110";when "0010" => q<="1011011";when "0011" => q<="1001111";when "0100" => q<="1100110";when "0101" => q<="1101101";when "0110" => q<="1111101";when "0111" => q<="0000111";when "1000" => q<="1111111";when "1001" => q<="1101111";when others => null;end case;end process;qout<=q;end behav;仿真波形6、四选一数据分配器的程序数据选择器中,Y1,Y2,Y2是从状态机中输出的控制信号,sshi,sge分别代表30进制的十位和各位,eshi,ege分别代表26进制的十位和各位,wshi,wge分别代表5进制的十位和各位。
jshi,jge分别连接着甲道的数码管来显示时间。
Yshi,yge分别连接着甲道的数码管来显示时间。
由于仿真时候设置的数据太多了,在这里就不进行波形仿真了。
library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity xuanzeqi isport(Y1,Y2,Y3,rst:in std_logic;sshi,sge,eshi,ege,wu:in std_logic_vector(3 downto 0);jshi,jge,yshi,yge:out std_logic_vector(3 downto 0));end xuanzeqi;architecture behav of xuanzeqi issignal yy: std_logic_vector(2 downto 0);beginyy<=Y1&Y2&Y3;process(yy)beginif rst='0' then jshi<="0000";jge<="0000";yshi<="0000";yge<="0000";elsecase yy iswhen"001"=> jshi<=eshi;jge<=ege;yshi<=sshi;yge<=sge;when"010"=> jshi<="0000";jge<=wu;yshi<=sshi;yge<=sge;when"011"=> jshi<=sshi;jge<=sge;yshi<=eshi;yge<=ege;when"100"=> jshi<=sshi;jge<=sge;yshi<="0000";yge<=wu;when others=>null;end case;end if;end process;end behav;7、状态机的设计w1,w2,w3分别为5、26、30进制的进位输出信号,c1,c2,c3分别连接着5、26、30进制的使能端,控制计数器输出信号。