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

📄 keypad.c

📁 使用Keil uVision2下C语言开发的防盗报警系统
💻 C
字号:
/*------------------------------------------------------------------*-

   Keypad.C (v1.00)

  ------------------------------------------------------------------

   Simple library, for a switch-based "keypad".

   COPYRIGHT
   ---------

   This code is associated with the book:

   EMBEDDED C by Michael J. Pont 
   [Pearson Education, 2002: ISBN: 0-201-79523-X].

   This code is copyright (c) 2001 by Michael J. Pont.
 
   See book for copyright details and other information.

-*------------------------------------------------------------------*/

#include "Main.h"
#include "Port.h"

#include "Keypad.h"

// ------ Private function prototypes ------------------------------

bit KEYPAD_Scan(char* const);

// ------ Private constants ----------------------------------------

#define KEYPAD_RECV_BUFFER_LENGTH 6

// Any valid character will do - must not match anything on keypad
#define KEYPAD_NO_NEW_DATA (char) '-' 

// ------ Private variables ----------------------------------------

static char KEYPAD_recv_buffer[KEYPAD_RECV_BUFFER_LENGTH+1];

static tByte KEYPAD_in_read_index;     // Data in buffer that has been read 
static tByte KEYPAD_in_waiting_index;  // Data in buffer not yet read

static char Last_valid_key_G = KEYPAD_NO_NEW_DATA;

static data char Old_key_G;

/*------------------------------------------------------------------*-

  KEYPAD_Init()

  Init the keypad.

-*------------------------------------------------------------------*/
void KEYPAD_Init(void)
   {
   KEYPAD_in_read_index = 0;
   KEYPAD_in_waiting_index = 0;
   }

/*------------------------------------------------------------------*-

  KEYPAD_Update()

  The main 'update' function for the keypad library.

  Must call this function approx every 50 - 200 ms.

-*------------------------------------------------------------------*/
void KEYPAD_Update(void)
   {
   char Key;

   // Scan keypad here...
   if (KEYPAD_Scan(&Key) == 0)
      {
      // No new key data - just return
      return;
      }

   // Want to read into index 0, if old data has been read
   // (simple ~circular buffer)
   if (KEYPAD_in_waiting_index == KEYPAD_in_read_index)
      { 
      KEYPAD_in_waiting_index = 0;
      KEYPAD_in_read_index = 0;
      } 
   
   // Load keypad data into buffer
   KEYPAD_recv_buffer[KEYPAD_in_waiting_index] = Key;

   if (KEYPAD_in_waiting_index < KEYPAD_RECV_BUFFER_LENGTH)
      {
      // Increment without overflowing buffer
      KEYPAD_in_waiting_index++;
      }
   }


/*------------------------------------------------------------------*-

  KEYPAD_Get_Char_From_Buffer()

  The Update function copies data into the keypad buffer.  
  This function extracts data from the buffer.
  
-*------------------------------------------------------------------*/

bit KEYPAD_Get_Data_From_Buffer(char* const pKey)
   {
   // If there are new data in the buffer
   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;
   }


/*------------------------------------------------------------------*-

  KEYPAD_Clear_Buffer()

-*------------------------------------------------------------------*/
void KEYPAD_Clear_Buffer(void)
   {
   KEYPAD_in_waiting_index = 0;
   KEYPAD_in_read_index = 0;
   }

/*------------------------------------------------------------------*-

  KEYPAD_Scan()

  This function is called from scheduled keypad function.

  Must be edited as required to match your key labels.

  Adapt as required!
 
-*------------------------------------------------------------------*/
bit KEYPAD_Scan(char* const pKey)
   {
   char Key = KEYPAD_NO_NEW_DATA;

   if (K0 == 0) { Key = '0'; }
   if (K1 == 0) { Key = '1'; }
   if (K2 == 0) { Key = '2'; }
   if (K3 == 0) { Key = '3'; }
   if (K4 == 0) { Key = '4'; }
   if (K5 == 0) { Key = '5'; }
   if (K6 == 0) { Key = '6'; }
   if (K7 == 0) { Key = '7'; }

   if (Key == KEYPAD_NO_NEW_DATA)
      {
      // No key pressed 
      Old_key_G = KEYPAD_NO_NEW_DATA;
      Last_valid_key_G = KEYPAD_NO_NEW_DATA;

      return 0;  // No new data
      }
   
   // A key has been pressed: debounce by checking twice
   if (Key == Old_key_G)
      {
      // A valid (debounced) key press has been detected
      
      // Must be a new key to be valid - no 'auto repeat'
      if (Key != Last_valid_key_G)
         { 
         // New key!
         *pKey = Key;
         Last_valid_key_G = Key;

         return 1;
         }
      }

   // No new data
   Old_key_G = Key;
   return 0;
   }

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

⌨️ 快捷键说明

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