|
5-交通燈.png (13.05 KB, 下載次數(shù): 34)
下載附件
2018-6-21 09:03 上傳
5-1交通燈.png (11.54 KB, 下載次數(shù): 27)
下載附件
2018-6-21 09:03 上傳
/ L1 k5 h# `% b2 t& W
#include<reg51.h>" y: W1 |+ C2 D, i* p; n7 O
#define uchar unsigned char- n# k8 D0 O$ \3 [0 N$ }
#define uint unsigned int
* ?! S2 w7 f- n/ ]sbit RED_A=P0^0; //東西向燈4 B% n1 \" m( z3 N7 v' X. k6 F
sbit YELLOW_A=P0^1;
{0 r- G9 x+ c" h0 w* S0 gsbit GREEN_A=P0^2;
3 }& ?7 y4 A# n! Z. j r2 u% x+ Vsbit RED_B=P0^3; //南北向燈/ A% ]1 e% Z4 t
sbit YELLOW_B=P0^4;2 `! y$ K E) C4 p: `" Y% d
sbit GREEN_B=P0^5;/ h! G' q$ ~- o Y) f+ b: {' |
uchar Flash_Count=0,Operation_Type=1; //閃爍次數(shù),操作類型變量
) Y o: V" i2 g! n' M: M# J
; X9 C+ A( p5 f6 A- H//延時(shí)' l0 X. `0 h0 i
void DelayMS(uint x)# X/ @# c% X& c( j" |2 ~
{6 k3 V4 c5 ]/ v& `
uchar i;
# u8 s6 I' q4 P5 w; }4 P2 awhile(x--) for(i=0;i<120;i++);$ L8 T% ?& R' r: ?! e8 a; V" W
}
3 }; t; ]' [& D: q//交通燈切換
) Z. U! U0 s3 D6 t) E' a6 b) ]void Traffic_Light()9 l7 }3 g) B4 o
{
. A' H" [! p- F& [switch(Operation_Type)$ |) f2 n2 [5 L9 W2 o/ z5 L
{* G* w% p* `( A% ?* U- ^
case 1: //東西向綠燈與南北向紅燈亮
8 K( m, u0 ^) F W: a: e P4 VRED_A=1;YELLOW_A=1;GREEN_A=0;5 _4 _8 Z2 ]. G: ]" T i
RED_B=0;YELLOW_B=1;GREEN_B=1;
, p5 U! T2 D+ S% y- ^: IDelayMS(2000);
; ~$ }$ h1 k* E2 J* H; WOperation_Type=2;7 H7 X( {' U) z# W8 N9 N
break;
! F& o$ Q8 ^- g9 hcase 2: //東西向黃燈閃爍,綠燈關(guān)閉6 B! v; c+ m5 _' R9 y: B; z$ Y
DelayMS(300);
, J3 l7 G% c% O& o7 ]+ cYELLOW_A=~YELLOW_A;GREEN_A=1;, v1 I3 J! ]- y8 f/ L( B0 \7 P
if(++Flash_Count!=10) return; //閃爍 5 次
' w; E$ V }: D- |7 i1 t2 ^6 eFlash_Count=0;* X, @$ Y0 s: Y, F
Operation_Type=3;. \, S; _ X- `3 F/ }& d
break;
8 `% E6 A6 n: ^0 Ycase 3: //東西向紅燈,南北向綠燈亮& v% q e+ Y6 L' i; E
RED_A=0;YELLOW_A=1;GREEN_A=1;6 n$ i5 h# z* c5 Q
RED_B=1;YELLOW_B=1;GREEN_B=0;
3 t" l+ m) f. v( ]. JDelayMS(2000);
8 F/ q0 Y5 M' g; ]" i( Y2 qOperation_Type=4;0 l5 S" z, ~1 Q3 J! C
break;
+ g+ v- s0 Z% a* T Acase 4: //南北向黃燈閃爍 5 次" _6 h; L. \) L- D
DelayMS(300);
6 `; f/ G" e% z( CYELLOW_B=~YELLOW_B;GREEN_B=1;
0 @1 L# F4 m" f, ~' s7 cif(++Flash_Count!=10) return;: a4 r0 C% m5 T2 l+ G7 l3 G3 ~, K# r
Flash_Count=0;- N+ ]4 z0 w1 R! K! u( a1 {
Operation_Type=1;
9 H; \* T8 t2 v( E}3 {% Z4 T8 }' ^( F
}9 j5 ^/ V- m# L1 h4 U4 Q
//主程序
+ E: X/ k4 D% Wvoid main()+ D! ]0 \2 q c7 g
{. Q( t( s; E7 a
while(1) Traffic_Light();
: }8 _1 @$ w3 G/ b1 W2 ]1 w} 7 b/ U& m b) h. q
0 t; O: W) t; w- B- C( K) R
) n: B+ x u) p7 t
0 i$ p) n+ h9 G1 E8 Y( X |
|