嵌入式STM32学习笔记之按键查询方式控制led灯的亮灭

时间:2024-01-31 14:43:01

        其实接触STM32已经快半年了,端断续续的也学习了一下的STM32的各个模块的基本功能,刚开始的时候也是看比人的写的代码,看懂了然后再去修改,能够在自己的平台上实现基本的功能,也就是到现在才能真正的按照自己的想法来实现写功能,在本文中我将介绍下比较经典的按键驱动程序的实现方式--查询,当然中断方式的应用也比较广泛,在此先不做介绍了。

     首先我的硬件平台是奋斗版STM32,之所以选择这款开发版是因为板子提供了许多关于ucos和ucgui的例程,想往这方面发展的童鞋可以考虑哦,呵呵,不多说了,开发板带的芯片型号是STM32F103VET6: 64K 片内SRAM,512K 片内FALSH.

     本文所用到的外设:3个led以及4个按键,基本功能就是开发板上电3个led点亮,按下按键1时led灭,按下按键2时led2熄灭,按下按键3时led3熄灭,通过查询方式来获取键值

     接口设置情况:led灯: V6-PB5--LED1
                                            V7-PD6--LED2
                                            V8-PD3--LED3

                                 按键:K1--PC5
                                            K2--PC2
                                            K3--PC3
                                            K4--PE6  

函数部分:首先是主函数:

int main(void)
{
                 TYPEDEF_KEY key = KEY_NULL; 


                 RCC_Configuration();       //系统时钟配置
                 LED_Config();                   //LED控制配置
                 Key_Config();

                 LED1_ON;                     //开发板上电之后3个LED亮,这里是用宏定义的方式来写的:

                                                       //#define LED1_ON GPIO_SetBits(GPIOB, GPIO_Pin_5); //PB5置1  点亮LED1
                 LED2_ON;                   
                 LED3_ON;
   
  
              while (1)
             {  
                     key = GetKey();          //获取键值
                      if(key == KEY_1)
                     {
                                  LED1_OFF;   //按键1被按下 在按键释放后LED灭
                     }
                     if(key == KEY_2)
                     {
                                 LED2_OFF;
                     }
                     if(key == KEY_3)
                     {
                                  LED3_OFF;
                     }
                     if(key == KEY_4)
                    {
                                 LED1_ON;      //按键4被按下,在按键释放后3个LED全部点亮
                                 LED2_ON;
                                 LED3_ON;
                    }
                   Delay(10);
         }
}

 

接着就是LED的配置函数:                  

void LED_Config(void)
{
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD , ENABLE);//外设时钟使能 
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;                        //LED1   
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;     //推挽输出
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    //口线翻转速度为50MHz
      GPIO_Init(GPIOB, &GPIO_InitStructure);    

 

      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_3;   //LED2, LED3 

      GPIO_Init(GPIOD, &GPIO_InitStructure);
}

接着就是按键的配置函数:4个按键配置为上拉输入模式

void Key_Config(void)
{
                  GPIO_InitTypeDef GPIO_InitStructure;

                  RCC_APB2PeriphClockCmd(KEY1_PORT_APB2Periph, ENABLE);     
                  GPIO_InitStructure.GPIO_Pin   = KEY1_PIN;    //#define KEY1_PIN GPIO_Pin_5
                  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
                  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPU; //上拉输入
                  GPIO_Init(KEY1_PORT, &GPIO_InitStructure);     //#define KEY1_PORT GPIOC

                 RCC_APB2PeriphClockCmd(KEY2_PORT_APB2Periph, ENABLE);     
                 GPIO_InitStructure.GPIO_Pin   = KEY2_PIN;
                 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
                 GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPU;
                 GPIO_Init(KEY2_PORT, &GPIO_InitStructure);

                 RCC_APB2PeriphClockCmd(KEY3_PORT_APB2Periph, ENABLE);     
                GPIO_InitStructure.GPIO_Pin   = KEY3_PIN;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
                GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPU;
                GPIO_Init(KEY3_PORT, &GPIO_InitStructure);

                RCC_APB2PeriphClockCmd(KEY4_PORT_APB2Periph, ENABLE);     
                GPIO_InitStructure.GPIO_Pin   = KEY4_PIN;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
                GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPU;
                GPIO_Init(KEY4_PORT, &GPIO_InitStructure);

查询方式获取键值函数:

TYPEDEF_KEY GetKey(void)
{
                  TYPEDEF_KEY key = KEY_NULL;  
                  if( GPIO_ReadInputDataBit(KEY1_PORT, KEY1_PIN) == 0 ||  GPIO_ReadInputDataBit(KEY2_PORT,  KEY2_PIN) == 0 || GPIO_ReadInputDataBit(KEY3_PORT, KEY3_PIN) == 0 || GPIO_ReadInputDataBit(KEY4_PORT, KEY4_PIN) == 0)     //有键按下
                {
                           Delay(10000);   //去抖动   
                          if(GPIO_ReadInputDataBit(KEY1_PORT, KEY1_PIN) == 0)
                         {
                                    key = KEY_1;
                         }
                        else if(GPIO_ReadInputDataBit(KEY2_PORT, KEY2_PIN) == 0)
                        {
                                    key = KEY_2;
                        }
                       else if(GPIO_ReadInputDataBit(KEY3_PORT, KEY3_PIN) == 0)
                       {
                                    key = KEY_3;
                        }
                      else if(GPIO_ReadInputDataBit(KEY4_PORT, KEY4_PIN) == 0)
                     {
                                   key = KEY_4;
                     }  
                    else
                     {
                                     return(KEY_NULL);
                     }        
                    while(GPIO_ReadInputDataBit(KEY1_PORT, KEY1_PIN) == 0 ||  GPIO_ReadInputDataBit(KEY2_PORT, KEY2_PIN) == 0 || GPIO_ReadInputDataBit(KEY3_PORT, KEY3_PIN) == 0 || GPIO_ReadInputDataBit(KEY4_PORT, KEY4_PIN) == 0)//等待键抬起
                    {
                                     Delay(20);   
                    }             
                    return(key);     
           } 
 return(KEY_NULL);   
}

枚举类型:按键值

typedef enum
{
 KEY_1, KEY_2, KEY_3, KEY_4, KEY_NULL = 0xff,
}TYPEDEF_KEY;

 创客学院全新视频尽在:www.makeru.com.cn

嵌入式学习交流群:561213221