我想做一个简单的红绿灯,绿灯亮,数码管从90减到0,然后绿灯关闭,红灯亮,数码管再从90减到0,调试了半天转到红灯亮的时候就停止了,数码管不减了,哪位大虾帮帮忙看看怎么回事?
程序如下: #include<reg52.h>
#define uchar unsigned char;
#define uint unsigned int;
sbit green=P1^2;
sbit red=P1^0;
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char temp,shi,ge,t0,t1,flag,flag1,aa,bb,temp1;
void display1(unsigned char aa,unsigned char bb);
void delay(unsigned int z);
void init();
void display(unsigned char shi,unsigned char ge);
void main()
{
init();
while(1)
{
if(flag1!=1)
{
green=0;
display(shi,ge);
P2=0xff;
}
else
{
temp1=90;
red=0;
display1(aa,bb);
}
}
}
void display(unsigned char shi,unsigned char ge)
{
shi=temp/10;
ge=temp%10;
dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(1);
dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(1);
}
void display1(unsigned char aa,unsigned char bb)
{
aa=temp1/10;
bb=temp1%10;
wela=1;
P0=0xfe;
wela=0;
P0=0xff;
dula=1;
P0=table[aa];
dula=0;
delay(1);
wela=1;
P0=0xfd;
wela=0;
P0=0xff;
dula=1;
P0=table[bb];
dula=0;
delay(1);
}
void delay(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--)
;
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
t0++;
if(flag!=1)
{
if(t0==20)
{
t0=0;
temp--;
if(temp==0)
{
P2=0xff;
}
}
}
else
{
green=1;
if(t0==20)
{
t0=0;
temp1--;
if(temp1==0)
{
P2=0xff;
}
}
flag1=1;
}
}
void timer1() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
t1++;
if(t1==1820)
{
t1=0;
TR0=0;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
flag=1;
t0=0;
}
}
void init()
{
temp=90;
temp1=90;
TMOD=0x11;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
EA=1;
ET0=1;
ET1=1;
TR1=1;
TR0=1;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
flag与flag1只有置1语句没有置0语句!
修改后程序如下:
#include<reg52.h>
#define uchar unsigned char;
#define uint unsigned int;
sbit green=P1^2;
sbit red=P1^0;
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char temp,shi,ge,t0,t1,flag,flag1,aa,bb,temp1;
void display1(unsigned char aa,unsigned char bb);
void delay(unsigned int z);
void init();
void display(unsigned char shi,unsigned char ge);
void main()
{
init();
while(1)
{
if(flag1!=1)
{
green=0;//开始绿灯亮,
display(shi,ge);//显示数字
P2=0xff;//关闭数码管
}
else
{
red=0;
display1(aa,bb);
}
}
}
void display(unsigned char shi,unsigned char ge)
{
shi=temp/10;
ge=temp%10;
dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(1);
dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(1);
}
void display1(unsigned char aa,unsigned char bb)
{
aa=temp1/10;
bb=temp1%10;
dula=1;
P0=table[aa];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(1);
dula=1;
P0=table[bb];
dula=0;
P0=0xff;//消影
wela=1;
P0=0xfd;
wela=0;
delay(1);
}
void delay(unsigned int z)//延时函数
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--)
;
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
t0++;
if(flag!=1)
{
if(t0==20)
{
t0=0;
temp--;
if(temp==0)
{
P2=0xff;
}
}
}
else
{
green=1;
if(t0==20)
{
t0=0;
temp1--;
if(temp1==0)
{
P2=0xff;
TR0=0;
}
}
flag1=1;
}
}
void timer1() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
t1++;
if(t1==220)
{
t1=0;
flag=1;
t0=0;
}
}
void init() //初始化子程序
{
temp=10;
temp1=5;
TMOD=0x11;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
EA=1;
ET0=1;
ET1=1;
TR1=1;
TR0=1;
}
高手帮忙怎样实现红绿灯循环?数码管要跟着红绿灯变化
一周热门 更多>