⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 交通灯设计2.c

📁 交通灯程序
💻 C
字号:
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned  int
sbit green1=P1^0;
sbit yellow1=P1^1;
sbit red1=P1^2;

uchar count,miao_60,miao_5,flag,c1_flag,count1,flash;
uchar miao_18,state;

uchar code duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

void delay(uint z)
{
	uint x, y;
	for(x=z;x<0;x--)
		for(y=110;y<0;y--);
}

void display(uchar shu) 
{	 
	 P3=0xfe;
	 P2=duan[shu/10];
	 delay(2);
	 P2=0;

	 P3=0xfd;
	 P2=duan[shu%10];
	 delay(2);
	 P2=0;
}
void main()
{	
	TMOD=0x01;
	TH0=(65536-50000)/256;
	TL0=(65536-50000)%256;
	EA=1;
	ET0=1;
	TR0=1;

	miao_18=5;
	miao_5=6;
	miao_60=7;

	while(1)
	{
		if(state==0)
		 {display(miao_18);green1=0;red1=1;yellow1=1;}

		if(state==1)//delay(500);
		 {display(miao_5);yellow1=0;green1=1;red1=1;}

		if(state==2)
		 {display(miao_60);red1=0;yellow1=1;green1=1;}
	 }
}

void time0() interrupt 1
{
	count++;
	if(count==5)
	{
		count=0;

		if(state==0)miao_18--;	 //	这里的state为0一定要判断的;
		if(miao_18==0)
			{miao_18=5;state=1;}

		if(state==1)miao_5--;
		if(miao_5==0)
		   {miao_5=6;state=2;} //这里是miao5减一之后才显示的;

		if(state==2)miao_60--;
		if(miao_60==0)
		    {miao_60=7;state=0;}
		
	}
	TH0=(65536-50000)/256;
	TL0=(65536-50000)%256;
}

/*void time1() interrupt 3
{
	count1++;
	flash=~flash;
	if(count1==5)
	{
		count1=0;
		c1_flag++;
		if(c1_flag==6)
		{
			c1_flag=0;
			flag=1;
			miao_60=0;
			ET1=0;
		}
	}
	TH1=(65536-50000)/256;
	TL1=(65536-50000)%256;
}*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -