五岔路口的交通灯设计
1、道路包含2条主干道和1条支干道
2、设计合理的交通灯规则,控制道路通行
3、数码管显示倒计时时间
4、通行时间固定,可在程序内修改
#include<reg51.h>
#include "74hc595.h"
#define uchar unsigned char
#define uint unsigned int
sbit smg1=P2^0;//数码管引脚
sbit smg2=P2^1;
sbit smg3=P2^2;
sbit smg4=P2^3;
sbit smg5=P2^4;
sbit smg6=P2^5;
sbit smg7=P2^6;
sbit smg8=P2^7;
sbit smg9=P3^0;
sbit smg10=P3^1;
sbit smg11=P3^2;
sbit smg12=P3^3;
uchar num1=0xff,num2=0xff,num3=0xff;//LED控制
//数码管编码
uchar code tabel[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
uchar sec=0; //定时
uchar miao1=0,miao2=0,miao3=0,miao4=0,miao5=0,miao6=0;//时间
uchar mode=0; //路灯工作顺序
uchar zhi1=25,zuo1=10,zhi2=20,zuo2=10,zhi3=15;//时间设置
void set_led()//设置路灯
{
Hc595SendByte(num1);
Hc595SendByte(num2);
Hc595SendByte(num3);
RCLK = 0;
_nop_();
_nop_();
RCLK = 1;
}
//1路口
void run1_1()
{
num3 |=0x01;
}
void run1_0()
{
num3 &=0xfe;
}
void left1_1()
{
num3 |=0x02;
}
void left1_0()
{
num3 &=0xfd;
}
void yellow1_1()
{
num3 |=0x04;
}
void yellow1_0()
{
num3 &=0xfb;
}
void red1_1()
{
num3 |=0x08;
}
void red1_0()
{
num3 &=0xf7;
}
//2路口
void run2_1()
{
num3 |=0x10;
}
void run2_0()
{
num3 &=0xef;
}
void left2_1()
{
num3 |=0x20;
}
void left2_0()
{
num3 &=0xdf;
}
void yellow2_1()
{
num3 |=0x40;
}
void yellow2_0()
{
num3 &=0xbf;
}
void red2_1()
{
num3 |=0x80;
}
void red2_0()
{
num3 &=0x7f;
}
//3路口
void left3_1()
{
num2 |=0x01;
}
void left3_0()
{
num2 &=0xfe;
}
void run3_1()
{
num2 |=0x02;
}
void run3_0()
{
num2 &=0xfd;
}
void right3_1()
{
num2 |=0x04;
}
void right3_0()
{
num2 &=0xfb;
}
void yellow3_1()
{
num2 |=0x08;
}
void yellow3_0()
{
num2 &=0xf7;
}
void red3_1()
{
num2 |=0x10;
}
void red3_0()
阅读全文