|
大佬们我想问一下加了if(Temp)这里为什么按键就非常不灵敏了,不加判断返回值是否为0代码就能正确检测到按键,这个if(Temp)判断返回值是否为0为什么会导致按键操作不灵敏,这个是测试的,后面必须要用到判断是否为0,不是很理解,求大佬能指点一二
单片机源程序如下:
#include <REGX52.H>
#include "LCD1602.H"
#include "AT24C02.H"
#include "Delay.H"
#include "Timer0Init.H"
#include "Key.H"
void main()
{
unsigned char Temp=0;
Timer0Init();
while(1)
{
Temp=Keynum_return();
if(Temp)//加了判断按键就不灵敏,不加就很正常
{
if(Temp==1)
P2_0=~P2_0;
if(Temp==2)
P2_1=~P2_1;
if(Temp==3)
P2_2=~P2_2;
if(Temp==4)
P2_3=~P2_3;
}
}
}
void Timer0_Rountine(void) interrupt 1//中断函数
{
static unsigned char count;//计数范围为0-256
TL0 = 0x66;
TH0 = 0xFC;
count++;
if(count>=20)
{
count=0;
key_Interrupt();//按键中断
}
}
这是Key里面的函数
#include <REGX52.H>
unsigned char Key_num;
unsigned char Keynum_return()
{
unsigned char Temp=0;
Temp=Key_num;
Key_num=0;
return Temp;
}
unsigned char Key_Timer0()
{
unsigned char KeyNumber=0;
if(P3_1==0){KeyNumber=1;}
if(P3_0==0){KeyNumber=2;}
if(P3_2==0){KeyNumber=3;}
if(P3_3==0){KeyNumber=4;}
return KeyNumber;
}
void key_Interrupt()//按键中断函数
{
static unsigned char Last_Status=0,Now_Status=0;
Last_Status=Now_Status;
Now_Status=Key_Timer0();//现态次态检测
if( Last_Status==1 && Now_Status==0)
{
Key_num=1;
}
if( Last_Status==2 && Now_Status==0)
{
Key_num=2;
}
if( Last_Status==3 && Now_Status==0)
{
Key_num=3;
}
if( Last_Status==4 && Now_Status==0)
{
Key_num=4;
}
}
|
|