矩阵按键的处理
利用矩阵键盘进行按键数值的处理,需要设置标志位来判断按键是否按下
if 和else if 的使用 可能会导致现象显示的不是很好
#include <STC15F2K60S2.H>
#define uint unsigned int
#define uchar unsigned char
#include "onewire.h"
uint wendu;
uchar num,tmin=20,tmax=25;
uchar shezhi=0;
uchar qujian;
bit state=0;
bit qingchu=0;
bit flag;
sbit L1=P3^0;
sbit L2=P3^1;
sbit L3=P3^2;
sbit L4=P3^3;
sbit C1=P3^4;
sbit C2=P3^5;
sbit C3=P4^2;
sbit C4=P4^4;
uint count;
unsigned char yi,er,san,si,wu,liu,qi,ba;
uchar code smg_duan[18]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0xff,
0xbf,0x7f};
void select(uchar n)
{
switch(n)
{
case 4: P2=(P2&0x1f)|0x80;
break;
case 5: P2=(P2&0x1f)|0xa0;
break;
case 6: P2=(P2&0x1f)|0xc0;
break;
case 7: P2=(P2&0x1f)|0xe0;
break;
case 0: P2=(P2&0x1f)|0x00;
break;
}
}
void Delay1000us()
{
unsigned char i, j;
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
void display1(uchar yi,er)
{
select(6);
P0=0x01;
select(7);
P0=smg_duan[yi];
Delay1000us();
select(6);
P0=0x02;
select(7);
P0=smg_duan[er];
Delay1000us();
select(6);
P0=0xff;
select(7);
P0=smg_duan[15];
Delay1000us();
}
void display2(uchar san,si)
{
select(6);
P0=0x04;
select(7);
P0=smg_duan[san];
Delay1000us();
select(6);
P0=0x08;
select(7);
P0=smg_duan[si];
Delay1000us();
select(6);
P0=0xff;
select(7);
P0=smg_duan[15];
Delay1000us();
}
void display3(uchar wu,liu)
{
select(6);
P0=0x10;
select(7);
P0=smg_duan[wu];
Delay1000us();
select(6);
P0=0x20;
select(7);
P0=smg_duan[liu];
Delay1000us();
select(6);
P0=0xff;
select(7);
P0=smg_duan[15];
Delay1000us();
}
void display4(uchar qi,ba)
{
select(6);
P0=0x40;
select(7);
P0=smg_duan[qi];
Delay1000us();
select(6);
P0=0x80;
select(7);
P0=smg_duan[ba];
Delay1000us();
select(6);
P0=0xff;
select(7);
P0=smg_duan[15];
Delay1000us();
}
void keyscan()
{
L1=0;
L2=L3=L4=1;
C1=C2=C3=C4=1;
if(C4==0)
{
Delay1000us();
if(C4==0)
{
num=0;state=1;
}while(C4==0);
}
if(C3==0)
{
Delay1000us();
if(C3==0)
{
num=1;state=1;
}while(C3==0);
}
if(C2==0)
{
Delay1000us();
if(C2==0)
{
num=2;state=1;
}while(C2==0);
}
L2=0;
L1=L3=L4=1;
C1=C2=C3=C4=1;
if(C4==0)
{
Delay1000us();
if(C4==0)
{
num=3;state=1;
}while(C4==0);
}
if(C3==0)
{
Delay1000us();
if(C3==0)
{
num=4;state=1;
}while(C3==0);
}
if(C2==0)
{
Delay1000us();
if(C2==0)
{
num=5;state=1;
}while(C2==0);
}
L3=0;
L2=L1=L4=1;
C1=C2=C3=C4=1;
if(C4==0)
{
Delay1000us();
if(C4==0)
{
num=6;state=1;
}while(C4==0);
}
if(C3==0)
{
Delay1000us();
if(C3==0)
{
num=7;state=1;
}while(C3==0);
}
if(C2==0)
{
Delay1000us();
if(C2==0)
{
num=8;state=1;
}while(C2==0);
}
L4=0;
L2=L1=L3=1;
C1=C2=C3=C4=1;
if(C4==0)
{
Delay1000us();
if(C4==0)
{
num=9;state=1;
}while(C4==0);
}
if(C3==0)
{
Delay1000us();
if(C3==0)
{
if(shezhi==0){shezhi=1;yi=16;er=15;san=15;si=15;wu=15;liu=16;qi=15;ba=15;EA=0;ET0=0;select(4);P0=0xff;}
else if(shezhi==1){shezhi=0;EA=1;ET0=1;}
}while(C3==0);
}
if(C2==0)
{
Delay1000us();
if(C2==0)
{
if(qingchu==0){qingchu=1;shezhi=1;yi=16;er=15;san=15;si=15;wu=15;liu=16;qi=15;ba=15;}
else if(qingchu==1){qingchu=0;shezhi=1;yi=16;er=15;san=15;si=15;wu=15;liu=16;qi=15;ba=15;}
}while(C2==0);
}
}
void intint()
{
TMOD&=0xf0;
TH0=(65535-10000)/256;
TL0=(65535-10000)%256;
EA=1;
TR0=1;
ET0=1;
}
void service() interrupt 1
{
count++;
if(qujian==0)
{
if(count==80)
{ count=0;
if(flag==0){flag=1;P0=0xfe;select(4);}
else if(flag==1){flag=0;P0=0xff;select(4);}
}
}
if(qujian==1)
{
if(count==40)
{
count=0;
if(flag==0){flag=1;P0=0xfe;select(4);}
else if(flag==1){flag=0;P0=0xff;select(4);}
}
}
if(qujian==2)
{
if(count==20)
{
count=0;
if(flag==0){flag=1;P0=0xfe;select(4);}
else if(flag==1){flag=0;P0=0xff;select(4);}
}
}
}
void inintsystem()
{
P0=0x00;
select(5);
P0=0xff;
select(4);
}
void main()
{
inintsystem();
intint();
while(1)
{
wendu=get_wendu();
if(shezhi==0)
{
if(wendu<tmin){qujian=0;}
else if((tmin<=wendu)&&(wendu<=tmax)){qujian=1;}
else if(wendu>tmax){qujian=2;}
yi=16;er=qujian;san=16;si=15;wu=15;liu=15;qi=wendu/10;ba=wendu%10;
}
else if(shezhi==1)
{
if(er==15&&state==1){state=0;er=num;}
if(san==15&&state==1){state=0;san=num;}
if(qi==15&&state==1){state=0;qi=num;}
if(ba==15&&state==1)
{ state=0;ba=num;
if((er*10+san)>=(qi*10+ba)){select(4);P0=0xfd;}
else if((er*10+san)<(qi*10+ba)) {tmin=er*10+san;tmax=qi*10+ba;select(4);P0=0xff;}
}
}
if(qujian==0){select(5);P0=0x00;}
if(qujian==1){select(5);P0=0x00;}
else if(qujian==2){select(5);P0=0x10;}
keyscan();
display1(yi,er);display2(san,si);display3(wu,liu);display4(qi,ba);
}
}