基于51单片机的交通灯实验.docxVIP

  • 10
  • 0
  • 约1.94千字
  • 约 8页
  • 2020-08-30 发布于江苏
  • 举报
程序 #includereg52.h #define uint unsigned int #define uchar unsigned char sbit DX_R=P1^0; //东西方向红黄绿灯 sbit DX_Y=P1^1; sbit DX_G=P1^2; sbit NB_R=P1^3; //南北方向 sbit NB_Y=P1^4; sbit NB_G=P1^5; uchar code du_code[]={~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,~0x7F,~0x6F}; // 0~9( 共阳数码管 ) uchar num; uchar S20=20, S15=15, S5=5; bit flag=0; void delayms(uint x) //延时程序 { uint y; for(;x0;x--) for(y=110;y0;y--); } void initial() //定时器 50ms { TMOD=0x01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1; ET0=1; TR0=1; } void state1() { while(S20) //东西方向红灯亮 25 秒。南北绿灯两 20 秒 { NB_R=1; NB_Y=1; NB_G=0; DX_R=0; DX_Y=1; DX_G=1; if(flag==1) { flag=0; S20--; } P2=0x01; //南北数码管个位 P0=du_code[S20/10]; delayms(2); P2=0x02; //南北数码管十位 P0=du_code[S20%10]; delayms(2); P2=0x04; //东西 P0=du_code[(S20+5)/10]; delayms(2); P2=0x08; P0=du_code[(S20+5)%10]; delayms(2); } S20=20; } void state2() //东西红灯闪烁,南北黄灯亮 { while(S5) { NB_R=1; DX_R=0; NB_G=1; DX_Y=1; DX_G=1; if(flag==1) { NB_Y=~NB_Y; flag=0; S5--; } P2=0x01; P0=du_code[S5/10]; delayms(2); P2=0x02; P0=du_code[S5%10]; delayms(2); P2=0x04; P0=du_code[S5/10]; delayms(2); P2=0x08; P0=du_code[S5%10]; delayms(2); } S5=5; } void state3() { while(S15) { NB_R=0; NB_Y=1; NB_G=1; DX_R=1; DX_Y=1; DX_G=0; if(flag==1) { flag=0; S15--; } P2=0x01; P0=du_code[(S15+5)/10]; delayms(2); P2=0x02; P0=du_code[(S15+5)%10]; delayms(2); P2=0x04; P0=du_code[S15/10]; delayms(2); P2=0x08; P0=du_code[S15%10]; delayms(2); } S15=12; } void state4() { while(S5) { NB_Y=1; NB_G=1; DX_R=1; NB_R=0; DX_G=1; if(flag==1) { DX_Y=~DX_Y; flag=0; S5--; } P2=0x01; P0=du_code[S5/10]; delayms(2); P2=0x02; P0=du_code[S5%10]; delayms(2); P2=0x04; P0=du_code[S5/10]; delayms(2); P2=0x08; P0=du_code[S5%10]; delayms(2); } S5=5; } void main() { initial(); while(1) { state1(); state2(); state3(); state4(); } } void timer0() interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; num++; if(num==20) //20*50MS=1S { num=0; flag=1; } }

文档评论(0)

1亿VIP精品文档

相关文档