各位大侠帮忙看一下问题出在那儿,秒表开始后按了设定了好的按键不会停止,想实现的功能是按下key2=5的按键开始,再按下停止以此循环查了一下似乎if(key2==5)下面的语句进不去TR0一直都是1,按键编码检查了没有问题,还望指点迷津
#include <reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit wei=P2^6;
sbit duan=P2^7;
uchar code table_tube[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar code a[]={0xFE,0xFD,0xFB,0xF7};
uint xms,temp,key1,key2;
uchar num,bai,shi,ge,tt0;
void delay(uint);
void display(uchar,uchar,uchar);
void init();
uchar keynum();
uchar coding(uchar);
void main()
{
init();
while(1)
{
key1=keynum();
key2=coding(key1);
if(key2==5)
{
key2=0;
TR0=~TR0;
}
}
}
void init()
{
TMOD=0x11;
TH0=(65535-10000)/256;
TL0=(65535-10000)%256;
TH1=(65535-1000)/256;
TL1=(65535-1000)%256;
EA=1;ET0=1;TR0=1;
ET1=1;TR1=1;
P1=0xff;
wei=1;
P0=0xf8;
wei=0;
duan=1;
P0=table_tube[0];
duan=0;
}
void display(uchar bai,uchar shi,uchar ge)
{
wei=1;
P0=0xfe;
wei=0;
duan=1;
P0=table_tube[bai]+0x80;
duan=0;
delay(1);
wei=1;
P0=0xfd;
wei=0;
duan=1;
P0=table_tube[shi];
duan=0;
delay(1);
wei=1;
P0=0xfb;
wei=0;
duan=1;
P0=table_tube[ge];
duan=0;
delay(1);
}
uchar coding(uchar m)
{
uchar k;
switch(m)
{
case (0xee): k=0;break;
case (0xde): k=1;break;
case (0xbe): k=2;break;
case (0x7e): k=3;break;
case (0xed): k=4;break;
case (0xdd): k=5;break;
case (0xbd): k=6;break;
case (0x7d): k=7;break;
case (0xeb): k=8;break;
case (0xdb): k=9;break;
case (0xbb): k=10;break;
case (0x7b): k=11;break;
case (0xe7): k=12;break;
case (0xd7): k=13;break;
case (0xb7): k=14;break;
case (0x77): k=15;break;
}
return k;
}
uchar keynum()
{
uchar i,k;
P1=0xf0; //¿éòÔÖ±½óìá衸ßËÄλ±è½Ï£¬èôÎT¼ü°′Ï£¬¸ßËÄλ±Ø¶¨êÇ1111£¬±è½ÏP1&0xf0oí0xf0êÇ·ñÏàí¬¼′¿é
if((P1&0xf0)!=0xf0)
{
delay(5);
if((P1&0xf0)!=0xf0)
{
i=0;
P1=a[i];
while(i<4)
{
if((P1&0xf0)!=0xf0) //¿éòÔÖ±½óìá衸ßËÄ룬±è½ÏP1&0xf0oí0xf0
{
k=P1;
break;
}
else
{
i++;
P1=a[i];
}
}
// while((P1&0xf0)!=0xf0);
return k;
}
}
return 0;
}
void delay(uint xms)
{
uint x,y;
for(x=xms;x>0;x--)
for(y=110;y>0;y--);
}
void timer0()interrupt 1
{
TH0=(65535-10000)/256;
TL0=(65535-10000)%256;
temp++;
if(temp==1000)
temp=0;
bai=temp/100;
shi=temp%100/10;
ge=temp%100%10;
}
void timer1()interrupt 3
{
TH1=(65535-1000)/256;
TL1=(65535-1000)%256;
display(bai,shi,ge);
}
|