STM32矩阵键盘

#include "key.h"

static u8 key_up=1;

//矩阵键盘:

//行4:PF1

//行3:PF3

//行2:PF5

//行1:PF7、

//列1:PF0

//列2:PF2

//列3:PF4

//列4:PF6
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure1,GPIO_InitStructure2;
 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);           //打开时钟 
    
 GPIO_InitStructure1.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_5|GPIO_Pin_3|GPIO_Pin_1;  //行
 GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_IN;                                                
 GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_100MHz;
 GPIO_InitStructure1.GPIO_PuPd = GPIO_PuPd_UP;
 GPIO_Init(GPIOF, &GPIO_InitStructure1); 
    
 GPIO_InitStructure2.GPIO_Pin =GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_4|GPIO_Pin_6;    //列
 GPIO_InitStructure2.GPIO_Mode =GPIO_Mode_OUT;
 GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_100MHz;
 GPIO_InitStructure2.GPIO_PuPd =GPIO_PuPd_DOWN;
 GPIO_Init(GPIOF, &GPIO_InitStructure2);

}
u8 KEY_Scan(u8 mode)  //mode=0  ?????  mode=1  
{     
  
  u8 keyvalue = 0;
    //static u8 key_up = 1; //按键松开标志
    if(mode) key_up = 1; //连按
    
    GPIO_ResetBits(GPIOF,GPIO_Pin_0);
    GPIO_ResetBits(GPIOF,GPIO_Pin_2);
    GPIO_ResetBits(GPIOF,GPIO_Pin_4);
    GPIO_ResetBits(GPIOF,GPIO_Pin_6);
    if(key_up && (KEYF7==0 || KEYF5==0 || KEYF3==0 || KEYF1==0))
    { 
        delay_ms(10); //消抖
        key_up=0;   
        if(KEYF7==0)  keyvalue =0;
        if(KEYF5==0)  keyvalue =4;
        if(KEYF3==0)  keyvalue =8;
        if(KEYF1==0)  keyvalue =12;

        GPIO_ResetBits(GPIOF,GPIO_Pin_0);        
        GPIO_SetBits(GPIOF,GPIO_Pin_2);
        GPIO_SetBits(GPIOF,GPIO_Pin_4);
        GPIO_SetBits(GPIOF,GPIO_Pin_6);
        if(KEYF7==0||KEYF5==0||KEYF3==0||KEYF1==0) keyvalue = keyvalue+1;

        GPIO_SetBits(GPIOF,GPIO_Pin_0);        
        GPIO_ResetBits(GPIOF,GPIO_Pin_2);
        GPIO_SetBits(GPIOF,GPIO_Pin_4);
        GPIO_SetBits(GPIOF,GPIO_Pin_6);
        if(KEYF7==0||KEYF5==0||KEYF3==0||KEYF1==0) keyvalue = keyvalue+2;

        GPIO_SetBits(GPIOF,GPIO_Pin_0);        
        GPIO_SetBits(GPIOF,GPIO_Pin_2);
        GPIO_ResetBits(GPIOF,GPIO_Pin_4);
        GPIO_SetBits(GPIOF,GPIO_Pin_6);
        if(KEYF7==0||KEYF5==0||KEYF3==0||KEYF1==0) keyvalue = keyvalue+3;

        GPIO_SetBits(GPIOF,GPIO_Pin_0);        
        GPIO_SetBits(GPIOF,GPIO_Pin_2);
        GPIO_SetBits(GPIOF,GPIO_Pin_4);
        GPIO_ResetBits(GPIOF,GPIO_Pin_6);
        if(KEYF7==0||KEYF5==0||KEYF3==0||KEYF1==0) keyvalue = keyvalue + 4;    
            return keyvalue;
    }else if((KEYF7==1 && KEYF5==1 &&KEYF3==1 && KEYF1==1)) key_up=1; 
    return 0;
}
 

你可能感兴趣的:(STM32矩阵键盘)