zoukankan      html  css  js  c++  java
  • STM32按键控制程序

    由于最近时间比较匆忙 已经有很久的时间没有写博客了 这次和大家分享的是STM32的一个按键的小程序 他的优点呢也是和上面一个LED一样就是便于移植 更改管脚方便 虽然都是些小程序 但是我觉得他们就像基础一样 基础打好了 你才能建立更高的大楼 学了1年半的电子 我深有体会 其实我们最难学的是基础 可是我们往往忽视了这些方面 只顾一头钻入 这个算法 那个算法  其实说个最简单的给你一份英文资料和一块芯片你能确保能够驱动 如果你连芯片都驱动不了 会再多的算法有什么意义呢??

    好了不多说 下面给大家分享下我的程序。大神不要见笑哦!

      1 /******************************************************************************
      2 * 文件      Key.c
      3 * 作者      Belye
      4 * 版本      ST V3.5.0
      5 * 日期      04-February-2016
      6 * 提要      基于德飞莱开发板的四个按键的底层驱动程序
      7 * 使用方式  函数调用示例
      8 * 注意      记得修改对应的按键初始化管脚
      9 ******************************************************************************/
     10 
     11 /***************************************   函数调用示例   ************************************
     12                 KEY_Init();
     13                 while(1)
     14                 {
     15                     i=KEY_Scan(0);
     16                     switch (i)
     17                         {
     18                             case S4_PRES:
     19                             break;
     20                             case S1_PRES:
     21                                 break;
     22                             case S2_PRES:
     23                                 break;
     24                             case S3_PRES:
     25                             break;
     26                             default:
     27                                 break;
     28                         }
     29                 }    
     30 **********************************************************************************************/
     31 #include<Key.h>
     32 #include<delay.h>
     33 
     34 void KEY_Init()
     35 {
     36         S4_Init();
     37         S1_Init();
     38         S2_Init();
     39         S3_Init();
     40 }
     41 
     42 
     43 void S4_Init(void)//PA0
     44 {
     45     
     46     GPIO_InitTypeDef GPIO_InitStructure;
     47 
     48      RCC_APB2PeriphClockCmd(S4_CLK,ENABLE);//使能PORTA时钟
     49 
     50 //    GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//关闭jtag,使能SWD,可以用SWD模式调试
     51     
     52     GPIO_InitStructure.GPIO_Pin  = S4_PIN;//PA8
     53     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //设置成下拉输入
     54      GPIO_Init(S4_PORT, &GPIO_InitStructure);//初始化GPIOA8
     55 
     56 }
     57 
     58 void S1_Init(void)
     59 {
     60     
     61     GPIO_InitTypeDef GPIO_InitStructure;
     62 
     63      RCC_APB2PeriphClockCmd(S1_CLK,ENABLE);//使能PORTA时钟
     64 
     65 //    GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//关闭jtag,使能SWD,可以用SWD模式调试
     66     
     67     GPIO_InitStructure.GPIO_Pin  = S1_PIN;//PA8
     68     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
     69      GPIO_Init(S1_PORT, &GPIO_InitStructure);//初始化GPIOA8
     70 
     71 } 
     72 
     73 void S2_Init(void)
     74 {
     75     
     76     GPIO_InitTypeDef GPIO_InitStructure;
     77 
     78      RCC_APB2PeriphClockCmd(S2_CLK,ENABLE);//使能PORTD时钟
     79 
     80 //    GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//关闭jtag,使能SWD,可以用SWD模式调试
     81     
     82     GPIO_InitStructure.GPIO_Pin  = S2_PIN;//PD3
     83     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
     84      GPIO_Init(S2_PORT, &GPIO_InitStructure);//初始化GPIOD3
     85 
     86 } 
     87 
     88 
     89 
     90 void S3_Init(void)
     91 {
     92     
     93     GPIO_InitTypeDef GPIO_InitStructure;
     94 
     95      RCC_APB2PeriphClockCmd(S3_CLK,ENABLE);//使能PORTD时钟
     96 
     97 //    GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//关闭jtag,使能SWD,可以用SWD模式调试
     98     
     99     GPIO_InitStructure.GPIO_Pin  = S3_PIN;//PD3
    100     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
    101      GPIO_Init(S3_PORT, &GPIO_InitStructure);//初始化GPIOD3
    102 
    103 } 
    104 
    105 
    106 
    107 
    108 //按键处理函数
    109 //返回按键值
    110 //mode:0,不支持连续按;1,支持连续按;
    111 //返回值:
    112 //0,没有任何按键按下
    113 //KEY0_PRES,KEY0按下
    114 //KEY1_PRES,KEY1按下
    115 //WKUP_PRES,WK_UP按下 
    116 //注意此函数有响应优先级,KEY0>KEY1>WK_UP!!
    117 u8 KEY_Scan(u8 mode)
    118 {     
    119     static u8 key_up=1;//按键按松开标志
    120     if(mode)key_up=1;  //支持连按          
    121     if(key_up&&(S4==1||S1==0||S2==0||S3==0))
    122     {
    123         delay_ms(10);//去抖动 
    124         key_up=0;
    125         if(S4==1)      return S4_PRES;
    126         else if(S1==0) return S1_PRES;
    127         else if(S2==0) return S2_PRES;
    128         else if(S3==0) return S3_PRES;
    129     }else if(S4==0&&S1==1&&S2==1&&S3==1)key_up=1;          
    130     return 0;// 无按键按下
    131 }
    #ifndef __Key_H
    #define __Key_H
    
    #include<sys.h>
    
    
    
    #define S1_CLK     RCC_APB2Periph_GPIOE
    #define S1_PORT    GPIOE
    #define S1_PIN     GPIO_Pin_4
    
    
    #define S2_CLK     RCC_APB2Periph_GPIOE
    #define S2_PORT    GPIOE
    #define S2_PIN     GPIO_Pin_3
    
    #define S3_CLK     RCC_APB2Periph_GPIOE
    #define S3_PORT    GPIOE
    #define S3_PIN     GPIO_Pin_2
    
    #define S4_CLK     RCC_APB2Periph_GPIOA
    #define S4_PORT    GPIOA
    #define S4_PIN     GPIO_Pin_0
    
    
    
    #define S1      GPIO_ReadInputDataBit(S1_PORT,S1_PIN)//读取按键1
    #define S2      GPIO_ReadInputDataBit(S2_PORT,S2_PIN)//读取按键2
    #define S3    GPIO_ReadInputDataBit(S3_PORT,S3_PIN)//读取按键3
    #define S4      GPIO_ReadInputDataBit(S4_PORT,S4_PIN)//读取按键4
     
    
    
    #define S1_PRES    2        //KEY1  
    #define S2_PRES    3        //KEY2 
    #define S3_PRES 4   //KEY3
    #define S4_PRES 1   //KEY0
    
    void KEY_Init(void);
    void S1_Init(void);//IO初始化
    void S2_Init(void);
    void S3_Init(void);
    void S4_Init(void);
    
    
    
    u8 KEY_Scan(u8 mode);      //按键扫描函数    
    
    
    
    
    
    
    
    
    
    #endif
  • 相关阅读:
    论文阅读 | ExtremeNet:Bottom-up Object Detection by Grouping Extreme and Center Points
    论文阅读 | CornerNet:Detecting Objects as Paired Keypoints
    论文阅读 | FPN:Feature Pyramid Networks for Object Detection
    关于字符串 “*****AB**C*D*****” 中前缀、后缀和中间 '*' 的处理
    #include< > 和 #include” ” 的区别
    小朋友排队
    核桃的数量
    操作格子
    字串统计
    关联矩阵
  • 原文地址:https://www.cnblogs.com/Belye/p/5223944.html
Copyright © 2011-2022 走看看