2013-10-24 14:41 上传
下载附件 (94.58 KB)
可切换1X4按键和4X4按键在AT89S52上的原理图
//1X4按键程序
#include "reg51.h"
#include <intrins.h>
#include <absacc.h>
#define uchar unsigned char //定义unsigned char 为 uchar
#define uint unsigned int
#define keyport P3 //lcd忙检测标志
#define disport P0
uchar key,temp;
uchar code dis[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7C,0x39,0x5E,0x79,0x71};
sbit f1=P3^0;
sbit f2=P3^1;
sbit f3=P3^2;
sbit f4=P3^3;
/***************一微秒延时子程序******************/
void delayus(uint delay){
while(delay--);
}
/***************一毫秒延时子程序******************/
void delayms(uint delay){
while(delay--)
delayus(149);
}
void main()
{
P2=0x04;
if(!f1)
disport=dis[1];
if(!f2)
disport=dis[2];
if(!f3)
disport=dis[3];
if(!f4)
disport=dis[4];
}
//4X4按键程序
#include "reg51.h"
#include <intrins.h>
#include <absacc.h>
#define uchar unsigned char //定义unsigned char 为 uchar
#define uint unsigned int
#define keyport P3 //lcd忙检测标志
#define disport P0
uchar key,temp;
uchar code dis[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7C,0x39,0x5E,0x79,0x71};
/***************一微秒延时子程序******************/
void delayus(uint delay){
while(delay--);
}
/***************一毫秒延时子程序******************/
void delayms(uint delay){
while(delay--)
delayus(100);
}
/******************************************************/
uchar getkey()
{
keyport=0xef; //扫描第三列
temp=keyport;
if(temp!=0xef)
{delayms(10);
temp=keyport;
if(temp!=0xef)
while((keyport&0x0F)!=0x0F) ;
switch(temp)
{
case 0xee: key=0;break;
case 0xed: key=1;break;
case 0xeb: key=2;break;
case 0xe7: key=3;break;
}
return key; //返回键值
}
keyport=0xdf; //扫描第一列
temp=keyport;
if(temp!=0xdf)
{delayms(10);
temp=keyport;
if(temp!=0xdf)
while((keyport&0x0f)!=0x0f) ;
switch(temp)
{
case 0xde: key=4;break;
case 0xdd: key=5;break;
case 0xdb: key=6;break;
case 0xd7: key=7;break;
}
return key; //返回键值
}
keyport=0xbf; //扫描第二列
temp=keyport;
if(temp!=0xbf)
{delayms(10);
temp=keyport;
if(temp!=0xbf)
while((keyport&0x0F)!=0x0F) ;
switch(temp)
{
case 0xbe: key=8;break;
case 0xbd: key=9;break;
case 0xbb: key=10;break;
case 0xb7: key=11;break;
}
return key; //返回键值
}
keyport=0x7f; //扫描第三列
temp=keyport;
if(temp!=0x7f)
{delayms(10);
temp=keyport;
if(temp!=0x7f)
while((keyport&0x0F)!=0x0F) ;
switch(temp)
{
case 0x7e: key=12;break;
case 0x7d: key=13;break;
case 0x7b: key=14;break;
case 0x77: key=15;break;
}
return key; //返回键值
}
}
void main()
{
EA = 0;
while(1)
{
disport=dis[getkey()] ;
P2=0x04;
}
}
|
|