程序中所使用的矩陣鍵盤所接的引腳爲PC4-PC5、PF11-PF15和PG0,接線方法爲常規矩陣鍵盤的接法,PC4、PC5、PF11、PF12爲行線PF13、PF14、PF15、PG0爲列線。
u8 check_Key(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
u8 cord_h=0XFF,cord_l=0XFF; //h爲行線 l爲列線
u8 Val = 0xFF;
/* 行線 推輓輸出 */
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11|GPIO_Pin_12;
GPIO_Init(GPIOF,&GPIO_InitStructure);
/* 列線 上拉輸入 */
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_Init(GPIOG,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_Init(GPIOF,&GPIO_InitStructure);
/* 行線輸出全部設置爲0 */
GPIO_WriteBit(GPIOC, GPIO_Pin_4|GPIO_Pin_5, Bit_RESET);
GPIO_WriteBit(GPIOF, GPIO_Pin_11|GPIO_Pin_12, Bit_RESET);
delay_us(1);
/* 讀入列線值 讀入的值分別存入低四位 高四位全部爲0 */
cord_l&=(u8)((GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_13)<<0)|
(GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_14)<<1)|
(GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_15)<<2)|
(GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_0)<<3));
if(cord_l!=0X0F)
{
delay_ms(10); //消抖 延時後再讀一次
cord_l&=(u8)((GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_13)<<0)|
(GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_14)<<1)|
(GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_15)<<2)|
(GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_0)<<3));
if(cord_l!=0X0F)
{
/* 交換輸入信號讀取行線值 */
/* 列線 推輓輸出 */
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_Init(GPIOG,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_Init(GPIOF,&GPIO_InitStructure);
/* 行線 上拉輸入 */
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11|GPIO_Pin_12;
GPIO_Init(GPIOF,&GPIO_InitStructure);
/* 列線輸出全部設置爲0 */
GPIO_WriteBit(GPIOG, GPIO_Pin_0, Bit_RESET);
GPIO_WriteBit(GPIOF, GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15, Bit_RESET);
delay_ms(2);
/* 讀入行線值 */
cord_h&=(u8)((GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_4)<<3)|
(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_5)<<2)|
(GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_11)<<1)|
(GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_12)<<0));
Val=~(cord_h<<4|cord_l); //取反 便於分析Val對應的按鍵
return Val;
}
}
return ~Val;
}