|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
用VHDL语言编写十字路口交通灯
1 e$ s. [! n6 e$ z$ K
# c- k0 |4 @1 g" \library IEEE;
/ W- x. [ X( o$ ^/ nuse IEEE.STD_LOGIC_1164.ALL;
, P6 [6 P8 b# |: Euse IEEE.STD_LOGIC_ARITH.ALL;# E' \5 @7 S( Y* q: n/ b
use IEEE.STD_LOGIC_UNSIGNED.ALL;
; h! b/ m7 @6 s( Kentity TrafficLight is
7 U' _* t" J( z& X& H Z8 TPort(clk2, reset: in std_logic;/ b+ ~+ ^/ r1 U: `9 l# Z
light: out std_logic_vector(11 downto 0));
; g6 |# ~) L1 ^) A# @* u+ uend TrafficLight;3 e: n2 S6 H( A: U" o; I
architecture BEHAVIORAL of TrafficLight is
: G5 R6 [0 s2 C3 [9 h& N; }type zt is(s0, s1, s2, s3);
2 T1 k$ m- d, nsignal current_state: zt;
; Q6 q* s( z4 _% @; F& P P0 {signal clk1, clk3,clk4, clk1k: std_logic;
) o0 a/ u% V5 c, H& f6 X$ _' E- esignal east_red, east_yellow, east_green: std_logic; --东方三种灯
* o0 [: t/ a$ c9 E' }signal west_red, west_yellow, west_green: std_logic; --西方三种灯
7 W: k; M% R' e( C7 p! ksignal north_red, north_yellow, north_green: std_logic; --北方三种灯
+ D) J+ |6 a1 T4 a* n% Tsignal south_red, south_yellow, south_green: std_logic; --南方三种灯: G5 D3 h/ r8 |4 s5 S: B
signal ew_high, ns_high: std_logic_vector(3 downto 0); --南北和东西方向倒计时显示的高位数
5 b5 x6 S0 H7 t6 y2 U& Asignal ew_low, ns_low: std_logic_vector(3 downto 0); --倒计时显示的低位数
0 }4 S% t' B( n4 g9 K/ L" i! Vbegin. ]& u3 z$ Y" a
p1: process) S, e k3 p ~
variable c: integer := 0;1 j0 o3 b; I4 D9 J" V+ i
begin
& r# u; a- D. F& n9 Swait until clk2'event and clk2 = '1';# V1 m; b* F% X! X
if c < 50 then
8 L8 Q! Q+ X2 R+ sc := c + 1;
a4 D7 ?; g& u& S+ ]% w. Kelse2 a+ A) O3 E1 U/ y
clk1k <= not clk1k;
* p3 g% n! @9 A h# bc := 0;& t1 L! s3 w$ N* I: m: z }+ X4 _1 I
end if;
# J! `$ D9 |8 A8 Tend process p1;* B+ V k% H& y2 q3 ?, |1 x/ ~
p11: process
. [* {; S4 Y4 ^+ k: y7 d# avariable c: integer := 0;/ v0 V# s: K3 j
begin' S; A. p) A; e3 L6 R" D7 f( ^$ o
wait until clk1k'event and clk1k = '1';
5 g0 i: c2 L0 |( g& `if c < 125 then' Z7 t0 }9 J, W; V+ F( `' f
c := c + 1;! }9 {1 A2 v0 s0 o% O+ u0 ?
else
$ G! A- E0 U& ^clk4 <= not clk4;
; n, T: Q: v9 n3 K, X/ I$ f" fc:= 0;! O, N( X" o5 X: Q; d5 n6 H( u
end if;
* Y: [. M7 a3 r+ pend process p11;
& B4 p0 N- L( {. ]* bp2: process(clk4) --从4hz分频到2hz,用于黄灯闪烁
+ W: g$ q8 m* \begin
" f' O. n, _6 H: b. k" w) Wif rising_edge(clk4) then
v. g$ k K& V, q. ?1 j9 q- Rclk3 <= not clk3;2 d+ A* J0 r E6 Z1 h
end if;
4 b7 Z3 Z T$ [" Lend process p2;/ Y8 w6 K5 N) y ?0 n
p3:process(clk3) --从2hz分频到1hz,计数时钟
9 a! l' g7 Z4 C$ Rbegin. A+ J6 F+ c6 Z+ ~5 p' ?3 `8 k' @& j
if rising_edge(clk3) then
* U4 z( s3 H: w R; y. bclk1 <= not clk1;& O D+ C4 q7 H& D) Q7 \
end if;
' N; H5 T, u) @0 E6 H6 ?end process p3;
" j2 n! ?" G" g g F7 w$ d( O7 pp4: process(reset, clk1, current_state)7 y5 v+ r6 ]9 c3 q4 n- C
begin
. `6 L. K. L0 v- X! u% Bif(clk1 = '1' and clk1'event) then --当clk1处于上升沿
, s3 q' B7 s. V% vif reset = '1' then
% f! j- ~, H9 k0 M) Zcurrent_state <= s0; --复位时强行进入s0状态
/ h9 s8 x) ?( Ens_low <= "0000";) {" u0 r* I# d7 c+ |
ns_high <= "0000";: p5 [. s3 t( F$ F& N
ew_low <= "0000";
0 g: _. t- R& n+ C' pew_high <= "0000";
+ P# @( X; k+ K# h4 W9 V7 Belse% i5 k1 f9 }$ M3 V9 A. M. c
if(ns_low = "0000" and not(ns_high = "0000")) then --如果低位不够减,则高位减一,低位置9
9 C& q* l( X* d2 b' T, H( Cns_low <= "1001";4 M$ ~3 G% o" a9 g5 q
ns_high <= ns_high - 1;$ K8 N# ]1 k- k9 D5 l
else --每扫描一次,时间减一,如果低位够减,则只需低位减一,高位不变" N' ]0 c% `+ p( c/ G9 i
ns_low <= ns_low - 1;
5 v, k/ d& D; ^$ } G/ ?: fns_high <= ns_high;
) A: ]6 {& g6 N" gend if;6 z' h5 W/ B9 V
if(ew_low = "0000" and not(ew_high = "0000")) then
/ Z6 J4 A( _' Z( Y, jew_low <= "1001";' F% z3 ~* M! J$ _, x- f v
ew_high <= ew_high - 1;$ h' R# {2 I% B$ q4 v
else
* ]. r' x$ M$ ?* V/ vew_low <= ew_low - 1;: B' e: l9 b" V7 g. r
ew_high <= ew_high;
9 C+ I4 K8 X" ]end if;& y' E1 N Z3 R3 P6 ]2 o+ F: e
case current_state is --检测当前状态
% [3 l( J- r+ z6 Fwhen s0 => --当现在是南北红,东西绿
. h n8 Z5 A/ @1 f7 Z- r3 Pif(ew_high = "0000" and ew_low = "0000") then --如果绿灯时间结束8 k+ H7 Q* |' m5 z) t k
current_state <= s1; --改变状态为南北红,东西黄$ D1 P3 Z& N+ Q- Y6 S
north_red <= '1'; south_red <= '1';
# j) Z+ o# ]8 x5 f0 @# Snorth_yellow <= '0'; south_yellow <= '0';
3 r% Z$ |0 B4 J7 o8 N. {1 tnorth_green <= '0'; south_green <= '0';
6 Z9 z3 O$ N& beast_yellow <= '1'; west_yellow <= '1';
6 X3 D. b2 \+ y2 U- S; P- D1 |east_green <= '0'; west_green <= '0';
: H+ X; j3 q7 z8 X, S [5 y( K( Teast_red <= '0'; west_red <= '0';3 Z) Y1 W2 |- f
ew_high <= "0000"; ew_low <= "0100"; --黄灯显示时间为4$ M" C* s Y, B
end if;! d/ k/ v) v; v6 h, ]/ g
when s1 => --当现在是南北红,东西黄- h+ p( d5 l V" D; e
if(ew_high = "0000" and ew_low = "0000") then --黄灯时间结束
5 f* K$ `. O# ucurrent_state <= s2; --南北绿,东西红
6 x& s1 @+ ?( {7 K9 cnorth_green <= '1'; south_green <= '1';
1 T3 J) S" g' {8 W6 enorth_red <= '0'; south_red <= '0';
' }# b1 E! `* X7 z7 ]% Hnorth_yellow <= '0'; south_yellow <= '0';
- F4 |$ A+ w9 Z; e- r3 jeast_red <= '1'; west_red <= '1';' ?2 w% H1 `( N' M* k
east_green <= '0'; west_green <= '0';0 z! e0 c% k4 |5 V
east_yellow <= '0'; west_yellow <= '0';
3 D- K, a, P3 E- S0 g8 p5 Jns_high <= "0001"; ns_low <= "1001"; --绿灯显示时间为19( t N5 Q7 E; ~1 w! H; m# H
ew_high <= "0010"; ew_low <= "0100"; --红灯显示时间为24,多出来1因为要对应两个05 W* Z4 q# s0 S: e. {
end if;
: p' L6 h7 M$ \. `. B" Swhen s2 => --当现在是南北绿,东西红
: @' F" @4 \0 s( J9 ~if(ns_high = "0000" and ns_low = "0000") then --绿灯时间结束8 F0 g$ h0 D. D: \
current_state <= s3; --南北黄,东西红
' S% G0 ]' U8 Y wnorth_yellow <= '1'; south_yellow <= '1';% H; t3 M+ A7 B/ o( `% o
north_green <= '0'; south_green <= '0';
- c$ D9 U5 S V4 ?3 V: Anorth_red <= '0'; south_red <= '0';1 f7 i0 B) C! A' e. s
east_red <= '1'; west_red <= '1';
" O' ^6 J% l5 F" E/ R2 Teast_green <= '0'; west_green <= '0';4 F8 d* H, c! O4 T/ A* o
east_yellow <= '0'; west_yellow <= '0';
8 C6 _% K! I9 L% jns_high <= "0000"; ns_low <= "0100"; --黄灯时间为4& T7 ^' _0 t- v% j2 f) o* @* d, B
end if;; h {' t x' }+ s# D
when s3 => --当现在是南北黄,东西红
8 f: ]& U1 A+ }2 Iif(ns_high = "0000" and ns_low = "0000") then --黄灯时间结束
2 f6 h6 f8 w2 S2 D+ _0 Y0 Ocurrent_state <= s0; --南北红,东西绿1 R' g1 o9 Y' X4 ]: p6 s7 E
north_red <= '1'; south_red <= '1';
; a, p6 Z2 ^3 q* Cnorth_green <= '0'; south_green <= '0';
$ ]; J- m; M* A& ^( k. `6 `9 }north_yellow <= '0'; south_yellow <= '0';% Y, j ~4 ]* [' l5 C4 g& R8 j
east_green <= '1'; west_green <= '1'; V. k& D" w( W& w4 q+ R
east_red <= '0'; west_red <= '0';( [ E8 n) N; {; ]8 S' t) u) A
east_yellow <= '0'; west_yellow <= '0';
) w' `8 ?. i3 ^, ens_high <= "0010"; ns_low <= "0100"; --红灯时间为24
! ]0 N. h! E' P2 u; S% Oew_high <= "0001"; ew_low <= "1001"; --绿灯时间为199 B; f. H$ p1 e4 {
end if;5 E, e6 Y5 E" n& ?3 r8 H
end case;
g7 R! n+ }) y" O3 s5 Fend if;/ {5 c0 F. x! l6 |. t
end if;
4 u: l, w9 ^2 x& Hend process;
; H. N4 j. V+ xlight(0) <= east_red; light(1) <= east_green;
" G2 D) x( t* V8 U5 r0 D5 c- slight(2) <= east_yellow and clk3; --实现黄灯闪烁
- P7 g+ F3 U6 olight(3) <= south_red; light(4) <= south_green;
$ d/ x8 v5 f* c+ ]! jlight(5) <= south_yellow and clk3; --实现黄灯闪烁3 c o H* J1 g4 I W( u
light(6) <= west_red; light(7) <= west_green;
( |, k3 @+ P' }7 Slight(8) <= west_yellow and clk3; --实现黄灯闪烁
4 m$ T A! W& b9 [light(9) <= north_red; light(10) <= north_green;* F4 u6 K1 z- Q3 X) v1 y* {
light(11) <= north_yellow and clk3; --实现黄灯闪烁
: H- r8 B6 s9 Y; M: W& _5 {end BEHAVIORAL; |
6 v8 k$ L! N8 | |
|