• 按键点亮led灯


    原理图:


    K0这个按键按下时,开发板D1这个灯亮,松开,灯灭


     代码如下:

    1. #include "stm32f4xx.h"
    2. void LED_Init(void)
    3. {
    4. //1.定义一个GPIO外设的结构体变量
    5. GPIO_InitTypeDef GPIO_InitStructure;
    6. //RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOF, ENABLE);
    7. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
    8. //3.对结构体变量的成员进行赋值
    9. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //输出模式
    10. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
    11. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //输出速率100MHZ
    12. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉输出
    13. //4.初始化GPIO
    14. //GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10; //引脚
    15. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //引脚
    16. GPIO_Init(GPIOF, &GPIO_InitStructure);
    17. //GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14; //引脚
    18. //GPIO_Init(GPIOE, &GPIO_InitStructure);
    19. GPIO_SetBits(GPIOF,GPIO_Pin_9); //设置高电平 LED灭
    20. // GPIO_SetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10); //设置高电平 LED灭
    21. //GPIO_SetBits(GPIOE,GPIO_Pin_13|GPIO_Pin_14); //设置高电平 LED灭
    22. }
    23. void KEY_Init(void)
    24. {
    25. //1.定义一个GPIO外设的结构体变量
    26. GPIO_InitTypeDef GPIO_InitStructure;
    27. //2.打开外设时钟 GPIOA PA0
    28. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    29. //3.对结构体变量的成员进行赋值
    30. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //输入模式
    31. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉输出
    32. //4.初始化GPIO
    33. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //引脚
    34. GPIO_Init(GPIOB, &GPIO_InitStructure);
    35. }
    36. int main()
    37. {
    38. KEY_Init(); //按键的初始化
    39. LED_Init(); //LED的初始化
    40. while(1)
    41. {
    42. if( GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9) == RESET ) //说明被按下
    43. {
    44. GPIO_ResetBits(GPIOF,GPIO_Pin_9); //设置低电平 LED亮
    45. }
    46. else
    47. {
    48. GPIO_SetBits(GPIOF,GPIO_Pin_9); //设置高电平 LED灭
    49. }
    50. }
    51. }

  • 相关阅读:
    HTML5、CSS3、ES6新特性总结
    AcWing算法提高课-5.6.1同余方程
    SpringBoot项目
    JAVA 开发pc端桌面软件 基于idea+javafx+maven+springboot
    【刷题之路 | Java & Python】两数之和(暴力枚举&哈希表)
    XXE-Lab for PHP
    力扣细节题:删除链表中的重复元素
    基于开源模型搭建实时人脸识别系统(六):人脸识别(人脸特征提取)
    web课程设计使用html+css+javascript+jquery技术制作个人介绍6页
    越南公司注册要求
  • 原文地址:https://blog.csdn.net/qq_15267341/article/details/132950336