2019-03-24 15:44发布
#ifndef KEYBOARD_H#define KEYBOARD_H//定义长标识符成较短的形式#define SysCtlPeriEnable SysCtlPeripheralEnable#define SysCtlPeriDisable SysCtlPeripheralDisable#define GPIOPinTypeIn GPIOPinTypeGPIOInput#define GPIOPinTypeOutOD GPIOPinTypeGPIOOutputOD
//行线使用GPIOB(0-3)1111#define KEYBOARD_LIN_PERIPH SYSCTL_PERIPH_GPIOB#define KEYBOARD_LIN_BASE GPIO_PORTB_BASE#define KEYBOARD_LIN_PIN 0x0F //列线使用GPIOC(4-7)1111#define KEYBOARD_COL_PERIPH SYSCTL_PERIPH_GPIOC#define KEYBOARD_COL_BASE GPIO_PORTC_BASE#define KEYBOARD_COL_PIN 0xF0
#endif
uchar scanKeyboard(void){
ulong scanValue[20]={0xEE,0xDE,0xBE,0x7E, 0xED,0xDD,0xBD,0x7D, 0xEB,0xDB,0xBB,0x7B, 0xE7,0xD7,0xB7,0x77, };
const ulong keyASC[20]={'1','2','3','4', '5','6','7','8', '9','10','11','12', '13','14','15','16', };
//定义变量及使能GPIOC和GPIOB ulong key,temp,temp1,temp2,temp3;SysCtlPeripheralEnable(KEYBOARD_LIN_PERIPH);SysCtlPeripheralEnable(KEYBOARD_COL_PERIPH);//9个引脚全部上拉电阻有效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); temp1=GPIOPinRead(KEYBOARD_COL_BASE,KEYBOARD_COL_PIN)& KEYBOARD_COL_PIN;
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); temp2=GPIOPinRead(KEYBOARD_LIN_BASE,KEYBOARD_LIN_PIN)& KEYBOARD_LIN_PIN; temp=temp1 | temp2;
if(temp!=0xFF) { 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); temp1=GPIOPinRead(KEYBOARD_COL_BASE,KEYBOARD_COL_PIN)& KEYBOARD_COL_PIN; 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); 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]); } }
else
{
return('E');
}}return ('N');}
最多设置5个标签!
#ifndef KEYBOARD_H
#define KEYBOARD_H
//定义长标识符成较短的形式
#define SysCtlPeriEnable SysCtlPeripheralEnable
#define SysCtlPeriDisable SysCtlPeripheralDisable
#define GPIOPinTypeIn GPIOPinTypeGPIOInput
#define GPIOPinTypeOutOD GPIOPinTypeGPIOOutputOD
//行线使用GPIOB(0-3)1111
#define KEYBOARD_LIN_PERIPH SYSCTL_PERIPH_GPIOB
#define KEYBOARD_LIN_BASE GPIO_PORTB_BASE
#define KEYBOARD_LIN_PIN 0x0F
//列线使用GPIOC(4-7)1111
#define KEYBOARD_COL_PERIPH SYSCTL_PERIPH_GPIOC
#define KEYBOARD_COL_BASE GPIO_PORTC_BASE
#define KEYBOARD_COL_PIN 0xF0
#endif
uchar scanKeyboard(void)
{
ulong scanValue[20]={0xEE,0xDE,0xBE,0x7E,
0xED,0xDD,0xBD,0x7D,
0xEB,0xDB,0xBB,0x7B,
0xE7,0xD7,0xB7,0x77,
};
const ulong keyASC[20]={'1','2','3','4',
'5','6','7','8',
'9','10','11','12',
'13','14','15','16',
};
//定义变量及使能GPIOC和GPIOB
ulong key,temp,temp1,temp2,temp3;
SysCtlPeripheralEnable(KEYBOARD_LIN_PERIPH);
SysCtlPeripheralEnable(KEYBOARD_COL_PERIPH);
//9个引脚全部上拉电阻有效
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);
temp1=GPIOPinRead(KEYBOARD_COL_BASE,KEYBOARD_COL_PIN)& KEYBOARD_COL_PIN;
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);
temp2=GPIOPinRead(KEYBOARD_LIN_BASE,KEYBOARD_LIN_PIN)& KEYBOARD_LIN_PIN;
temp=temp1 | temp2;
if(temp!=0xFF)
{
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);
temp1=GPIOPinRead(KEYBOARD_COL_BASE,KEYBOARD_COL_PIN)& KEYBOARD_COL_PIN;
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);
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]);
}
}
else
{
return('E');
}
}
return ('N');
}
一周热门 更多>