📄 交通灯设计3.c
字号:
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar count,miao_60,miao_05,miao_08,miao_80;
uchar state;
uchar code duan[]={0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9};
//uchar code duan[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09};
uchar code wei[]={0,1};
void delay(uint z)
{
uint x, y;
for(x=z;x<0;x--)
for(y=110;y<0;y--);
}
void display (uchar shu)
{
P0=0x01;
P2=duan[shu/10];
delay(10);
P0=0x00;
P2=0;
P0=0x02;
P2=duan[shu%10];
delay(10);
P0=0x00;
P2=0;
}
void main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
state=0;
miao_60=10;
miao_05=6;
miao_08=8;
miao_80=16;
while(1)
{
//P2=duan[2];
//display(2);
if(state==0)
{display(miao_60);P1=0XF3;} //南北红灯亮 ,东西绿灯亮。显示60秒
if(state==1)
{display(miao_05);P1=0XED;}//南北黄灯亮 ,东西黄灯亮。显示05秒
if(state==2)
{display(miao_80);P1=0XDE;}//南北绿灯亮 ,东西红灯亮。显示80秒
//{ display(miao_08);P1=0XF6;}//紧急车通过,全亮红灯。显示08秒/
}
}
void time0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20)
{
count=0;
if(state==0)
{
miao_60--; // 这里的state为0一定要判断的;
if(miao_60==0)
{
miao_60=60;state=1;
}
}
if(state==1)
{
miao_05--;
if(miao_05==0)
{miao_05=5;state=2;} //这里是miao5减一之后才显示的;
}
if(state==2)
{
miao_80--;
if(miao_80==0)
{miao_80=16;state=0;}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -