我使用逐行扫描法,使用定时器的方式做了延迟防抖。Keil中软件调试的时候发现,查询不到TF0置1,因此不能再次赋初值,请帮忙解答,非常感谢。
原码如下:
#include<reg52.h>
code unsigned char ShU_Duan_Data[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
code unsigned char ShU_Wei_Data[8]={0xfe,0xfd,0xfb,0xf7,0xEf,0xdf,0xbf,0x7f};
unsigned char Shu_Data[8]={0x00};
unsigned int counter=0;
unsigned char Row_Scan_Data,Column_Data,Key_Data,Key_Value,Key_Temp,Timeout_Flag;
sbit HC595_RCK=P1^0;
sbit HC595_SCK=P1^1;
sbit HC595_Data=P1^4;
sbit Key_P23=P2^3;
sbit Key_P24=P2^4;
void Shuma_Send_Data(unsigned char Shu_Data_One,unsigned char Shu_Data_Two)
{
int i;
HC595_RCK=0;
for(i=0;i<8;i++)
{
HC595_SCK=0;
if(0!=(Shu_Data_One&0x80))
{
HC595_Data=1;
}
else
{
HC595_Data=0;
}
Shu_Data_One<<=1;
HC595_SCK=1;
}
for(i=0;i<8;i++)
{
HC595_SCK=0;
if(0!=(Shu_Data_Two&0x80))
{
HC595_Data=1;
}
else
{
HC595_Data=0;
}
Shu_Data_Two<<=1;
HC595_SCK=1;
}
HC595_RCK=1;
}
void Delay_1ms(int j)
{
int i;
for(;j>0;j--)
{
for(i=0;i<110;i++)
{}
}
}
void Interrupt_Init()
{
ET0=1;
ET1=1;
EA=1;
}
void Timer0_Init()
{
TMOD&=0xF0;
TMOD|=0x01;
TH0=0xDC; //30ms定时
TL0=0x00;
TR0=1;
}
void Timer1_Init()
{
TMOD&=0x0F;
TMOD|=0x10;
TH1=0xFC; //1ms定时
TL1=0x66;
TR1=1;
}
void T1_Interrupt() interrupt 3
{
unsigned char k;
TH1=0xFC; //1ms定时
TL1=0x66;
Shuma_Send_Data(ShU_Duan_Data[Shu_Data[k]],ShU_Wei_Data[k]);//delay_1ms(1);
if(k<7)
{
k++;
}
else
{
k=0;
}
}
int Key_Scan();
void main()
{
Timer0_Init();
Timer1_Init();
Interrupt_Init();
while(1)
{
Key_Data=Key_Scan();
if(0!=Key_Data)
{
switch(Key_Data)
{
case 0x11:Key_Value=0;break;
case 0x21:Key_Value=1;break;
case 0x41:Key_Value=2;break;
case 0x81:Key_Value=3;break;
case 0x12:Key_Value=4;break;
case 0x22:Key_Value=5;break;
case 0x42:Key_Value=6;break;
case 0x82:Key_Value=7;break;
case 0x14:Key_Value=8;break;
case 0x24:Key_Value=9;break;
case 0x44:Key_Value=10;break;
case 0x84:Key_Value=11;break;
case 0x18:Key_Value=12;break;
case 0x28:Key_Value=13;break;
case 0x48:Key_Value=14;break;
case 0x88:Key_Value=15;break;
}
Shu_Data[0]=Key_Value;
}
Key_Data=0;
}
}
int Key_Scan()
{
P2=0xF0;
Key_P24=0;
if(P2^0xF0)
{
if(1==TF0)
{
TF0=0;
TH0=0xDC; //30ms定时
TL0=0x00;
Timeout_Flag=1;
}
if(1==Timeout_Flag)
{
if(P2^0xF0)
{
Row_Scan_Data=0xFE;
while(Row_Scan_Data^0xEF) //Row_Scan_Data左移加1,赋值到EF就扫描完成了
{
P2=Row_Scan_Data;
if((P2&0xF0)^0xF0)
{
Column_Data=((P2&0xF0)|0x0F);
return((~Column_Data)+(~Row_Scan_Data));
}
else
{
Row_Scan_Data=((Row_Scan_Data<<1)|0x01);
}
Delay_1ms(10);
}
}
}
}
return(0);
}
原码如下:
#include<reg52.h>
code unsigned char ShU_Duan_Data[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
code unsigned char ShU_Wei_Data[8]={0xfe,0xfd,0xfb,0xf7,0xEf,0xdf,0xbf,0x7f};
unsigned char Shu_Data[8]={0x00};
unsigned int counter=0;
unsigned char Row_Scan_Data,Column_Data,Key_Data,Key_Value,Key_Temp,Timeout_Flag;
sbit HC595_RCK=P1^0;
sbit HC595_SCK=P1^1;
sbit HC595_Data=P1^4;
sbit Key_P23=P2^3;
sbit Key_P24=P2^4;
void Shuma_Send_Data(unsigned char Shu_Data_One,unsigned char Shu_Data_Two)
{
int i;
HC595_RCK=0;
for(i=0;i<8;i++)
{
HC595_SCK=0;
if(0!=(Shu_Data_One&0x80))
{
HC595_Data=1;
}
else
{
HC595_Data=0;
}
Shu_Data_One<<=1;
HC595_SCK=1;
}
for(i=0;i<8;i++)
{
HC595_SCK=0;
if(0!=(Shu_Data_Two&0x80))
{
HC595_Data=1;
}
else
{
HC595_Data=0;
}
Shu_Data_Two<<=1;
HC595_SCK=1;
}
HC595_RCK=1;
}
void Delay_1ms(int j)
{
int i;
for(;j>0;j--)
{
for(i=0;i<110;i++)
{}
}
}
void Interrupt_Init()
{
ET0=1;
ET1=1;
EA=1;
}
void Timer0_Init()
{
TMOD&=0xF0;
TMOD|=0x01;
TH0=0xDC; //30ms定时
TL0=0x00;
TR0=1;
}
void Timer1_Init()
{
TMOD&=0x0F;
TMOD|=0x10;
TH1=0xFC; //1ms定时
TL1=0x66;
TR1=1;
}
void T1_Interrupt() interrupt 3
{
unsigned char k;
TH1=0xFC; //1ms定时
TL1=0x66;
Shuma_Send_Data(ShU_Duan_Data[Shu_Data[k]],ShU_Wei_Data[k]);//delay_1ms(1);
if(k<7)
{
k++;
}
else
{
k=0;
}
}
int Key_Scan();
void main()
{
Timer0_Init();
Timer1_Init();
Interrupt_Init();
while(1)
{
Key_Data=Key_Scan();
if(0!=Key_Data)
{
switch(Key_Data)
{
case 0x11:Key_Value=0;break;
case 0x21:Key_Value=1;break;
case 0x41:Key_Value=2;break;
case 0x81:Key_Value=3;break;
case 0x12:Key_Value=4;break;
case 0x22:Key_Value=5;break;
case 0x42:Key_Value=6;break;
case 0x82:Key_Value=7;break;
case 0x14:Key_Value=8;break;
case 0x24:Key_Value=9;break;
case 0x44:Key_Value=10;break;
case 0x84:Key_Value=11;break;
case 0x18:Key_Value=12;break;
case 0x28:Key_Value=13;break;
case 0x48:Key_Value=14;break;
case 0x88:Key_Value=15;break;
}
Shu_Data[0]=Key_Value;
}
Key_Data=0;
}
}
int Key_Scan()
{
P2=0xF0;
Key_P24=0;
if(P2^0xF0)
{
if(1==TF0)
{
TF0=0;
TH0=0xDC; //30ms定时
TL0=0x00;
Timeout_Flag=1;
}
if(1==Timeout_Flag)
{
if(P2^0xF0)
{
Row_Scan_Data=0xFE;
while(Row_Scan_Data^0xEF) //Row_Scan_Data左移加1,赋值到EF就扫描完成了
{
P2=Row_Scan_Data;
if((P2&0xF0)^0xF0)
{
Column_Data=((P2&0xF0)|0x0F);
return((~Column_Data)+(~Row_Scan_Data));
}
else
{
Row_Scan_Data=((Row_Scan_Data<<1)|0x01);
}
Delay_1ms(10);
}
}
}
}
return(0);
}