[mw_shl_code=c,true]#include "keyboard.h"
#include "delay.h"
u16 rowPin[4] = {GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_4|GPIO_Pin_6};
u16 colPin[4] = {GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_4|GPIO_Pin_6};
u8 lastKey,currentKey;
void keyboard_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOF, ENABLE); //????GPIOBEF?????±??
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_4|GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //???ì????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //??????50MHz
GPIO_Init(GPIOE, &GPIO_InitStructure); //?ù????????????GPIOB.8
GPIO_ResetBits(GPIOE, GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_4|GPIO_Pin_6);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_4|GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //????????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //??????50MHz
GPIO_Init(GPIOF, &GPIO_InitStructure); //?ù????????????GPIOB.8
//GPIO_SetBits(GPIOF, GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_4|GPIO_Pin_6);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //LED0-->  B.5 ????????
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //???ì????
GPIO_Init(GPIOB, &GPIO_InitStructure); //?ù???è?¨??????????GPIOB.5 //LED??????
GPIO_SetBits(GPIOB,GPIO_Pin_5); //PB.5 ??????
}
u8 Rescan(u16 colPin,u8 colIndex)
{
u8 i;
for(i=0;i<4;i++)
{
GPIO_ResetBits(GPIOE, GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_4|GPIO_Pin_6);
delay_ms(10);
if((GPIO_ReadInputDataBit(GPIOF, colPin)==0))
{
GPIO_ResetBits(GPIOE, GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_4|GPIO_Pin_6);
return colIndex*4+i+1;
}
}
GPIO_ResetBits(GPIOE, GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_4|GPIO_Pin_6);
return 0;
}
u8 KEY_scan(void)
{
u8 i;
for(i = 0;i<4;i++)
{
if(GPIO_ReadInputDataBit(GPIOF,colPin ) == 0){
currentKey = Rescan(colPin,i);
if(currentKey == 0)
{
lastKey = 0;
return 0;
}
else
{
if(currentKey != lastKey)
{
lastKey = currentKey;
return lastKey;
}
else
{
lastKey = currentKey;
return 0;
}
}
}
}
lastKey = 0;
return 0;
}[/mw_shl_code]
|