C51单片机C语言编程实现交通灯

设计一个交通灯控制器

使用keil4 和 proteus 8

安装包:

Proteus 8
链接:
https://pan.baidu.com/s/1Qd6pMuK_Lwf1f_zVScLuSQ
提取码:nlui

Keil4
链接:https://pan.baidu.com/s/1QPmBQZb442qk9XZ1xub8kw
提取码:obej

1)能显示十字路口东西、南北两个方向的红、黄、绿灯的指示状态。
(初始状态0为东西红灯,南北红灯。然后转状态1南北绿灯通车,东西红灯。延时T1秒后转状态2,南北绿灯闪2秒转黄灯,延时3秒,东西仍然红灯。再转状态3,东西绿灯通车,南北红灯。延时T1秒后转状态4,东西绿灯闪2秒转黄灯,延时3秒,南北仍然红灯。最后循环至状态1。)

2)用拨动开关K0~K7实现延时时间T1的设置,当K7闭合时,为T1的设置状态,K0~K6为设置时间(秒),此时东西、南北两个方向均显示红灯状态。当K7断开时为工作状态。绿灯的闪烁频率为1HZ。

3)在紧急状态下,可通过开关设置,使所有指示灯均为红灯状态。

#include<reg51.h>
#include <intrins.h>
#define red_red 0x36  		//东西红,南北红     绿黄红110 110
#define red_yellow 0x35		//东西红,南北黄		   110 101
#define red_green 0x33	    //东西红,南北绿	       110 011
#define green_red 0x1e	    //东西绿,南北红		   011 110
#define yellow_red 0x2e	    //东西黄,南北红		   101 110
sbit P3_0=P3^0;	 //	gewei
sbit P3_1=P3^1;	 //	shiwei
sbit P3_4=P3^4;	//加两秒
sbit P3_5=P3^5;	//减两秒
sbit P3_6=P3^6;	// ge
sbit P3_7=P3^7;	// shi
int t1=14;
int t;
int count;
unsigned char shu[]=
{
    
    0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};        

void delayls (){
    
       //延时1s函数
	int k,j;
	for( k=1000;k>0;k--)
	for(j=110;j>0;j--);
	_nop_();
}

void initialize()//初始化
{
    
    
	P1=red_red;
	delayls();
}

void delay(int ms)	 //延时函数ms
{
    
    
	int j,k;
	for(j=0;j<ms;j++)
		for(k=0;k<124;k++);
}

void add_sub()	//加减扫描
{
    
    
	if(P3_4!=1)			 //加两秒
	{
    
    
		delay(10);
		if(P3_4!=1)
		{
    
    
			while(P3_4!=1);
			count=count+2;;
		}
	}
	if(P3_5!=1)			 //减两秒
	{
    
    
		delay(10);
		if(P3_5!=1)
		{
    
    
			while(P3_5!=1);
			count=count-2;;
		}
	}
}

void writer(int count,int key)		  //数码管显示设置
{
    
    
	unsigned int j;
	int num;
	int gewei,shiwei,ge,shi;

	if(key==1){
    
    		//东西红灯
	num=count+3;}
	if(key==2)		//南北红灯
	{
    
    num=count;
	count=count+3;}
	if(key==3)		//黄灯倒计时
	{
    
    num=count;}
		
	gewei = count % 10;
	shiwei = count / 10 % 10;	
	ge =num % 10;
	shi	=num / 10 % 10;
		
	P3_0=0;P3_1=1;P3_6=1;P3_7=1;	
	P2 =shu[gewei];     //个位
	j=10;
	while(j--);
	P2=0x00;
	P3_0=1;
   	
	P3_1=0;
	P2=shu[shiwei];	  //十位
	j=10;
	while(j--);
	P2=0x00;
	P3_1=1;

	P3_6=0;	
	P2 =shu[ge];     //个位
	j=10;
	while(j--);
	P2=0x00;
	P3_6=1;

	P3_7=0;
	P2=shu[shi];	  //十位
	j=10;
	while(j--);
	P2=0x00;
	P3_7=1;
}

void into(void) interrupt 0   	   //紧急按钮
{
    
    
	P1=red_red;
}

void read(void)	interrupt 2  using 1   //时间设置
{
    
    
	t1=P0;
	P1=red_red;
}

void main(void)
{
    
    	
	initialize();  //初始状态
	TMOD=0X01;
	ET0=1;
	IT0=0; //电平触发
	IT1=0; //电平触发
	EA=1; //全局中断开
	EX0=1; //外部中断0开
	EX1=1;
	PT0=1;
	
	while(1)
	{
    
    
		//状态1
		P1=red_green;
		for(count=t1;count>2;count--)
		{
    
    
		 	t=1400;
			while(t--)
			{
    
    
				writer(count,1);
				add_sub();
			}
		}
	
		//状态2
		P1=0x37;//绿灯闪
		for(count=2;count>1;count--)
		{
    
    
		 	t=700;
			while(t--)
			{
    
    
				writer(count,1);
				add_sub();
			}
		}
		P1=red_green;
	    for(count=2;count>1;count--)
		{
    
    
		 	t=700;
			while(t--)
			{
    
    
				writer(count,1);
				add_sub();
			}
		}
		P1=0x37;//绿灯闪
		for(count=1;count>0;count--)
		{
    
    
		 	t=700;
			while(t--)
			{
    
    
				writer(count,1);
				add_sub();
			}
		}
		P1=red_green;
		for(count=1;count>0;count--)
		{
    
    
		 	t=700;
			while(t--)
			{
    
    
				writer(count,1);
				add_sub();
			}
		}
		P1=red_yellow;
		for(count=3;count>0;count--)
		{
    
    
			t=1400;
			while(t--)
			{
    
    
				writer(count,3);
				add_sub();
			}
		}
	
		//状态3
		P1=green_red;
		for(count=t1;count>2;count--)
		{
    
    
			t=1400;
			while(t--)
			{
    
    
				writer(count,2);
				add_sub();
			}
		}
	   
		//状态4
		P1=0x3e;
		for(count=2;count>1;count--)
		{
    
    
		 	t=700;
			while(t--)
			{
    
    
				writer(count,2);
				add_sub();
			}
		}
		P1=green_red;
		for(count=2;count>1;count--)
		{
    
    
		 	t=700;
			while(t--)
			{
    
    
				writer(count,2);
				add_sub();
			}
		}
		P1=0x3e;
		for(count=1;count>0;count--)
		{
    
    
		 	t=700;
			while(t--)
			{
    
    
				writer(count,2);
				add_sub();
			}
		}
		P1=green_red;
		for(count=1;count>0;count--)
		{
    
    
		 	t=700;
			while(t--)
			{
    
    
				writer(count,2);
				add_sub();
			}
		}
		P1=yellow_red;
		for(count=3;count>0;count--)
		{
    
    
			t=1400;
			while(t--)
			{
    
    
				writer(count,3);
				add_sub();
			}
		}
	}
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_45784099/article/details/118283251