#include "hw_memmap.h" // hw_memmap.h - Macros defining the memory map of Stellaris. #include "hw_types.h" // hw_types.h - Common types and macros. 定义了一个tBoolean(unsigned char),定义位带 #include "hw_sysctl.h" #include "sysctl.h" // sysctl.h - Prototypes for the system control driver. #include "rom.h" // rom.h - Macros to facilitate calling functions in the ROM. #include"hw_gpio.h" #include "gpio.h" // gpio.h - Defines and Macros for GPIO API. #include "hw_ints.h" // hw_ints.h - Macros that define the interrupt assignment on Stellaris. //#include"key.h" //原来下面的宏定义是在头文件里面的,为了方便看就放负复制到key.C来了一样用
#define uchar unsigned char #define uint unsigned int #define ulong unsigned long //extern void Delay(ulong ulVal)
//#ifndef KEYBOARD_H //#define KEYBOARD_H //定义长标识符成较短的形式 #define SysCtlPeriEnable SysCtlPeripheralEnable #define SysCtlPeriDisable SysCtlPeripheralDisable #define GPIOPinTypeIn GPIOPinTypeGPIOInput #define GPIOPinTypeOutOD GPIOPinTypeGPIOOutputOD
//行线使用GPIOF #define KEYBOARD_LIN_PERIPH SYSCTL_PERIPH_GPIOF #define KEYBOARD_LIN_BASE GPIO_PORTF_BASE #define KEYBOARD_LIN_PIN GPIO_PIN_0 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 // 0x0F //列线使用GPIOH #define KEYBOARD_COL_PERIPH SYSCTL_PERIPH_GPIOH #define KEYBOARD_COL_BASE GPIO_PORTH_BASE #define KEYBOARD_COL_PIN GPIO_PIN_0 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 // 0x0F ////读取按键的函数 //extern uchar scanKeyboard(void); ////键盘初始化函数 //extern uchar KeyboardInit(void); ////将获取的按键转化为相应的指令,0x1E1F代表非法指令或者未按键 //extern void keyProcess(void); ////#endif
uchar scanKeyboard(void) { ulong scanValue[16]={0xEE,0xDE,0xBE,0x7E, //定义键值 0xED,0xDD,0xBD,0x7D, 0xEB,0xDB,0xBB,0x7B, 0xE7,0xD7,0xB7,0x77, }; const uchar keyASC[20]={1,2,3,4, //返回键号 5,6,7,8, 9,10,11,12, 13,14,15,16
}; //定义变量及使能GPIO ulong key,temp,temp1,temp2,temp3; SysCtlPeripheralEnable(KEYBOARD_LIN_PERIPH); SysCtlPeripheralEnable(KEYBOARD_COL_PERIPH); //8个引脚全部上拉电阻有效 GPIOPadConfigSet(KEYBOARD_LIN_BASE,KEYBOARD_LIN_PIN,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_OD_WPU); GPIOPadConfigSet(KEYBOARD_COL_BASE,KEYBOARD_COL_PIN,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_OD_WPU); GPIOPinTypeOutOD(KEYBOARD_LIN_BASE,KEYBOARD_LIN_PIN); //设置为输出(行线)GPIOF(0-3) GPIOPinTypeIn(KEYBOARD_COL_BASE,KEYBOARD_COL_PIN); //设置为输入(列线)GPIOH(0-3) GPIOPinWrite(KEYBOARD_LIN_BASE,KEYBOARD_LIN_PIN,0x00); //行线输出0 temp1=GPIOPinRead(KEYBOARD_COL_BASE,KEYBOARD_COL_PIN)& KEYBOARD_COL_PIN; //读列线的值 temp1=temp1<<4;
GPIOPadConfigSet(KEYBOARD_LIN_BASE,KEYBOARD_LIN_PIN,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_OD_WPU); GPIOPadConfigSet(KEYBOARD_COL_BASE,KEYBOARD_COL_PIN,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_OD_WPU); GPIOPinTypeIn(KEYBOARD_LIN_BASE,KEYBOARD_LIN_PIN); //设置为输入(行线) GPIOPinTypeOutOD(KEYBOARD_COL_BASE,KEYBOARD_COL_PIN); //设置为输出(列线) GPIOPinWrite(KEYBOARD_COL_BASE,KEYBOARD_COL_PIN,0x00); //列线输出0 temp2=GPIOPinRead(KEYBOARD_LIN_BASE,KEYBOARD_LIN_PIN)& KEYBOARD_LIN_PIN; //读行线的值 temp=temp1 | temp2;
if(temp!=0xFF) //有键按下 { // Delay(13*SysCtlClockGet()/4000); //延时去抖动 SysCtlDelay(13*(SysCtlClockGet()/3000)); GPIOPadConfigSet(KEYBOARD_LIN_BASE,KEYBOARD_LIN_PIN,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_OD_WPU); GPIOPadConfigSet(KEYBOARD_COL_BASE,KEYBOARD_COL_PIN,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_OD_WPU); GPIOPinTypeOutOD(KEYBOARD_LIN_BASE,KEYBOARD_LIN_PIN); //设置为输出(行线) GPIOPinTypeIn(KEYBOARD_COL_BASE,KEYBOARD_COL_PIN); //设置为输入(列线) GPIOPinWrite(KEYBOARD_LIN_BASE,KEYBOARD_LIN_PIN,0x00); //行线输出0 temp1=GPIOPinRead(KEYBOARD_COL_BASE,KEYBOARD_COL_PIN)& KEYBOARD_COL_PIN; temp1=temp1<<4; GPIOPadConfigSet(KEYBOARD_LIN_BASE,KEYBOARD_LIN_PIN,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_OD_WPU); GPIOPadConfigSet(KEYBOARD_COL_BASE,KEYBOARD_COL_PIN,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_OD_WPU); GPIOPinTypeIn(KEYBOARD_LIN_BASE,KEYBOARD_LIN_PIN); //设置为输入(行线) GPIOPinTypeOutOD(KEYBOARD_COL_BASE,KEYBOARD_COL_PIN); //设置为输出(列线) GPIOPinWrite(KEYBOARD_COL_BASE,KEYBOARD_COL_PIN,0x00); //列线输出0 temp2=GPIOPinRead(KEYBOARD_LIN_BASE,KEYBOARD_LIN_PIN)& KEYBOARD_LIN_PIN; temp3=temp1 | temp2; if(temp3==temp) { for(key=0;key<20;key++) if(temp==scanValue[key]) { GPIOPinTypeIn(KEYBOARD_LIN_BASE,KEYBOARD_LIN_PIN); GPIOPinTypeIn(KEYBOARD_COL_BASE,KEYBOARD_COL_PIN); return(keyASC[key]); } } } return ('N'); }
[ 本帖最后由 zyc1 于 2011-4-16 23:26 编辑 ] |