|
; _- e; ?0 Z9 }; Q! O1 q3 Q#include. d" X! R6 {+ y4 ?9 x4 ] #define uchar unsigned char8 m: \+ z& P2 @$ h #define uint unsigned int0 k( m5 Z, [9 ~* _ sbit RED_A=P0^0; //东西向灯 + U& e0 f) v/ c" W7 H0 I! vsbit YELLOW_A=P0^1; 7 @9 ^9 R! J1 k H) Nsbit GREEN_A=P0^2; 5 \) d7 R4 I7 n, N$ csbit RED_B=P0^3; //南北向灯. O! ]' _. |4 L: ` sbit YELLOW_B=P0^4; 9 `3 t8 e# ^3 rsbit GREEN_B=P0^5; 1 `( j |; A& z# Q3 W4 iuchar Flash_Count=0,Operation_Type=1; //闪烁次数,操作类型变量 : `: K9 {! E2 w8 J; r( Q @ ) ` G& t" C4 W( Q$ b//延时( t i) }' y& L void DelayMS(uint x)7 b8 K& d5 b/ _# E% D3 n( K# n& n+ |8 _ {, @0 |) R8 D- O% s+ l! k uchar i; " R- F) q/ j1 _" k% }while(x--) for(i=0;i<120;i++); 1 u2 C1 t5 S( K S, P! Q' m} . U" L0 l) O B+ [//交通灯切换1 {% i% @1 {- g2 I void Traffic_Light() / N7 r& u) n2 S{! Y7 g, `- ]! X7 \7 } switch(Operation_Type)r+ U/ a) N- w( @9 s {$ e: T, v7 R, {# |% d! y case 1: //东西向绿灯与南北向红灯亮/ v x; p/ I: i0 N; I RED_A=1;YELLOW_A=1;GREEN_A=0; 2 a0 j x' a# z! Y$ X( f, g* ORED_B=0;YELLOW_B=1;GREEN_B=1; ; {' b/ i, G7 p/ s' W, FDelayMS(2000); - f* \3 m& ~6 V% s7 K) hOperation_Type=2; ) G% o5 S3 ^1 ~) @( Z. D" g5 z) Qbreak; . n1 n" J2 R7 {( ecase 2: //东西向黄灯闪烁,绿灯关闭; l, m/ n0 Z( q2 q' } DelayMS(300); 8 n% d" i( Z# S+ }. o7 J, q; xYELLOW_A=~YELLOW_A;GREEN_A=1;* k" C2 h- F/ O+ t6 y if(++Flash_Count!=10) return; //闪烁 5 次; l- {5 j1 a- D* p$ n E Flash_Count=0; 7 [; d( n2 v; P2 m7 y$ {2 pOperation_Type=3; & _- c9 T* ]1 i3 }& |: M7 k# {3 Z0 E3 lbreak;f% C {1 h2 S/ H; m case 3: //东西向红灯,南北向绿灯亮# S2 M4 |7 \! O9 q+ K RED_A=0;YELLOW_A=1;GREEN_A=1;. ?# ~% p0 E. d RED_B=1;YELLOW_B=1;GREEN_B=0;6 P- T+ ?2 e& l! X" v/ W7 r/ x DelayMS(2000);6 f% T3 \( c" b% Z3 T' ` Operation_Type=4;* M" v' h o3 T- \3 } break; # H* V' {, L5 [( }case 4: //南北向黄灯闪烁 5 次4 m6 \% n2 s* v* G s DelayMS(300); : z' ]/ p2 D4 K: M# t; ]( ~( ?) o1 F$ lYELLOW_B=~YELLOW_B;GREEN_B=1; , O3 Y7 F- C$ ?- W: Yif(++Flash_Count!=10) return;n+ ]7 {# w4 p( U, a4 e Flash_Count=0; 1 i) u/ ]8 X, C. qOperation_Type=1; & @$ o0 D; N! i. S' N3 b}- Q8 o& X2 y2 N7 P } - X+ F. ~6 U0 }- Z//主程序* C7 H( B! Y0 q4 n$ h void main() : b& Q1 O6 A8 H! }8 @{/ Y G- Z$ v8 E/ v5 Q1 s( A O5 z while(1) Traffic_Light();* A2 l9 V9 {) ^9 p ?5 ? }/ H, o1 x) A/ u9 K+ } $ l4 j7 `; @9 q t* o, O& \5 a' }6 s7 U
$ H5 b- I6 y" W2 l ( u; E* A; C# Q Y n7 q% N |
|