• stm32学习笔记:GPIO输入


     1、寄存器输入输出函数

    1. //读取输入数据寄存器某一个端口的输入值,参数用来指定某一个端口,返回值是
    2. uint8_t类型,用来代表高低电平(读取按键的值)
    3. uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
    4. //读取整个输入数据寄存器,参数用来指定外设,uint16_t是一个16位的数据,每一位代表一个端口值
    5. uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
    6. //读取输出寄存器
    7. uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
    8. //读取输出寄存器
    9. uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);

    ReadInput:读取GPIO口

    ReadOutput:输出模式下输出的值/电平

    2、按下按键:低电平

    按键采取上拉输入模式,按下就接地,故没按下是高电平,按下的时候是低电平。

    按键抖动 :由于按键内部使用机械式弹等片进行判新,所以在按下和松手的瞬间会伴随有一连串的抖动

    1. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
    2. 上拉输入:可读取引脚电平,内部连接上拉电阻,悬空时默认高电平

           传感器元件的电阻会随看外界模拟量的变化而变化,通过领随电阻证即可得到模拟电压输出,再通过与电压比较器进行二值化即可得列数字电压输出

    3、主要代码

    (1)获取按键的值(按键接在B1和B11,led灯接在A1和A2)

    1. //读取按键值的函数
    2. uint8_t Key_GetNum(void)
    3. {
    4. uint8_t KeyNum = 0;
    5. //读取GPIO端口的功能
    6. if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)==0) //按下
    7. {
    8. Delay_ms(20);
    9. while((GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)==0)); //检测按键松手
    10. Delay_ms(20);
    11. KeyNum = 1;
    12. }
    13. if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11)==0)
    14. {
    15. Delay_ms(20);
    16. while((GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11)==0));
    17. Delay_ms(20);
    18. KeyNum = 2;
    19. }
    20. return KeyNum;
    21. }

    (2)调用按键中的函数

    1. while(1)
    2. {
    3. KeyNum = Key_GetNum();
    4. if(KeyNum == 1)
    5. {
    6. LED1_ON();
    7. }
    8. if(KeyNum == 2)
    9. {
    10. LED1_OFF();
    11. }
    12. }

    4、全部代码:

    一、按键控制灯

    (1)key.c
    1. #include "stm32f10x.h" // Device header
    2. #include "Delay.h"
    3. void Key_Init(void)
    4. {
    5. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
    6. GPIO_InitTypeDef GPIO_InitStructure;
    7. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    8. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_11;
    9. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    10. GPIO_Init(GPIOB,&GPIO_InitStructure);
    11. }
    12. //读取按键值的函数
    13. uint8_t Key_GetNum(void)
    14. {
    15. uint8_t KeyNum = 0;
    16. //读取GPIO端口的功能
    17. if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)==0) //按下
    18. {
    19. Delay_ms(20);
    20. while((GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)==0)); //检测按键松手
    21. Delay_ms(20);
    22. KeyNum = 1;
    23. }
    24. if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11)==0)
    25. {
    26. Delay_ms(20);
    27. while((GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11)==0));
    28. Delay_ms(20);
    29. KeyNum = 2;
    30. }
    31. return KeyNum;
    32. }
    (2)key.h
    1. #ifndef __KEY_H
    2. #define __KEY_H
    3. void Key_Init(void);
    4. uint8_t Key_GetNum(void);
    5. #endif
    (3)led
    1. #include "stm32f10x.h" // Device header 头文件
    2. void LED_Init(void) //LED初始化函数
    3. {
    4. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
    5. GPIO_InitTypeDef GPIO_InitStructure; //配置端口模式
    6. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    7. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
    8. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    9. GPIO_Init(GPIOA,&GPIO_InitStructure);
    10. GPIO_SetBits(GPIOA, GPIO_Pin_1 | GPIO_Pin_2);//关闭LED
    11. }
    12. void LED1_ON(void) //控制LED开关灯
    13. {
    14. GPIO_ResetBits(GPIOA, GPIO_Pin_1);
    15. }
    16. void LED1_OFF(void)
    17. {
    18. GPIO_SetBits(GPIOA, GPIO_Pin_1);
    19. }
    20. void LED1_Turn(void)
    21. {
    22. if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_1)==0)
    23. {
    24. GPIO_SetBits(GPIOA,GPIO_Pin_1); //再按一下置为1
    25. }
    26. else
    27. {
    28. GPIO_ResetBits(GPIOA,GPIO_Pin_1);
    29. }
    30. }
    31. void LED2_ON(void)
    32. {
    33. GPIO_ResetBits(GPIOA, GPIO_Pin_2);
    34. }
    35. void LED2_OFF(void)
    36. {
    37. GPIO_SetBits(GPIOA, GPIO_Pin_2);
    38. }
    39. void LED2_Turn(void)
    40. {
    41. if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_2)==0)
    42. {
    43. GPIO_SetBits(GPIOA,GPIO_Pin_2);
    44. }
    45. else
    46. {
    47. GPIO_ResetBits(GPIOA,GPIO_Pin_2);
    48. }
    49. }
    (4)led.h
    1. #ifndef __LED_H
    2. #define __LED_H
    3. void LED_Init(void);
    4. void LED1_ON(void);
    5. void LED1_OFF(void);
    6. void LED1_Turn(void);
    7. void LED2_ON(void);
    8. void LED2_OFF(void);
    9. void LED2_Turn(void);
    10. #endif
    (5)主函数
    1. #include "stm32f10x.h" // Device header
    2. #include "Delay.h"
    3. #include "LED.h"
    4. #include "Key.h"
    5. uint8_t KeyNum; //存储键码的返回值
    6. int main(void)
    7. {
    8. LED_Init();
    9. Key_Init();
    10. while(1)
    11. {
    12. KeyNum = Key_GetNum();
    13. if(KeyNum == 1)
    14. {
    15. LED1_ON();
    16. }
    17. if(KeyNum == 2)
    18. {
    19. LED1_OFF();
    20. }
    21. }
    22. }

    二、光敏传感器控制蜂鸣器

    当遮住光线时,输出指示灯灭,代表输出高电平,有光线时,输出指示灯亮,代表输出低电平

    (6)Buzzer.c
    1. #include "stm32f10x.h" // Device header
    2. void Buzzer_Init(void)
    3. {
    4. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    5. GPIO_InitTypeDef GPIO_InitStructure;
    6. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    7. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
    8. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    9. GPIO_Init(GPIOB, &GPIO_InitStructure);
    10. GPIO_SetBits(GPIOB, GPIO_Pin_12);
    11. }
    12. void Buzzer_ON(void)
    13. {
    14. GPIO_ResetBits(GPIOB, GPIO_Pin_12);
    15. }
    16. void Buzzer_OFF(void)
    17. {
    18. GPIO_SetBits(GPIOB, GPIO_Pin_12);
    19. }
    20. void Buzzer_Turn(void)
    21. {
    22. if (GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_12) == 0)
    23. {
    24. GPIO_SetBits(GPIOB, GPIO_Pin_12);
    25. }
    26. else
    27. {
    28. GPIO_ResetBits(GPIOB, GPIO_Pin_12);
    29. }
    30. }
    (7)Buzzer.h
    1. #ifndef __BUZZER_H
    2. #define __BUZZER_H
    3. void Buzzer_Init(void);
    4. void Buzzer_ON(void);
    5. void Buzzer_OFF(void);
    6. void Buzzer_Turn(void);
    7. #endif
    (8)LightSensor.c
    1. #include "stm32f10x.h" // Device header
    2. void LightSensor_Init(void)
    3. {
    4. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    5. GPIO_InitTypeDef GPIO_InitStructure;
    6. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉模式
    7. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
    8. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    9. GPIO_Init(GPIOB, &GPIO_InitStructure);
    10. }
    11. uint8_t LightSensor_Get(void)
    12. {
    13. return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13);
    14. }
    (9)LightSensor.h
    1. #ifndef __LIGHT_SENSOR_H
    2. #define __LIGHT_SENSOR_H
    3. void LightSensor_Init(void);
    4. uint8_t LightSensor_Get(void);
    5. #endif

    (10)主函数

    1. #include "stm32f10x.h" // Device header
    2. #include "Delay.h"
    3. #include "Buzzer.h"
    4. #include "LightSensor.h"
    5. int main(void)
    6. {
    7. Buzzer_Init();
    8. LightSensor_Init();
    9. while (1)
    10. {
    11. if(LightSensor_Get()==1) //光线暗
    12. {
    13. Buzzer_ON();
    14. }
    15. else
    16. {
    17. Buzzer_OFF();
    18. }
    19. }
    20. }

    三、GPIO的使用方法:

    1.首先初始化时钟;

    2.定义,赋值结构体:

    GPIO_Mode 选择8种输入输出模式,

    GPIO_Pin 选择引脚,可以用按位或的方式同时选中多个引脚

    GPIO_Speed选择输出速度,这个不是很重要,要求不高的话直接选50MHZ就行了

    3.最后使用GPIO Init函数,将指定的GPIO外设初始化好

    然后这里有8个读取和写入的函数 

  • 相关阅读:
    构造函数调用原则
    java毕业生设计中学线上作业评判系统计算机源码+系统+mysql+调试部署+lw
    引理和定理啥区别
    Java版企业电子招标采购系统源码Spring Cloud + Spring Boot +二次开发+ MybatisPlus + Redis
    【Spring】多环境切换
    ESP8266使用记录(四)
    适用于多类移动场景的融合认证机制设计
    安路远程调试使用chipwatcher报错
    【LeetCode每日一题:1684. 统计一致字符串的数目~~~字符串+遍历+模拟+计数】
    从零开始带你实现一套自己的CI/CD(一)Jenkins
  • 原文地址:https://blog.csdn.net/hmh520i/article/details/133133656