zoukankan      html  css  js  c++  java
  • 第三个--对于按键的总结

    其实对于stm32的按键操作函数和那个51的操作函数是类似的  ,没啥太大区别,无非就是 电平识别,,消抖,,状态处理

    1-----和那个led的基本上是一样的,就是先操作那个GPIO,然后再按键处理

    2-----接下来就是代码展示了:

     1 #include "stm32f10x.h"
     2 #define RB1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
     3 #define RB2 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8)
     4 #define RB3 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)
     5 #define RB4 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2)//四个按键对应的端口
     6 u32 TimingDelay=0;
     7 extern u8 alarm_flag;//按键标志
     8 void Key_Init(void);//按键初始化
     9 u8 Key_Scan(void);//按键扫描
    10 void Delay_Ms(u32 ntime);//延时函数
    11 void Led_Init();//LED灯的初始化
    12 void Led_Control(u16 led,u8 status);//led灯显示函数
    13 void main()
    14 {
    15       u8 num;
    16       void Led_Init();
    17       void Led_Control(LEDALL,0);
    18       void Key_Init(void);
    19        num= Key_Scan(void);
    20        LED_Control(num+8, 1);  
    21         for (i = 0x3fffff; i > 0; i--)  
    22         LED_Control(num+8, 0);  
    23         for (i = 0x3fffff; i > 0; i--) 
    24       
    25 
    26 }
    27 
    28 void Delay_Ms(u32 ntime)
    29 {
    30     TimingDelay=ntime;
    31     while(TimingDelay);
    32 }
    33 void Key_Init(void)
    34 {
    35     GPIO_InitTypeDef GPIO_InitStructure;
    36     
    37     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);//A,B时钟使能
    38 
    39     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_8;//A0,A8
    40 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//输出模式 41 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化 42 43 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2;//B1,,B2初始化 44 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//模式 45 GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化 46 } 47 u8 Key_Scan(void) 48 { 49 static u8 mode=1; 50 if((mode==1)&&((RB1==0)||(RB2==0)||(RB3==0)||(RB4==0))) 51 { 52 Delay_Ms(10); 53 mode=0; 54 if(RB1==0) return 1; 55 else if (RB2==0) return 2; 56 else if(RB3==0) return 3; 57 else if(RB4==0) return 4; 58 } 59 else if((mode==0)&&((RB1==1)&&(RB2==1)&&(RB3==1)&&(RB4==1))) mode=1; 60 return 0; 61 62 } 63 void Led_Init() 64 { 65 GPIO_InitTypeDef GPIO_InitStructure; 66 67 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE); 68 69 GPIO_InitStructure.GPIO_Pin = LED1|LED2|LED3|LED4|LED5|LED6|LED7|LED8; 70 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 71 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 72 GPIO_Init(GPIOC, &GPIO_InitStructure); 73 74 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; 75 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 76 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 77 GPIO_Init(GPIOD, &GPIO_InitStructure); 78 } 79 void Led_Control(u16 led,u8 status) 80 { 81 if(status) 82 { 83 GPIO_ResetBits(GPIOC,led); 84 GPIO_SetBits(GPIOD,GPIO_Pin_2); 85 GPIO_ResetBits (GPIOD,GPIO_Pin_2); 86 } 87 else 88 { 89 GPIO_SetBits(GPIOC,led); 90 GPIO_SetBits(GPIOD,GPIO_Pin_2); 91 GPIO_ResetBits (GPIOD,GPIO_Pin_2); 92 } 93 } 94
  • 相关阅读:
    CodeForces 660D Number of Parallelograms
    【POJ 1082】 Calendar Game
    【POJ 2352】 Stars
    【POJ 2481】 Cows
    【POJ 1733】 Parity Game
    【NOI 2002】 银河英雄传说
    【NOI 2015】 程序自动分析
    【POJ 1704】 Georgia and Bob
    【HDU 2176】 取(m堆)石子游戏
    【SDOI 2016】 排列计数
  • 原文地址:https://www.cnblogs.com/xiaoyoucai/p/6573404.html
Copyright © 2011-2022 走看看