基于51单片机交通灯

Posted 一心向月

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于51单片机交通灯相关的知识,希望对你有一定的参考价值。

废话少说直接上图

https://pan.baidu.com/s/15BvnC4jLvHe2zWJLPi4qlg 提取码:azxy

#include"reg52.h"  //头文件

typedef unsigned char u8;
typedef unsigned int u16;

sbit P10=P1^0; //黄色灯
sbit P11=P1^1; //红色灯	   勇敢牛牛路前面3个灯
sbit P12=P1^2; //绿色灯

sbit P13=P1^3; //黄色灯
sbit P14=P1^4; //红色灯	   不怕困难路右面3个灯
sbit P15=P1^5; //绿色灯

sbit P16=P1^6; //黄色灯
sbit P17=P1^7; //红色灯	   勇敢牛牛路后面3个灯
sbit P30=P3^0; //绿色灯

sbit P31=P3^1; //黄色灯
sbit P32=P3^2; //红色灯	   不怕困难路左面3个灯
sbit P33=P3^3; //绿色灯

sbit P34=P3^4; //勇敢牛牛路直行按钮
sbit P35=P3^5; //不怕困难路直行按钮

u8 smg_display[8];
u8 code wala[8]=0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80; //数码管位选
u8 code duan[10]=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90; //数码管 0~9
u8 s=0;
/*************************************************
*********************延迟函数*********************
*************************************************/
void delay(u16 i)

	while(i--);

/*************************************************
********************定时器中断********************
*************************************************/
void Timer0Init(void)		//5毫秒@11.0592MHz

	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x00;		//设置定时初值
	TH0 = 0x28;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	ET0=1;
	EA=1;

/*************************************************
******************数码管显示函数******************
*************************************************/
void smg_buff()

	static u8 num=0;
	while(num<8)
	
		P2=~wala[num];
		P0=smg_display[num];
		num++;
		delay(200);
	
	if(num==8) num=0;

/*************************************************
******************交通灯显示函数******************
*************************************************/
void smg_led()

	if(s==70)
	
		s=0;
	
	if(s<30)
	
		smg_display[0]=~duan[(30-s)/10];	//勇敢牛牛路前面时间
		smg_display[1]=~duan[(30-s)%10];

		smg_display[2]=~duan[(35-s)/10];	//不怕困难路右面时间
		smg_display[3]=~duan[(35-s)%10];

		smg_display[4]=~duan[(30-s)/10];	//勇敢牛牛路后面时间
		smg_display[5]=~duan[(30-s)%10];

		smg_display[6]=~duan[(35-s)/10];	//不怕困难路左面时间
		smg_display[7]=~duan[(35-s)%10];

		P10=0;P11=0;P12=1;	//黄 红 绿	   勇敢牛牛路前面3个灯
		P13=0;P14=1;P15=0;	//黄 红 绿	   不怕困难路右面3个灯
		P16=0;P17=0;P30=1;	//黄 红 绿	   勇敢牛牛路后面3个灯
		P31=0;P32=1;P33=0;	//黄 红 绿	   不怕困难路左面3个灯		
	
	else if(s<35)
	
		smg_display[0]=~duan[(35-s)/10];    //勇敢牛牛路前面时间
		smg_display[1]=~duan[(35-s)%10];

		smg_display[2]=~duan[(35-s)/10];	//不怕困难路右面时间
		smg_display[3]=~duan[(35-s)%10];

		smg_display[4]=~duan[(35-s)/10];	//勇敢牛牛路后面时间
		smg_display[5]=~duan[(35-s)%10];

		smg_display[6]=~duan[(35-s)/10];	//不怕困难路左面时间
		smg_display[7]=~duan[(35-s)%10];

		P10=1;P11=0;P12=0;  //黄 红 绿	   勇敢牛牛路前面3个灯
		P13=0;P14=1;P15=0;	//黄 红 绿	   不怕困难路右面3个灯
		P16=1;P17=0;P30=0;	//黄 红 绿	   勇敢牛牛路后面3个灯
		P31=0;P32=1;P33=0;	//黄 红 绿	   不怕困难路左面3个灯	
	
	else if(s<65)
	
		smg_display[0]=~duan[(70-s)/10];	//勇敢牛牛路前面时间
		smg_display[1]=~duan[(70-s)%10];

		smg_display[2]=~duan[(65-s)/10];	//不怕困难路右面时间
		smg_display[3]=~duan[(65-s)%10];

		smg_display[4]=~duan[(70-s)/10];	//勇敢牛牛路后面时间
		smg_display[5]=~duan[(70-s)%10];

		smg_display[6]=~duan[(65-s)/10];	//不怕困难路左面时间
		smg_display[7]=~duan[(65-s)%10];

		P10=0;P11=1;P12=0;	//黄 红 绿	   勇敢牛牛路前面3个灯
		P13=0;P14=0;P15=1;	//黄 红 绿	   不怕困难路右面3个灯
		P16=0;P17=1;P30=0;	//黄 红 绿	   勇敢牛牛路后面3个灯
		P31=0;P32=0;P33=1;	//黄 红 绿	   不怕困难路左面3个灯	
	
	else
	
		smg_display[0]=~duan[(70-s)/10];	//勇敢牛牛路前面时间
		smg_display[1]=~duan[(70-s)%10];

		smg_display[2]=~duan[(70-s)/10];	//不怕困难路右面时间
		smg_display[3]=~duan[(70-s)%10];

		smg_display[4]=~duan[(70-s)/10];	//勇敢牛牛路后面时间
		smg_display[5]=~duan[(70-s)%10];

		smg_display[6]=~duan[(70-s)/10];	//不怕困难路左面时间
		smg_display[7]=~duan[(70-s)%10];

		P10=0;P11=1;P12=0;	//黄 红 绿	   勇敢牛牛路前面3个灯
		P13=1;P14=0;P15=0;	//黄 红 绿	   不怕困难路右面3个灯
		P16=0;P17=1;P30=0;	//黄 红 绿	   勇敢牛牛路后面3个灯
		P31=1;P32=0;P33=0;	//黄 红 绿	   不怕困难路左面3个灯	
	


