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

📄 lm3033_keyboard.c

📁 TOPWAR LM3033及4X4矩阵键盘实现代码。实现字符型LCD及矩阵键盘库
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************************************************
Copyright (C), 2007, Shenzhen University,college of information engneering.
File Name:	 LM3033_KeyBoard.c
Author:		   ZHOU DongYong 
Version:		 Version 1.0
Start:			 Sep 3,2007
Last update: Sep 3,2007
Description: Menu
************************************************************************************************************/
//include
#include "LM3033_KeyBoard_H.h"

/******************************************************************************
Function:	 delay_100us
Description:	delay 100us, the system clock is 8MHz
Calls:			- None
Input:			- None
Output:			- None
Return:			
******************************************************************************/
void delay_N100us(U16 dly) {
  U8 i;
  for(i = 0; i < 150; i++)
     asm("nop\n");
}
/******************************************************************************
Function:	 delay_Nms
Description:	delay dly ms, the system clock is 8MHz
Calls:			- None
Input:			- 0~65535
Output:			- None
Return:			
******************************************************************************/
void delay_Nms(U16 dly) {
  U16 k;
  U8 j;
  for(k = dly; k > 0; k--) {   //dly ms 
     for(j = 0; j < 10; j++)	 //1 ms
        delay_N100us(1);
  }
}
/******************************************************************************
Function:	 KeyScan
Description:	KeyScan for Matrix4 keyboard
Calls:			- None
Input:			- None
Output:			- None
Return:			- temp_1|temp_2 or 0
******************************************************************************/
U8 KeyScan(void)
{  //defines
   U8 temp_1,temp_2;
   KEYB_DATA_BUS = 0x0f;			         //pepare for key scan
   asm("nop\n");
   KEYB_DATA_DDR = 0xf0;	             //high 4 bits output 0, low 4 bits input;
   if((KEYB_DATA_BUS&0x0f) != 0x0f)    //read inputs, key pressed then define key
   {
   	  delay_Nms(20);                   //delay for sure
	    if((KEYB_DATA_BUS&0x0f) != 0x0f)
	    {
	       temp_1 = (KEYB_DATA_BUS&0x0f);//get column value
		 
		     KEYB_DATA_BUS = 0xf0;
		     asm("nop");
		     KEYB_DATA_DDR = 0x0f;		    //then high 4 bits input, low 4 bits output 0;
		     delay_Nms(1);
		     temp_2 = (KEYB_DATA_BUS&0xf0);    //get line value
		     while((KEYB_DATA_BUS&0xf0)!=0xf0);//wait for key off
		     return temp_1|temp_2;	           //or the store value and return
	    }  
   }
   return  0;  
}
/******************************************************************************
Function:	 Match_KeyInput
Description:	key code match for  Matrix4 keyboard
Calls:			- None
Input:			- key_value:key_value  got from keyscan
Output:			- None 
Return:			- Flag:if key press,then Flag = 1,otherwise 0
               key_match:0~14,if key exist
******************************************************************************/
U8 Match_KeyInput(U8 key_value,U8 *key_match) {
  
  U8 Flag= TRUE;
  switch(key_value) {
    case KEY11:
    //1
    *key_match= 1;
    break;
    case KEY12:
    //2
    *key_match=2;
    break;
    case KEY13:
    //3
    *key_match=3;
    break;     
    case KEY14:
    //A
    *key_match=10;
    break;   
    case KEY21:
    //4
    *key_match=4;
    break;
    case KEY22:
    //5
    *key_match=5;
    break;
    case KEY23:
    //6
    *key_match=6;
    break;       
    case KEY24:
    //B
    *key_match=11;
    break;         
    case KEY31:
    //7
    *key_match=7;
    break;
    case KEY32:
    //8
    *key_match=8;
    break;
    case KEY33:
    //9
    *key_match=9;
    break; 
    case KEY34: 
    //C
    *key_match=12;
    break;   
    case KEY41:
    //NO
    *key_match=14;
    break; 
    case KEY42:
    //0
    *key_match=0;
    break;        
    case KEY43:
    //,
    *key_match=15;            
    case KEY44:
    //D     
    *key_match=13;
    break;                
    default:
      Flag= FALSE;
     break;
  }
   return Flag;

}
/******************************************************************************
Function:	 Number_Output
Description:set number with Matrix4 keyboard output on the LCD,Ctr and end;
Calls:			- None
Input:			- key_value
Output:			- None
Return:			
******************************************************************************/
U16 Number_Set(U8 x,U8 y){ 
// 
  U8 key_value_temp,temp;
  U16 value_set=0;
  LCD_Coordinate(x,y);
  do{
     PORTA = 0x0f;
     DDRA = 0xf0;		  //lines output 0 ,columns input,wait for pull down
     if(Flag_Key) {   //wait for key interrupt
      	 Flag_Key = 0;//reset key state
         temp=Match_KeyInput(key_value,&key_value_temp);//get key valeu
         if(temp&&key_value_temp<10){										//check numbers or not
            LCD_WriteChar(1,key_value_temp+0x30);       //display,change key_value to ascii
            value_set=value_set*10+(U16)key_value_temp; //caculate value input
         }
     }
  }while(key_value_temp!=12);														//end while Ctr
  return value_set;																			//return key value
}
/******************************************************************************
Function:	 WaitForKey
Description:wait for any key press down
Calls:			- None
Input:			- key_value
Output:			- None
Return:			
******************************************************************************/
void WaitForKey(void) {
  U8 temp=0,key_value_temp;
  //wait for any key press
  do{
     PORTA = 0x0f;
     DDRA = 0xf0;
     if(Flag_Key){         //key interrupt
        Flag_Key = 0;
        temp=Match_KeyInput(key_value,&key_value_temp);
     }
  }while(!temp);
}
/************************************************************************************************
Function:	 LCD_WriteChar
Description:write a byte to the lcd , command or data
Calls:			- None
Input:			- U8 sel,U8 data ; sel = 1 ,the data is for output,while sel = 0 ,the data is command
Output:			- None
Return:			- None
**************************************************************************************************/
void LCD_WriteChar(U8 sel,U8 data) { 
//   
    S8 i;
   //initial state
    LCD_CS_CLR;
    LCD_SCLK_CLR;             
    LCD_SID_CLR;
    //chip select enable
    LCD_CS_SET;
    //send
    //send 5 "1"
    for(i = 0;i < 5;i ++) {
        LCD_SID_SET;
        LCD_SCLK_SET;
        LCD_SCLK_CLR;  
    }
    //send 1 "0"
    LCD_SID_CLR;
    LCD_SCLK_SET;
    LCD_SCLK_CLR;
    //send RS  :data 1;command 0;
    if(sel) LCD_SID_SET;//data  sid = 1
    else    LCD_SID_CLR;//command sid = 0  
    LCD_SCLK_SET;
    LCD_SCLK_CLR;
    //send 1 "0"
    LCD_SID_CLR;
    LCD_SCLK_SET;
    LCD_SCLK_CLR;
    //send data high 4 bits
    for(i = 7;i >= 4;i --) {
        if(data&(1<<i)) //current bit is "1"
            LCD_SID_SET;     
        else            //current bit is "0"
            LCD_SID_CLR; 
        LCD_SCLK_SET;
        LCD_SCLK_CLR;
    }

⌨️ 快捷键说明

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