通过改变KEY的值改变模式
.h
#ifndef _key_h
#define _key_h
#include<STC15F2K60S2.h>
#define u8 unsigned char
#define u16 unsigned int
#define KEY 1
sbit key1=P4^4;
sbit key2=P4^2;
sbit key3=P3^5;
sbit key4=P3^4;
sbit key_row1=P3^0;
sbit key_row2=P3^1;
sbit key_row3=P3^2;
sbit key_row4=P3^3;
extern u8 key_value;
extern bit key_flag;
void Key_Scan();
void Key_Judge();
#define _key_h
#include<STC15F2K60S2.h>
#define u8 unsigned char
#define u16 unsigned int
#define KEY 1
sbit key1=P4^4;
sbit key2=P4^2;
sbit key3=P3^5;
sbit key4=P3^4;
sbit key_row1=P3^0;
sbit key_row2=P3^1;
sbit key_row3=P3^2;
sbit key_row4=P3^3;
extern u8 key_value;
extern bit key_flag;
void Key_Scan();
void Key_Judge();
#endif
.c
#include<key.h>
u8 key_value=0;
bit key_flag=0;
#if KEY
u8 pdata key_catch[4]={0xff,0xff,0xff,0xff};
u8 pdata key_backup[4]={1,1,1,1};
u8 pdata key_state[4]={1,1,1,1};
u8 code key_button[4]={1,2,3,4};
u8 key_value=0;
bit key_flag=0;
#if KEY
u8 pdata key_catch[4]={0xff,0xff,0xff,0xff};
u8 pdata key_backup[4]={1,1,1,1};
u8 pdata key_state[4]={1,1,1,1};
u8 code key_button[4]={1,2,3,4};
void Key_Scan()
{
u8 i=0;
key_catch[0]=key_catch[0]<<1|keyrow1;
key_catch[1]=key_catch[1]<<1|keyrow2;
key_catch[2]=key_catch[2]<<1|keyrow3;
key_catch[3]=key_catch[3]<<1|keyrow4;
for(;i<4;i++)
{
if((key_catch[i]&0xff)==0xff)
{
key_state[i]=1;
}
else if((key_catch[i]&0xff)==0x00)
{
key_state[i]=0;
}
}
{
u8 i=0;
key_catch[0]=key_catch[0]<<1|keyrow1;
key_catch[1]=key_catch[1]<<1|keyrow2;
key_catch[2]=key_catch[2]<<1|keyrow3;
key_catch[3]=key_catch[3]<<1|keyrow4;
for(;i<4;i++)
{
if((key_catch[i]&0xff)==0xff)
{
key_state[i]=1;
}
else if((key_catch[i]&0xff)==0x00)
{
key_state[i]=0;
}
}
}
void Key_Judge()
{
u8 i=0;
for(;i<4;i++)
{
if(key_state[i]!=key_backup[i])
{
if(key_backup[i]==0)
{
key_value=key_button[i];
key_flag=1;
}
key_backup[i]=key_state[i];
}
}
}
{
u8 i=0;
for(;i<4;i++)
{
if(key_state[i]!=key_backup[i])
{
if(key_backup[i]==0)
{
key_value=key_button[i];
key_flag=1;
}
key_backup[i]=key_state[i];
}
}
}
#else
u8 pdata key_catch[4][4]={0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff};
u8 pdata key_backup[4][4]={1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1};
u8 pdata key_state[4][4]={1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1};
u8 code key_button[4][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
u8 pdata key_catch[4][4]={0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff};
u8 pdata key_backup[4][4]={1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1};
u8 pdata key_state[4][4]={1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1};
u8 code key_button[4][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
void Key_Scan()
{
u8 i=0;
static u8 row=0;
key_catch[row][0]=key_catch[row][0]<<1|keyrow1;
key_catch[row][1]=key_catch[row][1]<<1|keyrow2;
key_catch[row][2]=key_catch[row][2]<<1|keyrow3;
key_catch[row][3]=key_catch[row][3]<<1|keyrow4;
for(;i<4;i++)
{
if((key_catch[row][i]&0x0f)==0x0f)
{
key_state[row][i]=1;
}
else if((key_catch[row][i]&0x0f)==0x00)
{
key_state[row][i]=0;
}
}
row++;
row&=0x03;
switch(row)
{
case 0:key1=0;key4=1;break;
case 1:key2=0;key1=1;break;
case 2:key3=0;key2=1;break;
case 3:key4=0;key3=1;break;
default:break;
}
}
{
u8 i=0;
static u8 row=0;
key_catch[row][0]=key_catch[row][0]<<1|keyrow1;
key_catch[row][1]=key_catch[row][1]<<1|keyrow2;
key_catch[row][2]=key_catch[row][2]<<1|keyrow3;
key_catch[row][3]=key_catch[row][3]<<1|keyrow4;
for(;i<4;i++)
{
if((key_catch[row][i]&0x0f)==0x0f)
{
key_state[row][i]=1;
}
else if((key_catch[row][i]&0x0f)==0x00)
{
key_state[row][i]=0;
}
}
row++;
row&=0x03;
switch(row)
{
case 0:key1=0;key4=1;break;
case 1:key2=0;key1=1;break;
case 2:key3=0;key2=1;break;
case 3:key4=0;key3=1;break;
default:break;
}
}
void Key_Judge()
{
u8 i=0,j=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(key_state[i][j]!=key_backup[i][j])
{
if(key_backup[i][j]==0)
{
key_value=key_button[i][j];
key_flag=1;
}
key_backup[i][j]=key_state[i][j];
}
}
}
}
{
u8 i=0,j=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(key_state[i][j]!=key_backup[i][j])
{
if(key_backup[i][j]==0)
{
key_value=key_button[i][j];
key_flag=1;
}
key_backup[i][j]=key_state[i][j];
}
}
}
}
#endif