交通控制系统

 

【设计要求】 

VHDL语言设计一个由一条主干道和一条支干道的汇合点形成的十字交叉路口的交通灯控制器,要求如下:

1)主、支干道各设一个红、绿、黄指示灯,LED显示

2)主干道处于常允许通行状态,支干道有车来的时候才允许通行;主干道允许通行时亮绿灯,支干道红灯。支干道允许通行时亮绿灯,主干道亮红灯;

3)主、支干道均有车时,两者交替通行,主干道每次放行45秒,支干道每次放行25秒,在每次由亮绿灯变成亮红灯时,都有5秒的黄灯过渡。

 

【系统方案设计】

 

 

根据交通信号灯控制的要求,我们可用状态机来实现交通灯控制器。控制器由定时器和控制器两部分组成,其原理方框图如图-1所示。mainbranch分别为主干道和支干道来车信号检测,main_redmain_yellowmain_green为主干道红、黄、绿灯驱动信号输出,branch_redbranch_yellowbranch_green为支干道红、黄、绿灯驱动信号输出。

 

  -1  交通信号灯控制原理方框图

 

   -2 交通信号灯控制系统整体电路图

 

【源代码实现】

1.分频器模块(图-3):

    采用verilog语言,实现功能为将50MHz系统时钟分频为1KHz

其实现代码如下:

module fdiv( clk1, f1khz, ); output f1khz; input clk1; //50000KHz input reg f1khz; integer aa=0; always @(posedge clk1) begin if(aa<25000000) begin aa=aa+1; f1khz<=1'b0; end else begin aa= 0; f1khz <= 1'b1; end end endmodule  

 

2.控制器模块(图-4):

    采用VHDL语言,由定时器和控制器两部分组成,定时器用于计时LED显示。

其实现代码如下:

 

library ieee; use ieee.std_logic_1164.all; entity ad is port( clk,main,branch:in bit; main_red,main_yellow,main_green,branch_red, branch_yellow,branch_green: out bit ; ge,shi: out integer ); end ad; architecture behave of ad is type state_type is (a,b,c,d); signal state :state_type; begin cnt:process(clk) variable s: integer range 0 to 44 ; variable clr,en :bit; begin if (clk'event and clk='1') then if clr='0' then s:=0; elsif en='0'then s:=1; else s:=s+1; end if; case state is when a=>main_red<='1';main_yellow<='1';main_green<='0'; branch_red<='0';branch_yellow<='1';branch_green<='1'; if(branch and main)='1' then if s=44 then state<=b;clr:='0';en:='0'; else state<=a;clr:='1';en:='1'; end if; elsif(branch and (not main))='1' then state<=b;clr:='0';en:='0'; else state<=a;clr:='1';en:='1'; end if; when b=>main_red<='1';main_yellow<='0';main_green<='1'; branch_red<='0';branch_yellow<='1';branch_green<='1'; if s=4 then state<=c;clr:='0';en:='0'; else state<=b;clr:='1';en:='1'; end if; when c=>main_red<='0';main_yellow<='1';main_green<='1'; branch_red<='1';branch_yellow<='1';branch_green<='0'; if(main and branch)='1' then if s=24 then state<=d;clr:='0';en:='0'; else state<=c;clr:='1';en:='1'; end if; elsif branch='0' then state<=d;clr:='0';en:='0'; else state<=c;clr:='1';en:='1'; end if; when d=>main_red<='0';main_yellow<='1';main_green<='1'; branch_red<='1';branch_yellow<='0';branch_green<='1'; if s=3 then state<=a;clr:='0';en:='0'; else state<=d;clr:='1';en:='1'; end if; shi<=s/10; if(s>=0) then ge <=(s rem 10); else ge<=0; end if; end case; end if; end process cnt; end behave;  

 

 

3.LED显示模块(图-5):

采用VHDL语言,其实现代码如下:

library ieee; use ieee.std_logic_1164.all; entity led is port( ge,shi:in integer; hexs: out std_logic_vector(6 downto 0) ; hexg: out std_logic_vector(6 downto 0) ); end led; architecture behave of led is begin process(ge, shi) begin case shi is when 0 => hexs<="0000001"; when 1 => hexs<="1001111"; when 2 => hexs<="0010010"; when 3 => hexs<="0000110"; when 4 => hexs<="1001100"; when others =>hexs<="0000001"; end case; case ge is when 0 => hexg<="0000001"; when 1 => hexg<="1001111"; when 2 => hexg<="0010010"; when 3 => hexg<="0000110"; when 4 => hexg<="1001100"; when 5 => hexg<="0100100"; when 6 => hexg<="0100000"; when 7 => hexg<="0001111"; when 8 => hexg<="0000000"; when 9 => hexg<="0000100"; when others => hexg<="1111111"; end case; end process; end behave;  

 


 

你可能感兴趣的:(FPGA(Altera))