/*************************************************
*********************直行函数*********************
*************************************************/
void green()

	if(P34==0)
	
		TR0 = 0;
		smg_display[0]=0x00;	//勇敢牛牛路前面时间
		smg_display[1]=0x00;

		smg_display[2]=0x00;	//不怕困难路右面时间
		smg_display[3]=0x00;

		smg_display[4]=0x00;	//勇敢牛牛路后面时间
		smg_display[5]=0x00;

		smg_display[6]=0x00;	//不怕困难路左面时间
		smg_display[7]=0x00;

		P10=0;P11=0;P12=1;	 //黄 红 绿	   勇敢牛牛路前面3个灯
		P13=0;P14=0;P15=0;	 //黄 红 绿	   不怕困难路右面3个灯
		P16=0;P17=0;P30=1;	 //黄 红 绿	   勇敢牛牛路后面3个灯
		P31=0;P32=0;P33=0;	 //黄 红 绿	   不怕困难路左面3个灯	
	
	else
	
		TR0 = 1;
	
	if(P35==0)
	
		TR0 = 0;
	 	smg_display[0]=0x00;	//勇敢牛牛路前面时间
		smg_display[1]=0x00;

		smg_display[2]=0x00;	//不怕困难路右面时间
		smg_display[3]=0x00;

		smg_display[4]=0x00;	//勇敢牛牛路后面时间
		smg_display[5]=0x00;

		smg_display[6]=0x00;	//不怕困难路左面时间
		smg_display[7]=0x00;

		P10=0;P11=0;P12=0;	 //黄 红 绿	   勇敢牛牛路前面3个灯
		P13=0;P14=0;P15=1;	 //黄 红 绿	   不怕困难路右面3个灯
		P16=0;P17=0;P30=0;	 //黄 红 绿	   勇敢牛牛路后面3个灯
		P31=0;P32=0;P33=1;	 //黄 红 绿	   不怕困难路左面3个灯
	
	else
	
		TR0 = 1;
	

/*************************************************
*********************主函数*********************
*************************************************/
void main()

	P1=0x00;P3=0x00;
	P34=1;P35=1;
	Timer0Init();
	while(1)
	
		smg_led();
		green();	
		smg_buff();	
	

/*************************************************
********************中断服务函数*******************
*************************************************/
void timer0() interrupt 1

	static u8 i=0;
	i++;
	if(i==200)	   //定时1S
	
		i=0;
		s++;
	


以上是关于基于51单片机交通灯的主要内容,如果未能解决你的问题,请参考以下文章

基于51单片机交通灯

c51单片机c语言交通灯的程序

基于51单片机的交通信号灯设计

基于51单片机的交通信号灯设计

基于51单片机的交通灯Proteus仿真设计_带南北管控+东西管控+串口控制

基于51单片机的交通灯Proteus仿真设计_带紧急开关+可调时间(源码+仿真+报告)