|
H4 N& z: L# e+ Y% S) P9 f( K% e8 P#include 5 Z7 _+ ?9 E8 j3 i0 G! T7 W#define uchar unsigned char - y7 h, E) _) m' |8 h#define uint unsigned int " o3 x; z7 r: ^2 dsbit RED_A=P0^0; //东西向灯( a3 o8 T9 l4 j8 `! Z0 [+ b sbit YELLOW_A=P0^1; / w2 d7 z5 H+ P0 ^) I! Vsbit GREEN_A=P0^2; 0 g' H" S1 U. X* e4 _2 csbit RED_B=P0^3; //南北向灯; S* s4 @& M* O' K/ b9 a( H, o sbit YELLOW_B=P0^4;# k0 C% S3 h' [, b sbit GREEN_B=P0^5; 1 l3 |4 W7 f; nuchar Flash_Count=0,Operation_Type=1; //闪烁次数,操作类型变量6 K) ~- {1 x) X8 s X% h/ I/ T7 i& \& a //延时, u% \/ u. K4 N# f void DelayMS(uint x) ; |. @0 i$ M/ V# e. ], U% u0 A5 T{% C: Q2 S; ^5 {" v' t: w$ f& K uchar i;4 d5 J5 s5 e$ Q& J* M- d0 S: i while(x--) for(i=0;i<120;i++);* R2 z1 D, n8 Y5 `! G# F# r' k } 3 y( ~8 L0 R0 k) _: \//交通灯切换 7 p" Q+ |8 B2 i: _5 g; Uvoid Traffic_Light() 1 S6 T# U5 m/ y* n2 j$ T3 I" J{( h& M: Z$ b5 D" H8 z3 ~' J switch(Operation_Type)4 b/ h1 ~3 E6 P$ n" v { / I) ]9 {" {& J# ?, pcase 1: //东西向绿灯与南北向红灯亮3 q; b3 ]! R" ^ h i$ b RED_A=1;YELLOW_A=1;GREEN_A=0;5 m& [/ R! |) o/ Y/ @( x- p RED_B=0;YELLOW_B=1;GREEN_B=1;9 o- I8 g. J& Z. o" u4 \5 u1 o3 s8 R DelayMS(2000); : ]5 h/ U$ ^4 |/ ^4 \6 V7 @) J7 OOperation_Type=2; , t& P! y9 U: Ebreak;& A5 d; W7 |& F4 Q case 2: //东西向黄灯闪烁,绿灯关闭3 `2 A% B$ S8 r0 |5 s7 U DelayMS(300); : p* Y9 J" v; t* q( c- k: eYELLOW_A=~YELLOW_A;GREEN_A=1; 3 z! \+ J0 N E' h3 Y) a$ _if(++Flash_Count!=10) return; //闪烁 5 次 * Z3 K. W& Y1 S- }- MFlash_Count=0;" B8 [% F1 E- L+ v# V Operation_Type=3; # {4 L l/ p2 Ebreak; 5 l( [2 Q4 U4 ?0 p: r- i% D7 pcase 3: //东西向红灯,南北向绿灯亮; T) ?+ X; U6 h" ?8 W RED_A=0;YELLOW_A=1;GREEN_A=1; 5 K" V; T+ P0 tRED_B=1;YELLOW_B=1;GREEN_B=0; - h% J! R* v% O8 f9 M' A9 `0 lDelayMS(2000);3 A2 I( g) t2 ?8 I Operation_Type=4;8 p( V( z% W K a- k+ q9 Y break; 0 z7 n& z% Q3 x0 o# s2 T8 ucase 4: //南北向黄灯闪烁 5 次 5 Z' V& h1 B7 aDelayMS(300); 1 t" {0 F( v# K0 b1 W0 oYELLOW_B=~YELLOW_B;GREEN_B=1;$ j1 `; Q. L: G if(++Flash_Count!=10) return; " o: ], t% N' c. }. T% ^Flash_Count=0; ( h& K8 |5 J9 ^; P% hOperation_Type=1; # G1 U9 h4 F7 I( [* b8 C} 9 d6 s" D$ U: d4 E} + O6 t* a" q+ M* s2 ^& x//主程序S8 m- N. f& d+ o void main()8 s1 t" g7 b* n |' B" T {2 @ T) |4 `8 q$ x6 E+ n while(1) Traffic_Light();8 T' }/ H i& a3 |. L } / o- B- k$ ~; [: i' j' P- a 4 K6 E# G; v1 T7 W 7 C0 l. M A) ]+ H% q/ j# [# @0 K* }2 n/ D7 \- n% \
|
|