野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9856|回复: 2

第13章 按键检测(课后作业)

[复制链接]
发表于 2016-12-15 19:03:43 | 显示全部楼层 |阅读模式
现象:按下复位键,LED2和LED3被点亮。按下K1(K2)键,LED2(LED3)灭,再按一次,LED2(LED3)亮。。。。。
code
  1. #ifndef _BSP_KEY_H
  2. #define _BSP_KEY_H

  3. #include "stm32f10x.h"

  4. #define KEY1_GPIO_PIN                  GPIO_Pin_0
  5. #define KEY1_GPIO_PORT                 GPIOA
  6. #define KEY1_RCC_APB2_CLOCK_ENABLE     RCC_APB2Periph_GPIOA

  7. #define KEY2_GPIO_PIN                  GPIO_Pin_13
  8. #define KEY2_GPIO_PORT                 GPIOC
  9. #define KEY2_RCC_APB2_CLOCK_ENABLE     RCC_APB2Periph_GPIOC

  10. #define KEY_ON        1
  11. #define KEY_OFF       0

  12. void KEY1_GPIO_Config(void);
  13. void KEY2_GPIO_Config(void);
  14. uint8_t Key2_Scan(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin);
  15. uint8_t Key3_Scan(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin);

  16. #endif /*_BSP_KEY_H*/

  17. #include "bsp_key.h"
  18. void Delay(uint32_t count)
  19. {
  20.   for(;count!=0;count--);
  21. }

  22. void KEY1_GPIO_Config(void)
  23. {
  24.         GPIO_InitTypeDef KEY1_GPIO_InitStruct;
  25.   
  26.     RCC_APB2PeriphClockCmd(KEY1_RCC_APB2_CLOCK_ENABLE,ENABLE);
  27.         KEY1_GPIO_InitStruct.GPIO_Pin = KEY1_GPIO_PIN;
  28.         KEY1_GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  29.   
  30.         GPIO_Init(KEY1_GPIO_PORT,&KEY1_GPIO_InitStruct);
  31. }

  32. void KEY2_GPIO_Config(void)
  33. {
  34.         GPIO_InitTypeDef KEY2_GPIO_InitStruct;
  35.   
  36.     RCC_APB2PeriphClockCmd(KEY2_RCC_APB2_CLOCK_ENABLE,ENABLE);
  37.         KEY2_GPIO_InitStruct.GPIO_Pin = KEY2_GPIO_PIN;
  38.         KEY2_GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  39.   
  40.         GPIO_Init(KEY2_GPIO_PORT,&KEY2_GPIO_InitStruct);
  41. }

  42. uint8_t Key2_Scan(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin)
  43. {
  44.   if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_ON)
  45.   {
  46.     Delay(20000);//软件消抖
  47.     if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_ON)
  48.     {//松手检测
  49.       while(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_ON);
  50.       return KEY_ON;
  51.     }
  52.     else return KEY_OFF;
  53.   }
  54.   else return KEY_OFF;
  55. }

  56. uint8_t Key3_Scan(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin)
  57. {
  58.   if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_OFF)
  59.   {
  60.     Delay(20000);//软件消抖
  61.     if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_OFF)
  62.     {//松手检测
  63.       while(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_OFF);
  64.       return KEY_OFF;
  65.     }
  66.     else return KEY_ON;
  67.   }
  68.   else return KEY_ON;
  69. }

  70. #include "stm32f10x.h"
  71. #include "bsp_led.h"
  72. #include "bsp_key.h"

  73. int main(void)
  74. {
  75.         LED2_GPIO_Config();
  76.         LED3_GPIO_Config();
  77.         KEY1_GPIO_Config();
  78.         KEY2_GPIO_Config();
  79.         while(1)
  80.         {
  81.         if( Key2_Scan(KEY1_GPIO_PORT,KEY1_GPIO_PIN)  == ((uint8_t)KEY_ON))
  82.           LED2_GPIO_TOGGLE;
  83.         if( Key3_Scan(KEY2_GPIO_PORT,KEY2_GPIO_PIN)  == ((uint8_t)KEY_OFF))
  84.           LED3_GPIO_TOGGLE;
  85.         }
  86. }


复制代码


回复

使用道具 举报

发表于 2016-12-15 21:57:26 来自手机 | 显示全部楼层
不错不错
回复

使用道具 举报

 楼主| 发表于 2016-12-16 19:42:00 | 显示全部楼层

我会加油的
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-5-9 03:33 , Processed in 0.035744 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表