|
: N1 O7 f7 s- A! A0 @5 P( `
#include<reg51.h>
' J) ?; a- |1 H1 m4 E( h; ]) W#define uchar unsigned char B7 P+ k% u, E/ _$ x
#define uint unsigned int) @. J) E! p. I
sbit RED_A=P0^0; //东西向灯: \8 f9 N1 a% A# L" y! V8 U
sbit YELLOW_A=P0^1;
?4 e9 U5 U1 V5 H M" p5 J& `sbit GREEN_A=P0^2;
7 c7 G# f1 h$ k2 b7 b2 bsbit RED_B=P0^3; //南北向灯
0 y& ?% d% S, V5 k: Rsbit YELLOW_B=P0^4;. O' h* e# s4 H: J6 Y
sbit GREEN_B=P0^5;
% Z$ z/ G( J: ~2 g0 p) I8 @! Iuchar Flash_Count=0,Operation_Type=1; //闪烁次数,操作类型变量 % w- G( M0 `0 O2 @( a: d+ M
5 p8 H- J) ^1 t//延时
$ Q, y% B: Y9 G% @0 i& w' b& y( J7 `# kvoid DelayMS(uint x)# t C* w1 W. \: N7 j: l( k
{
, I5 f& B# L! ^: Z$ m( A0 \uchar i;8 N3 L7 b" P6 o" h* ^4 B2 F' h
while(x--) for(i=0;i<120;i++);+ z2 Z2 e9 ?6 K3 Q3 v2 h
}
: q! @/ G( G. n7 g, [ j//交通灯切换
0 r, M# u4 r# ]' nvoid Traffic_Light()9 [" z0 u/ X& n
{8 _: H z$ `" M/ ~( f j
switch(Operation_Type)
$ H q' `7 B8 d/ Y1 o9 r4 @{) `/ x" W5 Z% x: a
case 1: //东西向绿灯与南北向红灯亮4 ^* s4 n j9 ]) g3 ^! ~6 U
RED_A=1;YELLOW_A=1;GREEN_A=0;
D: S/ y# Q2 C1 O9 w+ `6 @6 yRED_B=0;YELLOW_B=1;GREEN_B=1;: l5 L, I$ N% ~: t
DelayMS(2000);
' g2 U3 d1 {% ?7 b: s7 N$ JOperation_Type=2;) l5 D" `/ j, v9 U
break;, N6 k7 \$ \5 J; D' G! y, `
case 2: //东西向黄灯闪烁,绿灯关闭
5 [0 v$ G% O" n0 O; m- M- JDelayMS(300);. j8 C) j) S. p$ H3 S' W
YELLOW_A=~YELLOW_A;GREEN_A=1;4 M0 I$ N' c& j
if(++Flash_Count!=10) return; //闪烁 5 次
" n _$ u& A8 ~( V0 eFlash_Count=0;+ A: k/ u: u! Y$ M4 \6 P1 @
Operation_Type=3;9 |+ @+ X) \7 c4 U8 H5 ^4 Z! d
break;( r0 B7 z4 G8 ~. n- I; F# p
case 3: //东西向红灯,南北向绿灯亮
4 i9 Z/ v9 b0 d5 _RED_A=0;YELLOW_A=1;GREEN_A=1;1 G& g$ \6 F7 A! j) N8 v
RED_B=1;YELLOW_B=1;GREEN_B=0;& Y W8 w. v4 D% ^/ Z; @
DelayMS(2000);
! u6 ]" V/ |8 d8 Q# N: UOperation_Type=4;
( }" F \+ m. B! t# v) y D& ]break;# P7 K G& M0 x6 K5 R
case 4: //南北向黄灯闪烁 5 次
# W. G% {, Q# y* @0 FDelayMS(300);
$ D& w ?/ }% P/ SYELLOW_B=~YELLOW_B;GREEN_B=1;
5 v6 m8 w5 ` z0 o8 Z+ L3 F+ N6 aif(++Flash_Count!=10) return;* G& n& f d! J8 q$ k( j0 L
Flash_Count=0;
: O: o b) ^. I0 M# W) jOperation_Type=1;
4 Q5 w5 N0 X5 A}* X! g" N2 t2 B, Q# m1 ?
}* t6 [5 Y* x( C5 z/ y8 L
//主程序3 d' v' ^" N$ ?$ s. ^
void main()
5 I' K/ q: {+ S{
, p; A" s7 Q6 P2 Kwhile(1) Traffic_Light();
# U! n" s3 m& h6 N}
8 h G; |; K8 Y5 d2 K. F7 V( p; c
5 n3 T4 t1 z8 F. Y2 O0 `
" v% L g1 |& W7 Z' R5 K- Z
8 a$ X6 {, P* ^/ |" [ |
|