【单片机课设】十字路口交通灯的设计

目录

题目要求:

仿真截图:

 源代码:


题目要求:

模拟交通灯运行情况。南北绿灯亮30秒,南北黄灯亮3秒,东西红灯亮33秒;南北红灯亮33秒,东西绿灯亮30秒,东西黄灯亮3秒;要求数码管同步显示时间的倒计时,用定时器实现延时。


仿真截图:

仿真文件已经上传,下载或者照着上面画都可。用到proteus仿真! 


 源代码:

#include <reg51.H>

#define uchar unsigned char
#define uint unsigned int
 
uchar code table[]={	
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,
0xC9,0xFF,0x40}; //数码管段码表

//函数声名
void delay(uint x);
void display(uchar,uchar,uchar,uchar);		
void traffic();	

//变量定义
uchar num,num1,num2, 
shi1,ge1,shi2,ge2,
value1,value2,
value3,value4,
count1,count2,flag1,flag2; 
/**********************************************/	  
void main()
{	
	//初始化定时器0		
	TMOD=0x01;
	TL0 = 0xB0;		//设置定时初值
	TH0 = 0x3C;		//设置定时初值
	EA=1;
	ET0=1;
	TR0=1;

	value1=30;	//南北通行时间为30S	 
	value2=3;	  
	value3=30;	//东西通行时间为30S  
	value4=3;

	num1=value1; 
	num2=value2+value1;
	shi1=num1/10; //显示南北时间
	ge1=num1%10;
	shi2=num2/10; //显示东西时间
	ge2=num2%10;
	P1=0x41;

	while(1)
	{						    
		if(num==20)//定时1秒	 20  
		{
			num=0;
			num1--;	 //计时减1
			num2--;
			traffic();	//变灯函数				
			shi1=num1/10;  //显示南北时间
			ge1=num1%10;
			shi2=num2/10;  //显示东西时间
			ge2=num2%10;
		}
		display(shi1,ge1,shi2,ge2);//刷新数码管显示
	}
}

void traffic() 	//变灯函数
{
	if(num1==0){
			count1++;
			if(count1==1){
				P1=0x42;   //南北黄东西红
				num1=value2;
			}
			if(count1==2){
				num1=value3+value4;
				P1=0x14;  //南北红东西绿
			}
			if(count1==3){
				P1=0x41;
				num1=value4;
				count1=0;							
			}
	}
	if(num2==0){
			count2++;
			if(count2==1){ 
				//P1=0x14;
				num2=value3;
			}
			if(count2==2){
				P1=0x24;
				num2=value4;
			}
			if(count2==3){
				num2=value1+value2; 
				num1=value1;
				count2=0;
			}					
	}
}

void display(uchar shi1,uchar ge1,uchar shi2,uchar ge2)	
{		
		uchar temp;
		temp=P2;
		P2=0xfe;        //选通1位数码管
		P0=table[shi1];	//输出笔段数据
		delay(5);		//显示一会
		
		P2=0xfd;
		P0=table[ge1];
		delay(5);
	
		P2=0xfb;
		P0=table[shi2];
		delay(5);
		
		P2=0xf7;
		P0=table[ge2];
		delay(5);		
}

void delay(uint x)	 //1ms延时函数
{
	uint i,j;
	for(i=x;i>0;i--)
		for(j=110;j>0;j--);
}

void T0_time() interrupt 1 	//定时器中断
{
	TL0 = 0xB0;		//设置定时初值
	TH0 = 0x3C;		//设置定时初值
 
	num++;			//计时变量加1
}

源文件也已经上传,下载可用,也可直接CV上面的!


想要文件点击【这里】下载,也可以评论区找我要文件,免费的捏!!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

X_chengonly

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值