#include<reg52.h>
#include"diy.h"
#define key_state1 1
#define key_state2 2
#define key_state3 3
unsigned char const dofly[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71};//0-F
void Key_Scan()
{
uint8 value_l,value_r,key_value,table;
static uint8 key_state=1;
switch(key_state)
{
case key_state1:
PORT3=0XF0;
value_l=PORT3&0XF0;
if(value_l!=0XF0)
{
key_state=key_state2;
value_l=PORT3&0Xf0;
PORT3=0X0F;
value_r=PORT3&0x0f;
key_value=(value_l|value_r);
}
else
{key_state=key_state1;}
break;
case key_state2:
{
switch(key_value)
{
case 0x7e: table=dofly[0];key_state=2;break;
case 0x7d: table=dofly[1];key_state=2;break;
case 0x7b: table=dofly[2];key_state=2;break;
case 0x77: table=dofly[3];key_state=2;break;
case 0xbe: table=dofly[4];key_state=2;break;
case 0xbd: table=dofly[5];key_state=2;break;
case 0xbb: table=dofly[6];key_state=2;break;
case 0xb7: table=dofly[7];key_state=2;break;
case 0xde: table=dofly[8];key_state=2;break;
case 0xdd: table=dofly[9];key_state=2;break;
case 0xdb: table=dofly[10];key_state=2;break;
case 0xd7: table=dofly[11];key_state=2;break;
case 0xee: table=dofly[12];key_state=2;break;
case 0xed: table=dofly[13];key_state=2;break;
case 0xeb: table=dofly[14];key_state=2;break;
case 0xe7: table=dofly[15];key_state=2;break;
default :key_state=key_state1;break;
}
}
break;
case key_state3:
if(value_l!=0XF0)
//while(value_l!=0XF0)
{
PORT2=0x00;
PORT0=dofly[table];
}
else
{
key_state=key_state1;
}
break;
}
}
void main()
{
while(1)
{
Key_Scan();
}
}
这是我根据状态机的思想写的单片机矩阵键盘扫描程序,用的是STC的单片机89c54系列,12M的晶振程序没有错误,但是功能实现不了,还请各位指点,我是在束手无策了!!!谢谢! |