|
, R9 L5 m2 T) l( e#include<reg51.h>/ T) w+ N! L8 a1 O- a1 \6 B$ v
#define uchar unsigned char* t7 r8 _& b7 j
#define uint unsigned int% `+ i V" l; X; @: s9 ~( C E
sbit RED_A=P0^0; //东西向灯1 c6 \+ F# S7 k6 v% E! h
sbit YELLOW_A=P0^1;! B# l8 v7 ^- n- V3 G5 X
sbit GREEN_A=P0^2;
2 P7 m, g& u: l0 \3 U; [* u% osbit RED_B=P0^3; //南北向灯
% b. E3 e/ Z' q: D6 r/ ] }sbit YELLOW_B=P0^4;
, w/ O- K. P8 c+ z) bsbit GREEN_B=P0^5;
! b5 n8 a& u% u; ?& Q2 E, L9 M/ fuchar Flash_Count=0,Operation_Type=1; //闪烁次数,操作类型变量 ! X+ R; c O- M K" V# b. F- I
D6 i/ M* U( j! F, T8 r/ J, \//延时2 S7 U4 j5 z$ r* {% d
void DelayMS(uint x)- o7 }# z* \% |) j
{# k; Y6 x9 y8 _ c3 }& `( ~
uchar i;2 a- |( m0 A2 U [) A
while(x--) for(i=0;i<120;i++); a! [" ^- t p6 v) {. x
}
2 {/ ?) g& Y% Z% @; C2 |: ~//交通灯切换
- G- f1 Y8 I' U6 g. `void Traffic_Light()' Q. I7 |+ S0 a2 n
{3 M, M3 d. H8 m- K
switch(Operation_Type)
/ D& B# r6 G% M. E* L{
( X; A3 \+ ^9 i" P! f. Ecase 1: //东西向绿灯与南北向红灯亮
+ ~" o* f1 U. \7 V+ a4 c% xRED_A=1;YELLOW_A=1;GREEN_A=0;% t0 x2 U' v# v, U _
RED_B=0;YELLOW_B=1;GREEN_B=1;9 O1 B' m& p d
DelayMS(2000);1 n# E6 v' J( l0 o5 e
Operation_Type=2;
- t7 g2 o4 ]7 @8 b; b# {( o; Cbreak;
a- J6 e9 [; u6 V1 Mcase 2: //东西向黄灯闪烁,绿灯关闭5 K# Q$ l; Y0 e: m+ ^& l1 T
DelayMS(300);$ e6 w/ { \6 ]# T- b+ U/ B( q
YELLOW_A=~YELLOW_A;GREEN_A=1;
5 Q6 T0 K J- T, Nif(++Flash_Count!=10) return; //闪烁 5 次6 d/ q# e! |6 F
Flash_Count=0;% J3 ^) ~4 t. y4 s" ]
Operation_Type=3;
* p7 ]( u8 y* Z1 t! x6 y# P1 ?break;
: O5 K5 f" j- bcase 3: //东西向红灯,南北向绿灯亮
- \2 p8 Y' R! j( d8 P0 j/ bRED_A=0;YELLOW_A=1;GREEN_A=1;# T) m9 e2 |% B( g% k3 }* n3 I
RED_B=1;YELLOW_B=1;GREEN_B=0;
. x5 M- w d, E0 U% L8 w1 \' D5 ZDelayMS(2000);* t( b. a# C+ n. T; x( z
Operation_Type=4;: }1 m, a2 \7 `; p& J: X
break;
5 s- A8 i/ g; Y2 D4 Zcase 4: //南北向黄灯闪烁 5 次$ k, `' t U7 h$ S+ B4 _
DelayMS(300);, O8 p9 n2 S0 B- ]
YELLOW_B=~YELLOW_B;GREEN_B=1;
4 o& ~* D/ L$ @if(++Flash_Count!=10) return;
0 C8 x9 O# _0 h" h( P! fFlash_Count=0;
' |# s/ f( O2 ]: tOperation_Type=1;7 f. u# N8 d6 {6 H8 w, [& s
}' V0 \: D* M& w
}& Z& P% ]" {6 w, E& h
//主程序
8 `; ~; K! c; W6 a/ K( svoid main()
& y8 w# \& w8 U{& g8 d8 u, H2 u! U2 X$ ~4 V5 I
while(1) Traffic_Light();
4 q! z9 O, n. ?}
2 H5 p' u: c& ^. e. m' ^/ G/ ?% D) B; j
5 B" p8 e9 ^/ W1 t5 k0 c+ M d; e% U! e
|
|