学习日志
姓名:虞蓓 日期:2018年6月29号
今日任务完成情况:单片机的交通灯显示
#include<reg51.h>
sbit duan = P2;
sbit wei = P1;
sbit led1 = P1^0; //30s 红
sbit led2 = P1^1; //30s 黄
sbit led3 = P1^2; //30s 绿
sbit led4 = P1^3; //20s 红
sbit led5 = P1^4; //20s 黄
sbit led6 = P1^5; //30s 绿
sbit wei = P1;
sbit led1 = P1^0; //30s 红
sbit led2 = P1^1; //30s 黄
sbit led3 = P1^2; //30s 绿
sbit led4 = P1^3; //20s 红
sbit led5 = P1^4; //20s 黄
sbit led6 = P1^5; //30s 绿
unsigned char code table_wei[] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f};
unsigned char code table_duan[] ={0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71};
unsigned char code table_duan[] ={0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71};
int num, j; //j 为 0 是东西路口,j 为 1 是南北路口
int time;
void display(int num)
{
int shi, ge;
shi = num/10; //十位
ge = num%10; //个位
{
int shi, ge;
shi = num/10; //十位
ge = num%10; //个位
//数码管显示倒计时
if(time % 2 == 0)
{
P0 = table_wei[0];
P2 = table_duan[shi];
}
if(time % 2 == 1)
{
P0 = table_wei[1];
P2 = table_duan[ge];
}
}
if(time % 2 == 0)
{
P0 = table_wei[0];
P2 = table_duan[shi];
}
if(time % 2 == 1)
{
P0 = table_wei[1];
P2 = table_duan[ge];
}
}
//灯亮规则:一侧路口红灯亮,另一侧绿灯亮 在还剩 3s 时,一侧路口红灯和黄灯一起亮,另一侧绿灯和黄灯一起亮
// 在还剩 1s 时,两侧路口都是黄灯亮
// 在还剩 1s 时,两侧路口都是黄灯亮
void display_LED(int num,int j)
{
if(num > 3 && 0 == j) // 东西路口红灯亮, 南北路口绿灯亮
{
led1 = 0; //30s 红
led2 = 1; //30s 黄
led3 = 1; //30s 绿
led4 = 1; //20s 红
led5 = 1; //20s 黄
led6 = 0; //30s 绿
}
else if(num <= 3 && num > 1 && 0 == j) // 东西路口红灯和黄灯一起亮, 南北路口绿灯和黄灯亮
{
led1 = 0; //30s 红
led2 = 0; //30s 黄
led3 = 1; //30s 绿
led4 = 1; //20s 红
led5 = 0; //20s 黄
led6 = 0; //30s 绿
}
else if(num > 3 && 1 == j) // 东西路口绿灯亮, 南北路口红灯亮
{
led1 = 1; //30s 红
led2 = 1; //30s 黄
led3 = 0; //30s 绿
led4 = 0; //20s 红
led5 = 1; //20s 黄
led6 = 1; //30s 绿
}
else if(num <= 3 && num > 1 && 1 == j) // 东西路口绿灯和黄灯一起亮, 南北路口红灯和黄灯一起亮
{
led1 = 1; //30s 红
led2 = 0; //30s 黄
led3 = 0; //30s 绿
led4 = 0; //20s 红
led5 = 0; //20s 黄
led6 = 1; //30s 绿
}
else if(num <= 1 ) /// 东西路口黄灯亮, 南北路口黄灯亮
{
led1 = 1; //30s 红
led2 = 0; //30s 黄
led3 = 1; //30s 绿
led4 = 1; //20s 红
led5 = 0; //20s 黄
led6 = 1; //30s 绿
}
{
if(num > 3 && 0 == j) // 东西路口红灯亮, 南北路口绿灯亮
{
led1 = 0; //30s 红
led2 = 1; //30s 黄
led3 = 1; //30s 绿
led4 = 1; //20s 红
led5 = 1; //20s 黄
led6 = 0; //30s 绿
}
else if(num <= 3 && num > 1 && 0 == j) // 东西路口红灯和黄灯一起亮, 南北路口绿灯和黄灯亮
{
led1 = 0; //30s 红
led2 = 0; //30s 黄
led3 = 1; //30s 绿
led4 = 1; //20s 红
led5 = 0; //20s 黄
led6 = 0; //30s 绿
}
else if(num > 3 && 1 == j) // 东西路口绿灯亮, 南北路口红灯亮
{
led1 = 1; //30s 红
led2 = 1; //30s 黄
led3 = 0; //30s 绿
led4 = 0; //20s 红
led5 = 1; //20s 黄
led6 = 1; //30s 绿
}
else if(num <= 3 && num > 1 && 1 == j) // 东西路口绿灯和黄灯一起亮, 南北路口红灯和黄灯一起亮
{
led1 = 1; //30s 红
led2 = 0; //30s 黄
led3 = 0; //30s 绿
led4 = 0; //20s 红
led5 = 0; //20s 黄
led6 = 1; //30s 绿
}
else if(num <= 1 ) /// 东西路口黄灯亮, 南北路口黄灯亮
{
led1 = 1; //30s 红
led2 = 0; //30s 黄
led3 = 1; //30s 绿
led4 = 1; //20s 红
led5 = 0; //20s 黄
led6 = 1; //30s 绿
}
}
int main()
{
int i = 0;
num = 30;
int main()
{
int i = 0;
num = 30;
EA = 1;
ET0 = 1;
TMOD = 0x01; //使用定时器0工作方式1
TH0 = (65535 - 5000) / 256; //5ms
TL0 = (65535 - 5000) % 256;
TR0 = 1; //开始计时
ET0 = 1;
TMOD = 0x01; //使用定时器0工作方式1
TH0 = (65535 - 5000) / 256; //5ms
TL0 = (65535 - 5000) % 256;
TR0 = 1; //开始计时
while(1)
{
display(num);
if(200 == time)
{
num = num - 1;
time = 0;
i++;
if(0 == num)
{
if(0 == i % 20)
{
num = 30;
i = 1;
j = 0; //30s倒计时,东西路口
}
else
{
num = 20;
i = 0;
j = 1; //20s倒计时,南北路口
}
}
}
display_LED(num,j); //led
}
{
display(num);
if(200 == time)
{
num = num - 1;
time = 0;
i++;
if(0 == num)
{
if(0 == i % 20)
{
num = 30;
i = 1;
j = 0; //30s倒计时,东西路口
}
else
{
num = 20;
i = 0;
j = 1; //20s倒计时,南北路口
}
}
}
display_LED(num,j); //led
}
return 0;
}
}
void timer_0() interrupt 1 //定时器中断
{
time++;
}
今日开发中出现的问题汇总:无
今日未解决的问题:无
今日开发收获:了解了锁存器。
自我评价:认真学习,完成课题。