|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
用VHDL语言编写十字路口交通灯
8 k) T2 a7 v" s$ t* N9 E* |0 J; e$ ]. y2 a% D' p' j* l) `
library IEEE;( n. y* a6 @9 g4 X
use IEEE.STD_LOGIC_1164.ALL; }- U7 M! |5 [
use IEEE.STD_LOGIC_ARITH.ALL;
) Z. n% L8 P3 r5 g& c/ Luse IEEE.STD_LOGIC_UNSIGNED.ALL;# X/ s, h' V, p0 m" \5 A3 L
entity TrafficLight is
( B+ G2 i, `2 ^" @+ KPort(clk2, reset: in std_logic;
6 ]) l$ F' ]- `) g- |. j5 Klight: out std_logic_vector(11 downto 0));
# \3 u6 O$ J; F& A/ Gend TrafficLight;) C( S: c8 l$ f! g- F8 r
architecture BEHAVIORAL of TrafficLight is, {2 j1 b% d* Y; f( b
type zt is(s0, s1, s2, s3);
# u& V8 Z& O' h+ ysignal current_state: zt;
( {4 B5 p5 d- B+ U1 H' Jsignal clk1, clk3,clk4, clk1k: std_logic;
6 {7 c" W# v! a2 A lsignal east_red, east_yellow, east_green: std_logic; --东方三种灯
- @! v$ b0 I& x' K$ w8 jsignal west_red, west_yellow, west_green: std_logic; --西方三种灯1 v; a5 x- }$ \ n7 j
signal north_red, north_yellow, north_green: std_logic; --北方三种灯
" B3 v* ^" i! i2 d* msignal south_red, south_yellow, south_green: std_logic; --南方三种灯
: b$ H$ ?( A) I9 I% Y; Asignal ew_high, ns_high: std_logic_vector(3 downto 0); --南北和东西方向倒计时显示的高位数
+ \/ [: Y' L- C8 ^& N. w& R- Zsignal ew_low, ns_low: std_logic_vector(3 downto 0); --倒计时显示的低位数
7 C& J6 ~* g/ O2 t1 mbegin) S* D0 G' q, H: \1 b
p1: process3 x0 ?5 L N1 K* b! ?
variable c: integer := 0;- r) v" w' B2 u0 n9 L/ D/ r) E
begin
: ~3 Q- a3 L9 jwait until clk2'event and clk2 = '1';
9 l/ j* x. ] b( X1 W pif c < 50 then
6 V; U3 L7 q3 r O, n8 a6 M! }$ R8 k9 `c := c + 1;
9 t: x* k$ n. v1 T" e selse* T% L! W+ ~" U8 h
clk1k <= not clk1k;
6 y$ x5 U+ Z+ @! }c := 0;' V% {1 x! K6 @; L6 Z4 l4 H$ n
end if;
' R8 | n: ]; s3 Q# r4 @end process p1;
* E, @3 h& r" lp11: process
6 L! C2 r8 W5 \' j7 m* p$ Jvariable c: integer := 0;
. ]8 q9 X( n" w! t; qbegin
- I; P! R# N8 O* ?wait until clk1k'event and clk1k = '1';
- T6 `5 `; _: q) @! v6 qif c < 125 then- j. w! g2 j, Z) B: N( d
c := c + 1;
8 r$ s4 A8 z$ felse1 T& w2 E- [. @7 A
clk4 <= not clk4;% g9 s. x! K+ e5 m2 ~) Z: [
c:= 0;
1 T( c3 w, ~( ~0 gend if;# s! [0 o, A' d
end process p11;1 R5 q; z$ u1 h, |- W
p2: process(clk4) --从4hz分频到2hz,用于黄灯闪烁
) F8 B/ o) c7 M8 V4 ebegin0 G9 c" U2 @8 f" x c
if rising_edge(clk4) then/ y3 ^6 O- \8 z$ t
clk3 <= not clk3;
# p0 T( |$ H/ O' t' dend if;4 H) a$ t! \2 D) y1 U G1 Y' F( ^3 X
end process p2;4 l+ R$ U" K% {: Q0 v7 p1 D. \
p3:process(clk3) --从2hz分频到1hz,计数时钟
, P; X3 q0 f8 `( s7 R @' Lbegin
9 c" Y; R0 U2 P; _ W3 ~if rising_edge(clk3) then8 s2 p$ T* I) y% f; G+ R( d
clk1 <= not clk1;! J+ ~! B/ @* F; H; i8 w2 Z* u/ E
end if;
! v5 [1 `! v! s3 Y& Zend process p3;
) U& T' Q+ h1 _4 g4 ~1 T+ \p4: process(reset, clk1, current_state)
- m. R7 @. {5 m: U0 Qbegin
* @! \' T. w0 T1 d+ ]; rif(clk1 = '1' and clk1'event) then --当clk1处于上升沿; V' i# n6 C$ V* }- w+ V" e7 y# |' L
if reset = '1' then
# X6 w8 _$ p/ T) r: Ucurrent_state <= s0; --复位时强行进入s0状态1 H& [3 l0 O9 w$ L/ x3 d
ns_low <= "0000";
4 V& Q/ |4 _& d# x }+ @: X- Tns_high <= "0000";! S+ `* g1 ?( Q% L) t8 P
ew_low <= "0000";
# Q2 P3 T: ~- Y8 s0 Lew_high <= "0000";
1 |# m* Y! K7 C2 Y5 Belse
, w* W* L7 H. ?1 z0 j& q9 K; U; n- {if(ns_low = "0000" and not(ns_high = "0000")) then --如果低位不够减,则高位减一,低位置9
/ p, S; A' Y- z; q2 gns_low <= "1001";% ]) ~5 s+ Q% |
ns_high <= ns_high - 1;
+ i* B' X4 [+ g& Jelse --每扫描一次,时间减一,如果低位够减,则只需低位减一,高位不变4 k9 I) I8 D l) E+ V
ns_low <= ns_low - 1;: l( m! \; V3 `% C. W
ns_high <= ns_high;
4 }; Z) Q4 t3 K s2 n0 D, dend if;
8 n( F7 z" R7 X9 Wif(ew_low = "0000" and not(ew_high = "0000")) then
6 B8 ^) }- l/ vew_low <= "1001";( G& f2 s/ A3 S; J6 v8 d& v4 J
ew_high <= ew_high - 1;; z1 a/ i, v$ n" _
else
1 {4 @/ v( B8 l* ~; P6 w. cew_low <= ew_low - 1;& C, q, ]( P( ]" N( D
ew_high <= ew_high;
: s* G t" x. \9 k% mend if;
i, z" R/ N, I9 b, @5 [case current_state is --检测当前状态
) |: q" U1 N' g: Owhen s0 => --当现在是南北红,东西绿* a q) U6 y- f, p! k. ?2 g
if(ew_high = "0000" and ew_low = "0000") then --如果绿灯时间结束
' T6 I7 R, @( R- @ g" T, B9 Ecurrent_state <= s1; --改变状态为南北红,东西黄, N2 K9 W0 z6 @; V: e5 ^2 {; g
north_red <= '1'; south_red <= '1';$ T% G5 l7 q4 s. n: ^
north_yellow <= '0'; south_yellow <= '0';
! F( H7 Q K+ P Q& N$ N9 u4 S' tnorth_green <= '0'; south_green <= '0';
$ ]& S5 @. t- S. ~5 Keast_yellow <= '1'; west_yellow <= '1';
; [7 d! J, ~9 L2 [8 I T! }5 ~east_green <= '0'; west_green <= '0';
) E% S; d* ` E% `east_red <= '0'; west_red <= '0';- Z2 o* F# e5 D- D" ^/ d
ew_high <= "0000"; ew_low <= "0100"; --黄灯显示时间为4
7 O. P6 O \. t& W$ `/ u( v& pend if;" D& N6 [ f3 p! r" y3 E& B( y" Q
when s1 => --当现在是南北红,东西黄
7 s; ^% B# M Z9 Zif(ew_high = "0000" and ew_low = "0000") then --黄灯时间结束2 D# Z$ `6 o# M6 ^) |. M9 I. _
current_state <= s2; --南北绿,东西红
- U4 ^6 q+ a, B9 ~) lnorth_green <= '1'; south_green <= '1';$ {* a7 Y% v; V
north_red <= '0'; south_red <= '0';
- x0 O( Y% S& x# r1 _1 Fnorth_yellow <= '0'; south_yellow <= '0';! X, W; p1 z& ~, S5 T) ]) Y
east_red <= '1'; west_red <= '1';7 p" M$ K9 ~5 T! h) @1 \
east_green <= '0'; west_green <= '0';3 O, Q% k+ V0 w3 K" @
east_yellow <= '0'; west_yellow <= '0'; C5 I4 _0 X, }+ j S( z; c( l& N
ns_high <= "0001"; ns_low <= "1001"; --绿灯显示时间为19
: ^% H0 {# M- ]- m- Gew_high <= "0010"; ew_low <= "0100"; --红灯显示时间为24,多出来1因为要对应两个01 B2 R2 N0 I* A" e3 ~7 O
end if;' d1 J8 z5 C6 N6 L0 z
when s2 => --当现在是南北绿,东西红
6 G: \; z: m+ R4 y" |1 gif(ns_high = "0000" and ns_low = "0000") then --绿灯时间结束
: n! r1 z/ L" n" z: f* ycurrent_state <= s3; --南北黄,东西红
& D/ X1 m9 b c/ _' @north_yellow <= '1'; south_yellow <= '1';8 m; S8 L& {" ~5 S( e' ~' b
north_green <= '0'; south_green <= '0';
1 x( b, M6 I1 G7 \north_red <= '0'; south_red <= '0';
4 o5 C1 T; _) @7 U0 `east_red <= '1'; west_red <= '1';
7 I2 |6 v( q% T) l4 d( V, H5 heast_green <= '0'; west_green <= '0';
8 V+ d* u/ A* u2 K- g) m2 teast_yellow <= '0'; west_yellow <= '0';
" @8 v9 f1 y* J+ J6 Z" D7 U% gns_high <= "0000"; ns_low <= "0100"; --黄灯时间为4! b, l6 E# ^9 N$ W" y; u
end if;
8 |) t1 \: @8 }2 i( ~: U( E- dwhen s3 => --当现在是南北黄,东西红$ q! V- i! z! U! _. `. S2 \ n
if(ns_high = "0000" and ns_low = "0000") then --黄灯时间结束( y$ r6 c; \# [ ^2 u1 O2 F0 j
current_state <= s0; --南北红,东西绿
8 T1 r; `2 V5 p" O. O3 y2 J' Mnorth_red <= '1'; south_red <= '1';
" @% |, v& ?9 f$ Rnorth_green <= '0'; south_green <= '0';# R4 a9 ]# S9 H: r6 J( g0 X
north_yellow <= '0'; south_yellow <= '0';
- F# B0 X2 C8 _' X. K& J' ^, Qeast_green <= '1'; west_green <= '1'; N. H2 M# y# s' a/ ?
east_red <= '0'; west_red <= '0';" t+ Q" k) E6 _! Y* y% t a
east_yellow <= '0'; west_yellow <= '0';0 n! C, r. g$ l# h+ ^; M: i
ns_high <= "0010"; ns_low <= "0100"; --红灯时间为24
/ f" J' R7 U2 f0 O1 N7 ^$ h; N( [ew_high <= "0001"; ew_low <= "1001"; --绿灯时间为19
! x- Y2 r9 E7 w- R" }) l/ |; Bend if;
1 w+ O0 D, O9 \- c" uend case;/ ^! V* S$ W" @2 h/ e2 e+ g* H" R
end if;
% D! i6 O1 s, ^* tend if;% K; f+ t2 ^) X Q6 o
end process;0 Y3 @& a% J! ?4 c, D3 S/ i/ m
light(0) <= east_red; light(1) <= east_green;
$ y3 i! L) c' b" e2 v wlight(2) <= east_yellow and clk3; --实现黄灯闪烁
( r. X+ L+ a6 Q3 N& n" slight(3) <= south_red; light(4) <= south_green;; Q6 o; v. R/ s( {' D
light(5) <= south_yellow and clk3; --实现黄灯闪烁& Q$ C- k) G) |1 d1 l
light(6) <= west_red; light(7) <= west_green;; X4 k ~! P4 ~& p' ]
light(8) <= west_yellow and clk3; --实现黄灯闪烁
1 |9 G( Y8 W# [light(9) <= north_red; light(10) <= north_green;
2 C" r5 X2 q9 E8 C# olight(11) <= north_yellow and clk3; --实现黄灯闪烁' H" y8 e# R! Q2 Q2 c
end BEHAVIORAL; | 3 \- S9 ^+ k( V$ h& b
|
|