STM32F407实现矩阵键盘程序

来源:互联网 发布:有招聘在家淘宝客服吗 编辑:程序博客网 时间:2024/05/29 15:10

由于最近在做一个有关按键的项目,有用到矩阵键盘,因为网上有关矩阵键盘的程序很少,所以我就自己写一个发上去供大家参考,该程序所用到的GPIO口是PD0--PD7,至于矩阵键盘的原理就不一一描述了,直接上程序,具体程序如下:

//GPIO初始化函数

void GPIO_Configuration(void)
{
 GPIO_InitTypeDef GPIO_InitStructuress;
 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOA, ENABLE);           //打开时钟               
         GPIO_InitStructuress.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;  //初始化PD0-PD3
  GPIO_InitStructuress.GPIO_Mode = GPIO_Mode_OUT ;      
 GPIO_InitStructuress.GPIO_OType = GPIO_OType_PP;                                             //设置为推挽输出
         GPIO_InitStructuress.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructuress.GPIO_PuPd =GPIO_PuPd_UP;                                                   //上拉
         GPIO_Init(GPIOD, &GPIO_InitStructuress);
         GPIO_InitStructuress.GPIO_Pin =GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;    //初始化PD4-PD7
 GPIO_InitStructuress.GPIO_Mode =GPIO_Mode_OUT ;
          GPIO_InitStructuress.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructuress.GPIO_PuPd = GPIO_PuPd_DOWN ;//Ï下拉
         GPIO_Init(GPIOD, &GPIO_InitStructuress);
 GPIO_SetBits( GPIOD,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);
         GPIO_ResetBits( GPIOD,GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
}

//按键函数

extern void Keyspress()
{

int KeyValue;

int y=0;
int f=5;
while(f)
{
   if((GPIO_ReadInputData(GPIOD)&0xff)!=0x0f) 

Delay10ms(20);
 if((GPIO_ReadInputData(GPIOD)&0xff)!=0x0f) 

GPIO_SetBits(GPIOD,GPIO_Pin_0); 

GPIO_ResetBits(GPIOD,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);

 switch(GPIO_ReadInputData(GPIOD)&0xff)
              { 
                case 0x11:KeyValue=12;break; 
 
                case 0x21:KeyValue=13;break; 
 
                case 0x41:KeyValue=14;break; 
 
                case 0x81:KeyValue=15;break;
               } 
                GPIO_SetBits(GPIOD,GPIO_Pin_1);
 
                GPIO_ResetBits(GPIOD,GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_3);
 
                 switch(GPIO_ReadInputData(GPIOD)&0xff)
               { 
                 case 0x12:KeyValue=8;break;
 
                 case 0x22:KeyValue=9;break; 
 
 
                 case 0x42:KeyValue=10;break;
 
                 case 0x82:KeyValue=11;break; 
 
                } 
                 GPIO_SetBits(GPIOD,GPIO_Pin_2); 


                 GPIO_ResetBits(GPIOD,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_3);


                  switch(GPIO_ReadInputData(GPIOD)&0xff)
                   { 
                    case 0x14:KeyValue=4;break;
 
                    case 0x24:KeyValue=5;break;
 
                    case 0x44:KeyValue=6;break;
 
                    case 0x84:KeyValue=7;break;
                   } 
                    GPIO_SetBits(GPIOD,GPIO_Pin_3); 
 
                    GPIO_ResetBits(GPIOD,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2);
                                              
                   switch(GPIO_ReadInputData(GPIOD)&0xff)
                    {
                     case 0x18:KeyValue=0;break;

                     case 0x28:KeyValue=1;break; 

                     case 0x48:KeyValue=2;break;

                     case 0x88:KeyValue=3;break;
                     } 
                        GPIO_SetBits(GPIOD,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2 |GPIO_Pin_3); 
                        GPIO_ResetBits(GPIOD, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 |GPIO_Pin_7);
                        while((GPIO_ReadInputData(GPIOD)&0xff)!=0x0f);
                      switch(KeyValue) 

                        case 0:f=3;break;
case 1:f=3;break;
case 2:f=3;break;
case 4: f=3;break;
case 5: f=3;break;
case 6: f=3;break;
case 8:f=3;break;
case 9: f=3;break;
 case 10: f=3; break;
 case 12:  f=3;break;
case 13:  f=3;break;//由于项目只用到12个按键,所以有4个按键的扫描程序是没有的

}

            }  
}
  }

}

//主函数:

int main

{

GPIO_Configuration();

Keyspress();

}

1 1