找回密码
 注册
关于网站域名变更的通知
查看: 394|回复: 1
打印 上一主题 下一主题

用VHDL语言编写十字路口交通灯

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2019-3-11 13:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x
用VHDL语言编写十字路口交通灯
. R0 d2 l) |  R, w( @
9 I. D$ o8 l( M1 {- n# f
library IEEE;
8 b  x) ~% K6 h2 [1 luse IEEE.STD_LOGIC_1164.ALL;
3 I9 V- T( ]0 r/ Y8 k+ d& Nuse IEEE.STD_LOGIC_ARITH.ALL;  @9 @* _% U; `% Q3 k1 k; ^5 K1 s, G
use IEEE.STD_LOGIC_UNSIGNED.ALL;
* |3 i2 l( C2 ientity TrafficLight is
6 R. s* F" O* v9 F. ~Port(clk2, reset: in std_logic;
' {' N; k& X; @7 elight: out std_logic_vector(11 downto 0));- i( M, M5 y) ^
end TrafficLight;
  V0 Q* z1 @9 C3 R" ]architecture BEHAVIORAL of TrafficLight is% @/ E& A7 m- u
type zt is(s0, s1, s2, s3);2 r% S' p) d9 m% Q* x: S
signal current_state: zt;- X! h! I: O4 S5 F
signal clk1, clk3,clk4, clk1k: std_logic;
: z5 R' u4 v* Lsignal east_red, east_yellow, east_green: std_logic; --东方三种灯
- y1 u; q4 _7 R! psignal west_red, west_yellow, west_green: std_logic; --西方三种灯
. z5 e6 ~+ R4 d) j4 ~# x" Rsignal north_red, north_yellow, north_green: std_logic; --北方三种灯/ r5 q' S( g( p
signal south_red, south_yellow, south_green: std_logic; --南方三种灯
, p4 z9 V; q+ {1 C6 J4 s6 Tsignal ew_high, ns_high: std_logic_vector(3 downto 0); --南北和东西方向倒计时显示的高位数; D  f: i1 q, @2 z, o3 E% H* U
signal ew_low, ns_low: std_logic_vector(3 downto 0); --倒计时显示的低位数
9 i9 L% H- ^& X, o4 lbegin* S4 X& t0 a, ]% b3 Z' |& |2 z) V7 {
p1: process$ i/ ?9 V' c5 U( H0 W
variable c: integer := 0;& |# x/ L- Q" H! K) h
begin; E5 r* F: _8 B1 K; m# ~4 ]  n
wait until clk2'event and clk2 = '1';
2 {" j. N/ Q+ \) K5 nif c < 50 then
  S; z) y+ o: w1 B- Mc := c + 1;& d2 ~4 c; c5 K, [
else
3 z$ [% s- q, Z5 g% ]' a0 [clk1k <= not clk1k;* n8 t. ?; W; t0 c$ X' [/ t6 s
c := 0;
$ i2 O. ?4 V: |. j* }end if;1 e- ?& e- d+ T; C& X
end process p1;
3 k5 w& x2 c9 l( V( T! P  Q+ qp11: process
7 _  D# X9 M3 g9 ]3 R2 f; xvariable c: integer := 0;7 [" Q% a3 l% Z$ d
begin# T/ {* P* C0 B/ u$ G4 [  H
wait until clk1k'event and clk1k = '1';
' U7 a5 ^0 V* R0 L+ P' ]if c < 125 then7 h3 L" _) {" X! A5 J1 s( ]
c := c + 1;
& t8 F, w+ z7 Delse
; ^1 P5 P, L! \! ^9 Lclk4 <= not clk4;  y  }( g( A: f3 u* Q
c:= 0;
) Z' T% w  f" x+ W/ j+ ~end if;
5 N& ]8 P$ i# d: p' X9 S/ Iend process p11;
( G3 P0 d% N  ?; @6 qp2: process(clk4) --从4hz分频到2hz,用于黄灯闪烁+ a( i3 }1 N- \5 u
begin
+ H. \4 ^) N) l1 h& kif rising_edge(clk4) then8 O; X6 _  F% R# n8 i' E
clk3 <= not clk3;1 W9 C- C2 I9 s7 ]; d7 O
end if;  k) i' \( o4 s- D" |
end process p2;
' s9 Q! W* I* F2 J8 r9 l6 Zp3:process(clk3) --从2hz分频到1hz,计数时钟
( {* f* G) w) K6 k! }6 Dbegin
5 J: n# o: v; Eif rising_edge(clk3) then
3 x" s* w2 d/ K% E; Xclk1 <= not clk1;9 Q4 A; Z" U: s. q$ h
end if;% B5 W" H+ L6 i9 u6 R
end process p3;& L' j4 {& D1 N" M
p4: process(reset, clk1, current_state)1 h8 _; y- V1 H& _6 Z9 M
begin; |: m6 @5 a+ q" i
if(clk1 = '1' and clk1'event) then --当clk1处于上升沿
8 {, f$ V- B* |% j7 ?! `2 Sif reset = '1' then( `( e, t  ?7 b2 ]. B- J
current_state <= s0; --复位时强行进入s0状态
4 @9 K3 J$ x1 J  z% Bns_low <= "0000";
# Q. g1 W9 T* O9 L2 s3 R8 ^- Zns_high <= "0000";2 f% T1 S( [' ~2 v; a* A; N
ew_low <= "0000";% s5 f( X: K0 W! X' t& F! [
ew_high <= "0000";
2 r  f2 \0 E: p( x3 i; selse0 V" ]1 O- q; c; y& m7 X' _
if(ns_low = "0000" and not(ns_high = "0000")) then --如果低位不够减,则高位减一,低位置9! U  t& L$ C. G5 h" z$ y
ns_low <= "1001";& R5 W4 f2 m' i3 F) b* X
ns_high <= ns_high - 1;
9 ?: ~3 c6 d& M7 v  [5 O/ T8 uelse --每扫描一次,时间减一,如果低位够减,则只需低位减一,高位不变7 u: f  B/ \# Z% _) A8 J
ns_low <= ns_low - 1;) A0 |; |! f8 [+ N) o* H
ns_high <= ns_high;7 d6 s4 R3 M! Q" c
end if;
2 S( q  x$ ~  c& Y( `! [( g$ O0 [if(ew_low = "0000" and not(ew_high = "0000")) then
7 ^) c6 ^1 d, i5 M3 M% o8 n( q8 ^6 Zew_low <= "1001";- B* T& o/ v/ S( n) J
ew_high <= ew_high - 1;  J  d5 F& |$ B/ h. n9 X
else$ \2 n& B2 S/ r9 P
ew_low <= ew_low - 1;
# K( W$ W9 m- g0 o9 cew_high <= ew_high;
" @. D  a, P3 Iend if;
" N0 L1 k, E7 c: p9 `case current_state is --检测当前状态
5 |+ i! v0 S" |" @" cwhen s0 => --当现在是南北红,东西绿: |: |9 t  p, g, c2 Y
if(ew_high = "0000" and ew_low = "0000") then --如果绿灯时间结束2 [% v3 `3 I3 E2 f, d
current_state <= s1; --改变状态为南北红,东西黄
$ @! d5 x# g4 c  B3 q; _9 q9 Enorth_red <= '1'; south_red <= '1';
- y) W4 b; J( r& O& _/ k) Wnorth_yellow <= '0'; south_yellow <= '0';! K8 w" d6 U; v2 R/ T) N
north_green <= '0'; south_green <= '0';# K4 f7 V! }5 Z( C( X6 ?. ]
east_yellow <= '1'; west_yellow <= '1';% }# P9 Q8 r5 H3 v2 s- }6 E
east_green <= '0'; west_green <= '0';2 J# d, ]' }5 }  A( _! w* C" a
east_red <= '0'; west_red <= '0';
, e/ T# G7 h2 m9 p# O8 h' g1 Pew_high <= "0000"; ew_low <= "0100"; --黄灯显示时间为4
, y( d$ @9 V' R5 Tend if;
7 p0 z! D8 p, M9 M! Bwhen s1 => --当现在是南北红,东西黄
8 M5 D: A* L+ K  Rif(ew_high = "0000" and ew_low = "0000") then --黄灯时间结束6 X9 e; P9 @' r; S( G4 W- ?
current_state <= s2; --南北绿,东西红
1 R" M# h5 z: X" fnorth_green <= '1'; south_green <= '1';
0 ]; J; {: z+ J9 S  N/ R2 wnorth_red <= '0'; south_red <= '0';# @- V- e9 s0 ^: O/ `
north_yellow <= '0'; south_yellow <= '0';# ]4 Z, N4 A3 @
east_red <= '1'; west_red <= '1';
$ z! X) R; I  ~  B* n. ueast_green <= '0'; west_green <= '0';( c2 ?% R, `# S  b& r. h
east_yellow <= '0'; west_yellow <= '0';0 O7 _; H* Z4 I9 W1 z, N( n
ns_high <= "0001"; ns_low <= "1001"; --绿灯显示时间为196 S! T. {! |; w8 p- }
ew_high <= "0010"; ew_low <= "0100"; --红灯显示时间为24,多出来1因为要对应两个09 N- J$ ~) I; L/ i  L
end if;
# k, u! r5 S1 Zwhen s2 => --当现在是南北绿,东西红
: k% }3 s/ a9 y8 u& t( lif(ns_high = "0000" and ns_low = "0000") then --绿灯时间结束, h. Y4 e: `$ {# O
current_state <= s3; --南北黄,东西红3 \3 b* ^3 Z  a
north_yellow <= '1'; south_yellow <= '1';
& J" o& Z/ @; @, I' w, U/ mnorth_green <= '0'; south_green <= '0';2 X4 I3 T  V, O
north_red <= '0'; south_red <= '0';
: U  w" o; H; qeast_red <= '1'; west_red <= '1';# a  }5 C# T9 V. T3 x( ?
east_green <= '0'; west_green <= '0';
# b% a) \7 E& T3 n  @east_yellow <= '0'; west_yellow <= '0';
5 M" k$ b6 b/ ^# Pns_high <= "0000"; ns_low <= "0100"; --黄灯时间为4
. K% `& \, ?- ?& iend if;1 _8 I# C: u- S# A2 O
when s3 => --当现在是南北黄,东西红
  c5 {1 j0 A, G% j: o# u( \* Hif(ns_high = "0000" and ns_low = "0000") then --黄灯时间结束
: e: l; L7 C% V+ B- U* ccurrent_state <= s0; --南北红,东西绿
& f& x2 O- N2 A- Z- Anorth_red <= '1'; south_red <= '1';
% x% U: W# i7 q5 z( \; Z9 }north_green <= '0'; south_green <= '0';
/ N" `' h! R# c7 _& t8 S. l* z7 inorth_yellow <= '0'; south_yellow <= '0';; E' h' y+ n. C2 b8 v. ?# r8 |
east_green <= '1'; west_green <= '1';' Q' V0 j- z4 l3 N( @# H
east_red <= '0'; west_red <= '0';4 _- C/ q" k' j
east_yellow <= '0'; west_yellow <= '0';! `4 ]% `4 O% V2 ~7 e
ns_high <= "0010"; ns_low <= "0100"; --红灯时间为241 e6 }7 D7 X' T3 }7 Y1 g$ U
ew_high <= "0001"; ew_low <= "1001"; --绿灯时间为19- h5 v/ d) ~# r, p3 {& R$ L
end if;
* l) A- h4 n0 {end case;
* b' x' ]5 M6 nend if;# U' z+ d# n7 J: b7 d' T
end if;* v/ k/ v6 M0 P2 x# J% x
end process;6 I" C! m& h1 x+ P6 j7 ]
light(0) <= east_red; light(1) <= east_green;# S4 A# Z; ^$ W1 d3 N  c. C1 r
light(2) <= east_yellow and clk3; --实现黄灯闪烁
6 q) l" q& b8 J) }: l# Alight(3) <= south_red; light(4) <= south_green;
5 n  z" c' q$ u1 `3 O3 e; Qlight(5) <= south_yellow and clk3; --实现黄灯闪烁
+ T! f0 E0 F+ j1 |light(6) <= west_red; light(7) <= west_green;* ?' n6 f9 l+ {& m
light(8) <= west_yellow and clk3; --实现黄灯闪烁
! k7 Z# C  l8 V- Jlight(9) <= north_red; light(10) <= north_green;
7 z8 d0 C! r6 D. blight(11) <= north_yellow and clk3; --实现黄灯闪烁- Q1 X( Y! q0 p- Z$ Y( h5 `* H( j" e
end BEHAVIORAL;

+ t, D+ F2 g0 l; t

该用户从未签到

2#
发表于 2019-3-11 14:53 | 只看该作者
谢谢楼主 这就去看看可行不
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

EDA365公众号

关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

GMT+8, 2025-7-28 09:15 , Processed in 0.125000 second(s), 23 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

快速回复 返回顶部 返回列表