急!求大神帮忙看看我这程序问题出在哪里?
时间:10-02
整理:3721RD
点击:
我是参考写的,但是写好后发现有问题,暂停时有错,继续可以,然后设定时间也有错,我红色标明的地方也有问题
#include<reg52.h>
#define uint signed int
#define uchar signed char
uchar t0,hour1,hour2,min1,min2,sec1,sec2,k=1,k1,key,dingshi,yidingshi,temp,wei,i,s1,s2,f1,f2,m1,m2;
uint t;
uchar sec,min,hour,s,f,m,counter,j;
sbit dula=P2^6;
sbit wela=P2^7;
sbit d=P2^3;
bit nd;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delay(uint z);
void init();
void display(uchar hour2,uchar hour1,uchar min2,uchar min1,uchar sec2,uchar sec1);
void keyscan();
void keyscan0();
void time();
void main()
{
init();
while(1)
{
keyscan();
if(k==1)
{
TR0=1;
wei=0;
time();
display(s2,s1,f2,f1,m2,m1);
}
else
{
if(k1==1)
{
TR0=0;
// keyscan0();//有问题?
if(key!=10)
{
switch(wei)
{
case 1:if(key<3)//小时的最高位为2
s1=key;
else
wei--;
break;
case 2:if((s1==0)||(s1==1))
s2=key;
else
{if(key<5)
s2=key;
}
break;
case 3:if(key<6)
f1=key;
else
wei--;
break;
case 4:f2=key;
break;
case 5:if(key<6)
m1=key;
else
wei--;
break;
case 6:m2=key;
break;
}
key=10;
}
s=s1*10+s2;
f=f1*10+f2;
m=m1*10+m2;
display(s1,s2,f1,f2,m1,m2);
}
if(dingshi==1)
{
TR0=0;
display(hour1, hour2, min1, min2, sec1, sec2);
keyscan0();
if(key!=10)
{
switch(wei)
{
case 1:if(key<3)//小时的最高位为2
hour1=key;
else
wei--;
break;
case 2:if((s1==0)||(s1==1))
hour2=key;
else
{if(key<5)
s2=key;
}
break;
case 3:if(key<6)
min1=key;
else
wei--;
break;
case 4:min2=key;
break;
case 5:if(key<6)
sec1=key;
else
wei--;
break;
case 6:sec2=key;
break;
}
key=10;
}
yidingshi=1;
}
display(hour1, hour2, min1, min2, sec1, sec2);
}
}
/*if(yidingshi==1)
{
if((s1==hour1)&&(s2==hour2)&&(f1==min1)&&(f2==min2)&&(m1=sec1)&&(m2==sec2))
d=0;
}*/
}
void delay(uint z)//延时
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void init()//初始化
{
TMOD=0x01;//定时器0方式1
TH0=(65536-50000)/256;//定时器50ms发生一次中断 ,定时器高八位赋值
TL0=(65536-50000)%256;//定时器低八位赋值
/* TH1=(65536-10000)/256;
TL1=(65536-10000)%256;*/
EA=1;//打开总中断
ET0=1;//打开定时器0中断
// TR0=1;//启动定时器
//ET1=1;
//TR1=1;
}
void display(uchar hour2,uchar hour1,uchar min2,uchar min1,uchar sec2,uchar sec1)//显示函数
{
dula=1;
P0=table[hour1];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(1);
dula=1;
P0=table[hour2]|0x80;//小时个位带小数点
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(1);
dula=1;
P0=table[min1];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delay(1);
dula=1;
P0=table[min2]|0x80;//分钟个位带小数点
dula=0;
P0=0xff;
wela=1;
P0=0xf7;
wela=0;
delay(1);
dula=1;
P0=table[sec1];
dula=0;
P0=0xff;
wela=1;
P0=0xef;
wela=0;
delay(1);
dula=1;
P0=table[sec2];
dula=0;
P0=0xff;
wela=1;
P0=0xdf;
wela=0;
delay(1);
}
void time()//时间处理
{
if(t0==20)//1秒显示一个数,够20次中断,1秒钟进行一个结果采样
{
t0=0;//中断次数清零
sec++;
if(sec==60)
{
sec=0;
min++;
}
if(min==60)
{
min=0;
hour++;
}
if(hour==24)
{
hour=0;
}
}
s1=hour/10;
s2=hour%10;
f1=min/10;
f2=min%10;
m1=sec/10;
m2=sec%10;
}
void keyscan()//键盘定时
{
P3=0xfb;//选通第一行
temp=P3;//读回P3口的状态
temp=temp&0xf0;//屏蔽低四位,把高四位的端口状态读进来
if(temp!=0xf0)//检测是否有键按下
{
delay(10);//键盘是否按下,检测延时
temp=P3;//消抖
temp=temp&0xf0;
if(temp!=0xf0)//检测是否有键按下
{
temp=P3;//检测高四位
switch(temp)
{
case 0xbb://按下暂停
k=0;
k1=1;
break;
case 0x7b://按下继续
k=1;
k1=0;
dingshi=0;//定时
break;
}
if(temp!=0xf0)//松手检测
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7://设定时间
k=0;
dingshi=1;
break;
case 0xd7:
yidingshi=0; //取消定时
break;
}
if(temp!=0xf0)//松手检测
{
temp=P3;
temp=temp&0xf0;
}
}
}
}
void keyscan0()
{
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:key=0;
wei++;
break;
case 0xde:key=1;
wei++;
break;
case 0xbe:key=2;
wei++;
break;
case 0x7e:key=3;
wei++;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:key=4;
wei++;
break;
case 0xdd:key=5;
wei++;
break;
case 0xbd:key=6;
wei++;
break;
case 0x7d:key=7;
wei++;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:key=8;
wei++;
break;
case 0xdb:key=9;
wei++;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
}
}
/*void timer1() interrupt 3
{
if(i<400)
{
TH1=(65536-11500)/256;
TL1=(65535-11500)%256;
}
else
{
TH1=(65536-23000)/256;
TL1=(65535-23000)%256;
}
counter++;
if(counter==5)
{
counter=0;
//d=~d;
j++;
if(j==600)
{
d=1;
EA=0;
ET1=0;
TR1=0;
}
}
}*/
void timer0() interrupt 1 //中断
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
t0++;
}
#include<reg52.h>
#define uint signed int
#define uchar signed char
uchar t0,hour1,hour2,min1,min2,sec1,sec2,k=1,k1,key,dingshi,yidingshi,temp,wei,i,s1,s2,f1,f2,m1,m2;
uint t;
uchar sec,min,hour,s,f,m,counter,j;
sbit dula=P2^6;
sbit wela=P2^7;
sbit d=P2^3;
bit nd;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delay(uint z);
void init();
void display(uchar hour2,uchar hour1,uchar min2,uchar min1,uchar sec2,uchar sec1);
void keyscan();
void keyscan0();
void time();
void main()
{
init();
while(1)
{
keyscan();
if(k==1)
{
TR0=1;
wei=0;
time();
display(s2,s1,f2,f1,m2,m1);
}
else
{
if(k1==1)
{
TR0=0;
// keyscan0();//有问题?
if(key!=10)
{
switch(wei)
{
case 1:if(key<3)//小时的最高位为2
s1=key;
else
wei--;
break;
case 2:if((s1==0)||(s1==1))
s2=key;
else
{if(key<5)
s2=key;
}
break;
case 3:if(key<6)
f1=key;
else
wei--;
break;
case 4:f2=key;
break;
case 5:if(key<6)
m1=key;
else
wei--;
break;
case 6:m2=key;
break;
}
key=10;
}
s=s1*10+s2;
f=f1*10+f2;
m=m1*10+m2;
display(s1,s2,f1,f2,m1,m2);
}
if(dingshi==1)
{
TR0=0;
display(hour1, hour2, min1, min2, sec1, sec2);
keyscan0();
if(key!=10)
{
switch(wei)
{
case 1:if(key<3)//小时的最高位为2
hour1=key;
else
wei--;
break;
case 2:if((s1==0)||(s1==1))
hour2=key;
else
{if(key<5)
s2=key;
}
break;
case 3:if(key<6)
min1=key;
else
wei--;
break;
case 4:min2=key;
break;
case 5:if(key<6)
sec1=key;
else
wei--;
break;
case 6:sec2=key;
break;
}
key=10;
}
yidingshi=1;
}
display(hour1, hour2, min1, min2, sec1, sec2);
}
}
/*if(yidingshi==1)
{
if((s1==hour1)&&(s2==hour2)&&(f1==min1)&&(f2==min2)&&(m1=sec1)&&(m2==sec2))
d=0;
}*/
}
void delay(uint z)//延时
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void init()//初始化
{
TMOD=0x01;//定时器0方式1
TH0=(65536-50000)/256;//定时器50ms发生一次中断 ,定时器高八位赋值
TL0=(65536-50000)%256;//定时器低八位赋值
/* TH1=(65536-10000)/256;
TL1=(65536-10000)%256;*/
EA=1;//打开总中断
ET0=1;//打开定时器0中断
// TR0=1;//启动定时器
//ET1=1;
//TR1=1;
}
void display(uchar hour2,uchar hour1,uchar min2,uchar min1,uchar sec2,uchar sec1)//显示函数
{
dula=1;
P0=table[hour1];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(1);
dula=1;
P0=table[hour2]|0x80;//小时个位带小数点
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(1);
dula=1;
P0=table[min1];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delay(1);
dula=1;
P0=table[min2]|0x80;//分钟个位带小数点
dula=0;
P0=0xff;
wela=1;
P0=0xf7;
wela=0;
delay(1);
dula=1;
P0=table[sec1];
dula=0;
P0=0xff;
wela=1;
P0=0xef;
wela=0;
delay(1);
dula=1;
P0=table[sec2];
dula=0;
P0=0xff;
wela=1;
P0=0xdf;
wela=0;
delay(1);
}
void time()//时间处理
{
if(t0==20)//1秒显示一个数,够20次中断,1秒钟进行一个结果采样
{
t0=0;//中断次数清零
sec++;
if(sec==60)
{
sec=0;
min++;
}
if(min==60)
{
min=0;
hour++;
}
if(hour==24)
{
hour=0;
}
}
s1=hour/10;
s2=hour%10;
f1=min/10;
f2=min%10;
m1=sec/10;
m2=sec%10;
}
void keyscan()//键盘定时
{
P3=0xfb;//选通第一行
temp=P3;//读回P3口的状态
temp=temp&0xf0;//屏蔽低四位,把高四位的端口状态读进来
if(temp!=0xf0)//检测是否有键按下
{
delay(10);//键盘是否按下,检测延时
temp=P3;//消抖
temp=temp&0xf0;
if(temp!=0xf0)//检测是否有键按下
{
temp=P3;//检测高四位
switch(temp)
{
case 0xbb://按下暂停
k=0;
k1=1;
break;
case 0x7b://按下继续
k=1;
k1=0;
dingshi=0;//定时
break;
}
if(temp!=0xf0)//松手检测
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7://设定时间
k=0;
dingshi=1;
break;
case 0xd7:
yidingshi=0; //取消定时
break;
}
if(temp!=0xf0)//松手检测
{
temp=P3;
temp=temp&0xf0;
}
}
}
}
void keyscan0()
{
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:key=0;
wei++;
break;
case 0xde:key=1;
wei++;
break;
case 0xbe:key=2;
wei++;
break;
case 0x7e:key=3;
wei++;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:key=4;
wei++;
break;
case 0xdd:key=5;
wei++;
break;
case 0xbd:key=6;
wei++;
break;
case 0x7d:key=7;
wei++;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:key=8;
wei++;
break;
case 0xdb:key=9;
wei++;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
}
}
/*void timer1() interrupt 3
{
if(i<400)
{
TH1=(65536-11500)/256;
TL1=(65535-11500)%256;
}
else
{
TH1=(65536-23000)/256;
TL1=(65535-23000)%256;
}
counter++;
if(counter==5)
{
counter=0;
//d=~d;
j++;
if(j==600)
{
d=1;
EA=0;
ET1=0;
TR1=0;
}
}
}*/
void timer0() interrupt 1 //中断
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
t0++;
}