//#include <w77c32.h>
#include <reg52.h>
#include <absacc.h>
#include <INTRINS.h>
#define nop _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
#define uchar unsigned char
#define uint unsigned int
sbit KeyCom =P1^0;
sbit clk = P1^2; // clk 连接于 P3.5
sbit dat = P1^1; // dat 连接于 P3.7
sbit keyout = P3^2; // key 连接于 P3.3(INT0)
unsigned char KeyValue;
sbit bee =P1^7;
//---------------------------2005-12-22 键盘调试ok : 注:中断采用电平方式
void KeyInit(void);
unsigned char GetKeyvalue(void); //flag 1 中断 0:定时系统中接中键值
void Sendkeycom(unsigned char syscom,unsigned char sysdat); //发键盘命令 syscom 寄存器参数 sysdat :数据 (d0~d7)
uchar WaitKey(void);
//------------------------------------------------------------------------
//**************************************************************//
void WaitMs(uchar buf);
void SysInit(void);
uint keyint;//key time
//--------------------串行接收发送-----------------------
//-------------------------------------------------------
void main(void)
{uchar i;
bee=0;
SysInit();
ET0=0;
EA=1;
keyint=0;
KeyValue=0;
//------------------
while(1)
{
i=WaitKey();
nop;
}
}//end of main
//-----------------------------------------------------系统时钟
void Int1_systick(void) interrupt 1 //
{
uchar buf;
// TH0 = 0x8a; //32ms初值装载
// TL0 = 0x44;
TH0 = 0xFF; //32ms初值装载
TL0 = 0x44;
//-------------------------
buf=GetKeyvalue();
keyint++;
if(buf!=KeyValue)
{
ET0=0;
EX0=1;
keyint=0;
}
}
void Int0_key(void) interrupt 0 //
{
KeyValue=GetKeyvalue();
ET0=1;
EX0=0;
nop;
}
//-----------------------------------------------------------
void KeyInit(void) // 键盘初始化
{
Sendkeycom(0x04,0x02); //仅键盘使能
}
void Sendkeycom(unsigned char syscom,unsigned char sysdat)
{
uchar i;
clk=0;
KeyCom=0; //时钟线置0
for(i=0;i<8;i++)
{
if(sysdat&1) {dat=1;} else {dat=0;}
sysdat>>=1; clk=1; clk=0;
}
for(i=0;i<4;i++)
{
if(syscom&1) {dat=1;} else {dat=0;}
syscom>>=1; clk=1; clk=0;
}
dat=1;
KeyCom=1;
}
unsigned char GetKeyvalue(void)
{
uchar i, Keybuf=7;
clk=0;
KeyCom=0; //发出一 clk 脉冲
for(i=0;i<4;i++) //发送读键命令
{
if(Keybuf&1) {dat=1;} else {dat=0;}
Keybuf>>=1; clk=1; clk=0;
}
KeyCom=1;
Keybuf=0;
for(i=0;i<7;i++) //接收7位数据
{
clk=1; Keybuf<<=1;
if(keyout) { Keybuf=Keybuf|0x01;} // bit0=1
clk=0; // 输出一 clk 脉冲
}
return(Keybuf);
}
//----------------------------------------------------
void WaitMs(uchar buf)
{
uchar i=125;
for(;buf!=0;buf--)
{
for(;i!=0;i--) {nop;nop;}
i=125;
}
}
//---------------------------------------------------
void SysInit(void)
{
TMOD=0x21 ;TH0=0;TL0=0;TR0=1;ET0=0; //T0作为系统时钟
IT0=0;EX0=1; //外部中断0 电平触发方式 用于键盘中断
//IT1=1;EX1=1; //外部中断1
// TMOD=0X22;//,T0为方式2
KeyInit();
}
/****************************************************************************
* 名称:WaitKey()
* 功能:等待一个有效按键,并返回按键值。本函数有去抖功能。
* 入口参数:无
* 出口参数:返回按键盘值。
****************************************************************************/
uchar WaitKey(void)
{ uchar k;
KeyValue = 0;
while(!KeyValue); //接收一个键值
// k=KeyValue;
// while(KeyValue!=k);
while(keyint!=0);
return(KeyValue);
}