51单片机十字路口交通灯课程设计

原理图

51单片机十字路口交通灯课程设计
六车道十字路口交通控制原理图,图中有四个不同颜色的箭头,代表四个不同的状态。需要使用51单片机实现交通灯及人行道灯。

开发工具

  • Protues7
  • Keil4

设计图

51单片机十字路口交通灯课程设计

设计图分析

使用元件

名称 数量
AT89C51 1
RESPACK-8 1
74HC573 8
NOT 8
AND_3 4
TRAFFIC LIGHTS 20
7SEG-MPX4-CC 4

设计思想

  1. 每个路口有三个红绿灯模块,分别代表左转,直行,右转,高电平时亮灯,低电平熄灭
  2. 对于的红绿灯模块下的数码管显示倒计时,当对应的红绿灯要变灯时显示倒计时提醒驾驶员注意
  3. 单片机引脚直接控制红绿灯和数码管,人行道灯采用逻辑门控制,当面前的路口都是红灯时才为绿灯
  4. 全部路口的时间是一样的,到时间就切换到下一个状态

代码

#include <reg51.h>
#define uchar unsigned char
#define RR 0x24 //一红一红
#define RG 0x0c //一红一绿
#define GR 0x21 //一绿一红
#define GG 0x09 //一绿一绿
#define LOCK(num) A##num = 1; A##num = 0 //LOCK(8)意为 A8 = 1; A8 = 1; 锁存器一开一关
#define START 127
#define GAP 30

sbit A8 = P1^0;
sbit A9 = P1^1;
sbit A10 = P1^2;
sbit A11 = P1^3;
sbit A12 = P1^4;
sbit A13 = P1^5;
sbit A14 = P1^6;
sbit A15 = P1^7;

uchar mtable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x27,0x7f,0x6f};

char sec = START, cnt = 0;

void delay(unsigned int ms);
void status1();
void status2();
void status3();
void status4();
void inittimer();

void main(){
	inittimer();	//打开定时器
	while(1){
		status1();
		status2();
		status3();
		status4();
	}
}

void inittimer() {
	TMOD = 0X01;
 	TL0 = 0xb0;
	TH0 = 0x3c;
	ET0 = 1; EA = 1;
	TR0 = 1;
}

void status1(){
	while( START-GAP < sec && sec <= START ){
		while( START-GAP+2 <= sec && sec <= START-GAP/2 ){
			P0 = mtable[sec-(START-GAP+1)];
			P3 = ~(0x31); LOCK(14);
			P3 = ~(0x07); LOCK(15);
			delay(100);
			P3 = 0xff; LOCK(14); LOCK(15);
		}
		//点灯顺序为西路口开始逆时针选装
		P2 = RR; LOCK(8);
		P2 = RG; LOCK(9);
		P2 = RR; LOCK(10);
		P2 = ( sec <= START-GAP+2 ) ? 0x22 : GR; LOCK(11);
		P2 = RR; LOCK(12);
		P2 = ( sec <= START-GAP+2 ) ? 0x12 : GG; LOCK(13);
	}
}

void status2(){
	while( START-2*GAP < sec && sec <= START-GAP ){
		while( START-2*GAP+2 <= sec && sec <= (START-GAP)-GAP/2 ){
			P0 = mtable[sec-(START-2*GAP+1)];
			P3 = ~(0x08); LOCK(14);
			P3 = ~(0x3e); LOCK(15);
			delay(100);
			P3 = 0xff; LOCK(14); LOCK(15);
		}
		P2 = GR; LOCK(8);
		P2 = ( sec <= START-2*GAP+2 ) ? 0x14 : RG; LOCK(9);
		P2 = RR; LOCK(10);
		P2 = ( sec <= START-2*GAP+2 ) ? 0x14 : RG; LOCK(11);
		P2 = ( sec <= START-2*GAP+2 ) ? 0x22 : GR;	LOCK(12);
		P2 = RR; LOCK(13);
	}
}


void status3(){
	while( START-3*GAP < sec && sec <= START-2*GAP ){
		while( START-3*GAP+2 <= sec && sec <= (START-2*GAP)-GAP/2 ){
			P0 = mtable[sec-(START-3*GAP+1)];
			P3 = ~(0x07); LOCK(14);
			P3 = ~(0x31); LOCK(15);
			delay(100);
			P3 = 0xff; LOCK(14); LOCK(15);
		}
		P2 = ( sec <= START-3*GAP+2 ) ? 0x22 : GR; LOCK(8);
		P2 = RR; LOCK(9);
		P2 = ( sec <= START-3*GAP+2 ) ? 0x12 : GG; LOCK(10);
		P2 = RR; LOCK(11);
		P2 = RG; LOCK(12);
		P2 = RR; LOCK(13);
	}
}

void status4(){
	while( START-4*GAP < sec && sec <= START-3*GAP ){
		while( START-4*GAP+2 <= sec && sec <= (START-3*GAP)-GAP/2 ){
			P0 = mtable[sec-(START-4*GAP+1)];
			P3 = ~(0x3e); LOCK(14);
			P3 = ~(0x08); LOCK(15);
			delay(100);
			P3 = 0xff; LOCK(14); LOCK(15);
		}
		P2 = ( sec <= START-4*GAP+2 ) ? 0x14 : RG; LOCK(8);
		P2 = ( sec <= START-4*GAP+2 ) ? 0x22 : GR; LOCK(9);
		P2 = RR; LOCK(10);
		P2 = GR; LOCK(11);
		P2 = ( sec <= START-4*GAP+2 ) ? 0x14 : RG; LOCK(12);
		P2 = RR; LOCK(13);
	}
}

void delay(unsigned int ms) {
	unsigned int i,j;
	for(i=0;i<ms;i++);
		for(j=0;j<150;j++);
}

void time_ler() interrupt 1 {
	TL0=0xb0; TH0=0x3c;
	cnt++;
	if( cnt == 20 ){
		cnt = 0;
		sec--;
		if( sec == START-4*GAP ){	//倒计时结束重置sec 回到status1
			sec = START;
		}
	}
}

资源

DSN文件
https://share.weiyun.com/ggUgDg9T

 

上一篇:单片机:K1 K2控制流水灯0 1定时开关 K3 K4控制流水灯5 6定时开关 同时两个定时


下一篇:延时函数、数码管显示头文件(单片机)