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

📄 keypad.c

📁 基于MFRC500读卡芯片的完整读卡程序
💻 C
字号:
/*--------------------------------------------------------------------
 Keypad.C 
 4*3键盘基本函数库
---------------------------------------------------------------------                    
                          1  2  3
                          4  5  6
                          7  8  9
                          *  0  #                       
--------------------------------------------------------------------*/
#include "Main.h"
#include "PORT.h"

// ------ 私有函数原型 ------------------------------
bit KEYPAD_Scan(char* const);

// ------ 私有常量 ----------------------------------------
#define KEYPAD_RECV_BUFFER_LENGTH 8
#define KEYPAD_NO_NEW_DATA (char) '-' 

// ------ 私有变量 ----------------------------------------
static char  KEYPAD_recv_buffer[KEYPAD_RECV_BUFFER_LENGTH+1];
static tByte KEYPAD_in_read_index;        // 已读键盘缓冲区计数
static tByte KEYPAD_in_waiting_index;     // 没读键盘缓冲区计数
static char  Last_valid_key_G = KEYPAD_NO_NEW_DATA;

/*------------------------------------------------------------------*-
初始化键盘
-*------------------------------------------------------------------*/
void KEYPAD_Init(void)
   {
   KEYPAD_in_read_index = 0;
   KEYPAD_in_waiting_index = 0;
   }
/*------------------------------------------------------------------*-
写缓冲区 
(every 50 - 200 ms 被调度一次).
-*------------------------------------------------------------------*/
void KEYPAD_Update(void)
   {
    char Key;

   // 键盘扫描
   if (KEYPAD_Scan(&Key) == 0)
      {
      //没数据 - just return
      return;
      }

   // 如果缓冲区数据读完,则清读取指针为0
  
   if (KEYPAD_in_waiting_index == KEYPAD_in_read_index)
      { 
      KEYPAD_in_waiting_index = 0;
      KEYPAD_in_read_index = 0;
      } 
   
   // 加载数据到缓冲区
   KEYPAD_recv_buffer[KEYPAD_in_waiting_index] = Key;
  

   if (KEYPAD_in_waiting_index < KEYPAD_RECV_BUFFER_LENGTH)
      {
      // 加1且缓冲区不溢出
      KEYPAD_in_waiting_index++;
      }
   }
/*------------------------------------------------------------------*-
读缓冲区
-*------------------------------------------------------------------*/
bit KEYPAD_Get_Data_From_Buffer(char* const pKey)
   {
   // 如果缓冲区有数据
   if (KEYPAD_in_read_index < KEYPAD_in_waiting_index)
      {
      *pKey = KEYPAD_recv_buffer[KEYPAD_in_read_index];
   
      KEYPAD_in_read_index++;

      return 1;
      }

   return 0;
   }


/*------------------------------------------------------------------*-
清缓冲区
-*------------------------------------------------------------------*/
void KEYPAD_Clear_Buffer(void)
   {
   KEYPAD_in_waiting_index = 0;
   KEYPAD_in_read_index = 0;
   }

/*------------------------------------------------------------------*-
键盘扫描
-*------------------------------------------------------------------*/
bit KEYPAD_Scan(char* const pKey)
   {
   static data char Old_Key;

   char Key = KEYPAD_NO_NEW_DATA;


   C1 = 0; // 扫描列 1
      if (R1 == 0) Key = '1';
      if (R2 == 0) Key = '4';
      if (R3 == 0) Key = '7';
      if (R4 == 0) Key = '*';
   C1 = 1;

   C2 = 0; // 扫描列 2
      if (R1 == 0) Key = '2';
      if (R2 == 0) Key = '5';
      if (R3 == 0) Key = '8';
      if (R4 == 0) Key = '0';
   C2 = 1;

   C3 = 0; // 扫描列 3
      if (R1 == 0) Key = '3';
      if (R2 == 0) Key = '6';
      if (R3 == 0) Key = '9';
      if (R4 == 0) Key = '#';
   C3 = 1;

   if (Key == KEYPAD_NO_NEW_DATA)
      {
      // 没有键按下
      Old_Key = KEYPAD_NO_NEW_DATA;
      Last_valid_key_G = KEYPAD_NO_NEW_DATA;

      return 0;  // 没新数据
      }
   
   // 一个按键被按下: 通过检测两次来消抖
   if (Key == Old_Key)
      {
      //检测到一个有效的(经过消抖)键按下
      
      // 必须是一个新的按键才有效,不允许自动重复
      if (Key != Last_valid_key_G)
         { 
         
         *pKey = Key;
         Last_valid_key_G = Key;

         return 1;
         }
      }

   // 没新数据
   Old_Key = Key;
   return 0;
   }

/*------------------------------------------------------------------*-
  ---- END OF FILE -------------------------------------------------
-*------------------------------------------------------------------*/

⌨️ 快捷键说明

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