⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 key_t.h

📁 一般我们单片机同一端口有8个引脚,用来作键盘的话只可以实现4*4的矩阵,本源码介绍了如何在不使用芯片进行键盘扩展时的键盘扫描方法,以20(5*4)键为例.
💻 H
字号:
/*键盘扫描*/

#include <reg51.h>
#include "intrins.h"

#define NOP _nop_()
#define LOW  0
#define HIGH 1

/******************************************************************************/
#ifndef __KEY_T_H_
#define __KEY_T_H_
/******************************************************************************/

#ifdef _KEY_T_C_
#define GLOBAL
#else
#define GLOBAL extern
#endif
/*
example:
GLOBAL char variable
GLOBAL bit flag
*/

/***************定义键盘行列对应的芯片端口引脚**************/

/*************定义扫描行,分别对应第一行至第五行*************/
sbit KeyboardLine01 = P2 ^ 0;
sbit KeyboardLine02 = P2 ^ 1;
sbit KeyboardLine03 = P2 ^ 2;
sbit KeyboardLine04 = P2 ^ 3;
sbit KeyboardLine05 = P2 ^ 4;

/*************定义扫描列,分别对应第一列至第四列*************/
sbit KeyboardRow01  = P2 ^ 5;
sbit KeyboardRow02  = P2 ^ 6;
sbit KeyboardRow03  = P2 ^ 7;
sbit KeyboardRow04  = P3 ^ 5;

/******************定义发光二极管端口引脚******************/
sbit LED_GREEN = P3 ^ 6;

/******************定义无源蜂鸣器端口引脚******************/
sbit SOUND     = P1 ^ 3;

/**********************************************************
***   定义发送参数,SendNumber用于控制是单个发送还是     ***
***   成批发送;KeyValue值送往SBUF中.                    ***
**********************************************************/
unsigned char SendNumber,KeyValue;

/*******************记录按键的前一个状态值******************/
unsigned char KeyFrontValue = 0xff;


/************************************************************
***   定义键盘扫描值(按键接通码),键盘布键值如下:          ***
***      Esc   F1   F2   F3                               ***
***       7     8    9   F4                               ***
***       4     5    6   F5                               ***
***       1     2    3   F6                               ***
***      Clear  0    .   Enter                            ***
************************************************************/
unsigned char KeyboardValue[20] = {0x76,0x05,0x06,0x04,
                              0x3d,0x3e,0x46,0x0c,
                              0x25,0x2e,0x36,0x03,
                              0x16,0x1e,0x26,0x0b,
                              0x66,0x45,0x49,0x5a};


unsigned char ASCIIValue[20] = {
	0x1b, 0x41, 0x42, 0x43,
	0x37, 0x38, 0x39, 0x44,
	0x34, 0x35, 0x36, 0x45,
	0x31, 0x32, 0x33, 0x46,
	0x08, 0x30, 0x2e, 0x0d
};
/***********************************************************/
/*         F u n c t i o n      D e f i n e                                   */
/***********************************************************/
void InitInterrupt();

/*********键盘延时程序,用于软件延时来消除按键的抖动*********/
void Keyboard_DelayMs( unsigned char x);

/*********************判断是否有键按下 *********************/
unsigned char Keyboard_Scannign();

/*************通过移位的算法对键盘进行逐个的扫描************/
unsigned char Keyboard_Read_Value();


unsigned char Keyboard_Scanning()
{
	KeyboardLine01 = LOW;
	KeyboardLine02 = LOW;
	KeyboardLine03 = LOW;
	KeyboardLine04 = LOW;
	KeyboardLine05 = LOW;

	KeyboardRow01  = HIGH;
	KeyboardRow02  = HIGH;
	KeyboardRow03  = HIGH;
	KeyboardRow04  = HIGH;

	if(KeyboardRow01 & KeyboardRow02 & KeyboardRow03 & KeyboardRow04 )
	{
		return 0x00;
	}
	else
	{
		return 0xff;
	}
}

unsigned char Keyboard_Read_Value()
{
	KeyboardLine01 = LOW;
	KeyboardLine02 = HIGH;
	KeyboardLine03 = HIGH;
	KeyboardLine04 = HIGH;
	KeyboardLine05 = HIGH;

	KeyboardRow01  = HIGH;
	KeyboardRow02  = HIGH;
	KeyboardRow03  = HIGH;
	KeyboardRow04  = HIGH;

	//判断第一行是否有键按下
	if( KeyboardRow01 == LOW )
	{
		return ( 0x00 );
	}
	else if( KeyboardRow02 == LOW )
	{
		return ( 0x01 );
	}
	else if( KeyboardRow03 == LOW )
	{
		return ( 0x02 );
	}
	else if( KeyboardRow04 ==LOW )
	{
		return ( 0x03 );
	}
	else
	{
		KeyboardLine01 = HIGH;
		KeyboardLine02 = LOW;
	}

	//判断第二行是否有键按下
	if( KeyboardRow01 == LOW )
	{
		return ( 0x04 );
	}
	else if( KeyboardRow02 == LOW )
	{
		return ( 0x05 );
	}
	else if( KeyboardRow03 == LOW )
	{
		return ( 0x06 );
	}
	else if( KeyboardRow04 ==LOW )
	{
		return ( 0x07 );
	}
	else
	{
		KeyboardLine02 = HIGH;
		KeyboardLine03 = LOW;
	}

	//判断第三行是否有键按下
	if( KeyboardRow01 == LOW )
	{
		return ( 0x08 );
	}
	else if( KeyboardRow02 == LOW )
	{
		return ( 0x09 );
	}
	else if( KeyboardRow03 == LOW )
	{
		return ( 0x0a );
	}
	else if( KeyboardRow04 ==LOW )
	{
		return ( 0x0b );
	}
	else
	{
		KeyboardLine03 = HIGH;
		KeyboardLine04 = LOW;
	}

	//判断第四行是否有键按下
	if( KeyboardRow01 == LOW )
	{
		return ( 0x0c );
	}
	else if( KeyboardRow02 == LOW )
	{
		return ( 0x0d );
	}
	else if( KeyboardRow03 == LOW )
	{
		return ( 0x0e );
	}
	else if( KeyboardRow04 ==LOW )
	{
		return ( 0x0f );
	}
	else
	{
		KeyboardLine04 = HIGH;
		KeyboardLine05 = LOW;
	}

	//判断第五行是否有键按下
	if( KeyboardRow01 == LOW )
	{
		return ( 0x10 );
	}
	else if( KeyboardRow02 == LOW )
	{
		return ( 0x11 );
	}
	else if( KeyboardRow03 == LOW )
	{
		return ( 0x12 );
	}
	else if( KeyboardRow04 ==LOW )
	{
		return ( 0x13 );
	}
	else
	{
		KeyboardLine05 = HIGH;
		return ( 0x0ee );
	}
}

void Keyboard_DelayMs( unsigned char x )
{
   unsigned char i = 0x00;

   while( x-- )
   {
      i = 0xc8;
      do
      {
         NOP;
         NOP;
         NOP;
         i--;
      }while( i );
   }
}

#undef GLOBAL


/***********************************************************/
#endif
/***********************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -