|
# A: K, B+ O b8 Y8 K
#include<reg51.h>( M* G$ O- J" g: O3 l
#define uchar unsigned char/ v: a+ o' H& k( s J% o
#define uint unsigned int
) D+ p# `% z7 _6 `sbit RED_A=P0^0; //东西向灯/ z. g, F( q1 n+ F! y
sbit YELLOW_A=P0^1;
7 W3 [& X6 T f. `2 C- q7 {sbit GREEN_A=P0^2;
7 p$ a; q8 M9 Y4 Z; o E& D. \sbit RED_B=P0^3; //南北向灯0 e% K" w, p- k* A
sbit YELLOW_B=P0^4;9 L2 C0 O8 _ G
sbit GREEN_B=P0^5;
( e. o1 `/ h" ~. w) q) v7 huchar Flash_Count=0,Operation_Type=1; //闪烁次数,操作类型变量
P* N+ [. N# c8 i' T7 y
) H. ~! \1 y5 F' r//延时7 Y7 e5 c, k* C( r
void DelayMS(uint x)5 g& z1 s8 f$ T
{; i9 j$ T6 o0 ?& r1 A# y7 _4 S! f
uchar i;
4 S% M$ O+ L' P0 l* bwhile(x--) for(i=0;i<120;i++);" Q6 q' N* z4 A. R$ h1 M1 \; Z
}
; X' J' u. q; v1 ~//交通灯切换
; d! U/ t9 r. n+ f: A( U" Yvoid Traffic_Light()
( o7 T/ M( U. S$ W- W{
- l/ u9 X) D* c$ j. v4 tswitch(Operation_Type) z. b+ l" B3 N% D
{
9 F7 g, f$ o. e! c) [9 Ycase 1: //东西向绿灯与南北向红灯亮# s* e' K- q3 z9 V9 ^5 h2 u
RED_A=1;YELLOW_A=1;GREEN_A=0;; R9 F! A- l+ E& }0 ?# ]5 S
RED_B=0;YELLOW_B=1;GREEN_B=1;
( l; @' R" o, E" w/ i8 `DelayMS(2000);% Y. }! {. r' z, N5 J$ @9 w; H
Operation_Type=2;
+ u; w+ i4 u+ c8 Ibreak;
* i, O* _/ n. a2 G8 Pcase 2: //东西向黄灯闪烁,绿灯关闭
. Q! j* X5 D( H9 B' k0 uDelayMS(300);
$ _' ?) f% @* n: |4 W2 M* C" G& eYELLOW_A=~YELLOW_A;GREEN_A=1;4 ?+ W3 u) F3 q
if(++Flash_Count!=10) return; //闪烁 5 次
) z+ _6 x; A% N4 HFlash_Count=0;) L, e# u. ~9 Q: B' v% I k5 h- Y3 W
Operation_Type=3;8 M2 J/ G) _; p& s' w1 R
break;
# T/ Z6 B0 J: G# b6 v- N# ocase 3: //东西向红灯,南北向绿灯亮% p, P% W. B y
RED_A=0;YELLOW_A=1;GREEN_A=1;5 r3 }' U- |, a+ z5 {9 b
RED_B=1;YELLOW_B=1;GREEN_B=0;$ @- Q; L) c& I: C Z2 K' F
DelayMS(2000);
$ _6 O4 I1 y/ Q1 o8 L% z6 m8 b8 SOperation_Type=4;
" V1 e- p' {( V6 N0 hbreak;4 I8 n" ?* S2 j( j2 C
case 4: //南北向黄灯闪烁 5 次
9 X2 y# O5 e5 u1 |DelayMS(300);8 o6 c+ _ V; R
YELLOW_B=~YELLOW_B;GREEN_B=1;( M9 |/ t7 G7 g' ?1 H( b
if(++Flash_Count!=10) return;
, j% W4 D& E4 I/ c0 WFlash_Count=0;! ^0 V: n% x, Z/ { I: e
Operation_Type=1;' R. o: K6 q# M5 R- D
}
6 S" c! S+ B; x9 Q8 o}
7 x( [: L- ?# Y- T$ n! L# I//主程序7 A/ n' L, D7 v9 C
void main()/ {* h+ k5 ?( e6 Z1 e) X
{
; ]5 ^' f3 b$ a4 Nwhile(1) Traffic_Light();0 f+ N# `2 ?; i; R0 N7 S) @
}
- \; @' K( [* A) Y; ]6 I r4 ]' O/ F/ h3 H
, \0 M( w3 q/ a* ]& P: S% }6 i* a
: a8 R# d9 Q; A# O6 I( | |
|