学习按键扫描的时候就碰到许多问题了,是在protues,今天又遇到了,是在LCD1602上做时钟的,我的设定是按了k1键,即P3.0口,弄了一个K1_flag标志,先放代码
#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar code str[]={"---LCD Clock---"};
uchar code str1[]={"****"} ;
uchar hour=23,min=59;sec=45;
***it k1=P3^0;
***it k2=P3^1;
***it k3=P3^2;
***it k4=P3^3;
bit k1_flag=0;
uchar buf=':';
uchar count_10ms;
uchar disp_buf[6]={0x00,0x00,0x00,0x00,0x00,0x00};
void timer_init()
{
TMOD=0x10;
TH1=(65536-10000)/256; TL1=(65536-10000)%256;
EA=1;ET1=1;
TR1=1;
}
void LCD_conv(uchar in1,in2,in3)
{
disp_buf[0]=in1/10+0x30;
disp_buf[1]=in1%10+0x30;
disp_buf[2]=in2/10+0x30;
disp_buf[3]=in2%10+0x30;
disp_buf[4]=in3/10+0x30;
disp_buf[5]=in3%10+0x30;
}
void delay(uchar xms)
{
uchar i,j;
for(i=xms;i>0;i--)
for(j=115;j>0;j--);
}
void timer1() interrupt 3
{
TH1=(65536-10000)/256; TL1=(65536-10000)%256;
count_10ms++;
}
void keyprocess()
{
TR1=0;
if(k1==0)
{
delay(10);
if(k1==0)
{
while(!k1);
k1_flag=1;
}
}
if(k2==0 && k1_flag==1)
{
delay(10);
if(k2==0)
{
while(!k2);
//Write_LCD_Command(0x07);
hour++;
if(hour==24)
{
hour=0;
}
}
}
if(k3==0 && k1_flag==1)
{
delay(10);
if(k3==0)
{
while(!k3);
min++;
if(min==60)
{
min=0; hour++;
if(hour==24)
hour=0;
}
}
}
if(k4==0 && k1_flag==1)
{
delay(10);
if(k4==0)
{
while(!k4);
sec++;
if(sec==60)
{
sec=0; min++;
}
}
}
// k1_flag=0;
TR1=1;
}
void LCD_disp()
{
ShowString(4,1,disp_buf);
ShowString(5,1,disp_buf+1);
ShowString(6,1,&buf);
ShowString(7,1,disp_buf+2);
ShowString(8,1,disp_buf+3);
ShowString(9,1,&buf);
ShowString(10,1,disp_buf+4);
ShowString(11,1,disp_buf+5);
}
void main()
{
uchar i;
Initialize_LCD();
i=0;
count_10ms=0;
while(str[i]!=' |