这是我写的交通灯程序,怎么才能让黄灯闪啊~~?
时间:10-02
整理:3721RD
点击:
#include<reg52.h> //52单片机头文件
#include <intrins.h> //包含有左右循环移位子函数的库
#define uint unsigned int //宏定义
#define uchar unsigned char //宏定义
sbit dula=P2^6;
sbit wela=P2^7;
sbit spk=P3^0;
sbit k1=P3^7;
uchar code table[]={ //显示数据编码
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar t0,t1,shi1,ge1,shi2,ge2;
uint shu1,shu2;
uchar ID1=1;
uchar ID2=1;
uchar flag=1;
void init(); //函数声明
void display1(uchar shi1,uchar ge1);
void display2(uchar shi2,uchar ge2);
void delay(uint z) //延时子函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void main() //主函数
{
init();
while(1)
{
if(key1==0) //夜间模式,黄灯闪烁
{
if(key1==0)
while(1)
{
P1=0xfd&0xbf;
delay2(500);
P1=0xff;
elay2(500);
}
while(!key1);
delay2(5);
while(!key1);
}
if(flag==1)
display1( shi1, ge1);
if(ID1==4)
ID1=1;
switch(ID1)
{
case 1:P1=0xfe;break; //红
case 2:P1=0xfb;break; //绿
case 3:P1=0xfd;break; //黄
default:break;
}
display2( shi2, ge2);
if(ID2==4)
ID2=1;
switch(ID2)
{
case 1:P1=0x7f;break; //绿
case 2:P1=0xbf;break; //黄
case 3:P1=0xdf;break; //红
default:break;
}
}
}
void init() //初始化函数
{
shu1=25;
shu2=22;
TMOD=0x00;
TH0=(65536-50000)/256; //定时器初始化
TL0=(65536-50000)%256;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void timer0() interrupt 1 //定时器0中断函数
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
t1++;
if(t1==100)
{
t1=0;
shu1--;
shu2--;
shi1=shu1/10;
shi2=shu2/10;
ge1=shu1%10;
ge2=shu2%10;
if(shu1==0) //25 南北
{
ID1++;
if(ID1==5)
ID1=2;
switch(ID1)
{
case 1:shu1=3;break;
case 2:shu1=22;break;
case 3:shu1=3;break;
case 4:shu1=25;break;
default:break;
}
}
if(shu2==0) //22 东西
{
ID2++;
if(ID2==5)
ID2=2;
switch(ID2)
{
case 1:shu2=25;break;
case 2:shu2=3;break;
case 3:shu2=25;break;
case 4:shu2=22;break;
default:break;
}
}
}
}
void display1(uchar shi1,uchar ge1) //显示子函数
{
dula=1;
P0=table[shi1];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(5);
dula=1;
P0=table[ge1];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(5);
}
void display2(uchar shi2,uchar ge2)
{
dula=1;
P0=table[ge2];
dula=0;
P0=0xff;
wela=1;
P0=0xef;
wela=0;
delay(5);
dula=1;
P0=table[shi2];
dula=0;
P0=0xdf;
wela=1;
P0=0xf7;
wela=0;
delay(5);
}
#include <intrins.h> //包含有左右循环移位子函数的库
#define uint unsigned int //宏定义
#define uchar unsigned char //宏定义
sbit dula=P2^6;
sbit wela=P2^7;
sbit spk=P3^0;
sbit k1=P3^7;
uchar code table[]={ //显示数据编码
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar t0,t1,shi1,ge1,shi2,ge2;
uint shu1,shu2;
uchar ID1=1;
uchar ID2=1;
uchar flag=1;
void init(); //函数声明
void display1(uchar shi1,uchar ge1);
void display2(uchar shi2,uchar ge2);
void delay(uint z) //延时子函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void main() //主函数
{
init();
while(1)
{
if(key1==0) //夜间模式,黄灯闪烁
{
if(key1==0)
while(1)
{
P1=0xfd&0xbf;
delay2(500);
P1=0xff;
elay2(500);
}
while(!key1);
delay2(5);
while(!key1);
}
if(flag==1)
display1( shi1, ge1);
if(ID1==4)
ID1=1;
switch(ID1)
{
case 1:P1=0xfe;break; //红
case 2:P1=0xfb;break; //绿
case 3:P1=0xfd;break; //黄
default:break;
}
display2( shi2, ge2);
if(ID2==4)
ID2=1;
switch(ID2)
{
case 1:P1=0x7f;break; //绿
case 2:P1=0xbf;break; //黄
case 3:P1=0xdf;break; //红
default:break;
}
}
}
void init() //初始化函数
{
shu1=25;
shu2=22;
TMOD=0x00;
TH0=(65536-50000)/256; //定时器初始化
TL0=(65536-50000)%256;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void timer0() interrupt 1 //定时器0中断函数
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
t1++;
if(t1==100)
{
t1=0;
shu1--;
shu2--;
shi1=shu1/10;
shi2=shu2/10;
ge1=shu1%10;
ge2=shu2%10;
if(shu1==0) //25 南北
{
ID1++;
if(ID1==5)
ID1=2;
switch(ID1)
{
case 1:shu1=3;break;
case 2:shu1=22;break;
case 3:shu1=3;break;
case 4:shu1=25;break;
default:break;
}
}
if(shu2==0) //22 东西
{
ID2++;
if(ID2==5)
ID2=2;
switch(ID2)
{
case 1:shu2=25;break;
case 2:shu2=3;break;
case 3:shu2=25;break;
case 4:shu2=22;break;
default:break;
}
}
}
}
void display1(uchar shi1,uchar ge1) //显示子函数
{
dula=1;
P0=table[shi1];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(5);
dula=1;
P0=table[ge1];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(5);
}
void display2(uchar shi2,uchar ge2)
{
dula=1;
P0=table[ge2];
dula=0;
P0=0xff;
wela=1;
P0=0xef;
wela=0;
delay(5);
dula=1;
P0=table[shi2];
dula=0;
P0=0xdf;
wela=1;
P0=0xf7;
wela=0;
delay(5);
}
循环3次{黄灯=1,延时,黄灯=0,延时}
在主函数的case 里面加是吧