你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32F4 按键控制小灯亮灭例程

[复制链接]
STMCU小助手 发布时间:2020-12-31 09:27
STM32F4 按键控制小灯亮灭例程
通过一个外接按键,来控制 LED 灯的亮灭。程序需要创建 led.h、led.c、key.h、key.c 文件。需要修改 main.c 文件。外接按键模块的 out 端口接单片机的 F0 端口。
LED 小灯程序
led.h
  1. # ifndef __LED_H
  2. # define __LED_H
  3. void LED_Init(void);
  4. # endif<span style="background-color: rgb(255, 255, 255);"> </span>
复制代码

led.c
  1. # include <led.h>
  2. # include <stm32f4xx.h>
  3. void LED_Init(void) {
  4.     GPIO_InitTypeDef GPIO_InitStructure;
  5.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
  6.     // F8
  7.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  8.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  9.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  10.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上拉
  11.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  12.     GPIO_Init(GPIOF, &GPIO_InitStructure);
  13.     GPIO_SetBits(GPIOF, GPIO_Pin_8);
  14. }
复制代码

KEY 按键程序
按键程序采用上拉输入
key.h
  1. # ifndef KEY_H
  2. # define KEY_H
  3. # include <sys.h>
  4. # include <stm32f4xx.h>
  5. #define KEY_ON 1 // 按键初始化程序设置为上拉,所以要达到按键按下小灯亮
  6. #define KEY_OFF 0 // 的功能需要设置 KEY_ON 为高电平,KEY_OFF 为低电平

  7. void key_Init(void);

  8. u8 Key_Scan(GPIO_TypeDef * GPIOx, u16 GPIO_Pin);

  9. # endif
复制代码

key.c
  1. # include <key.h>
  2. # include <stm32f4xx.h>
  3. # include <delay.h>
  4. // 按键初始化
  5. void key_Init(void) {
  6.     GPIO_InitTypeDef GPIO_InitStructure;
  7.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); // 打开 PA 口时钟
  8.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  9.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  10.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  11.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上拉
  12.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  13.     GPIO_Init(GPIOF, &GPIO_InitStructure);
  14.     GPIO_SetBits(GPIOF, GPIO_Pin_0);
  15. }
  16. // 判断按键是否按下
  17. u8 Key_Scan(GPIO_TypeDef * GPIOx, u16 GPIO_Pin) {
  18.     if(GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) == KEY_ON) {
  19.         delay_ms(50); // 战略性消抖,根据实际情况加减延迟时长
  20.         if(GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) == KEY_ON) {
  21.             return KEY_ON;
  22.         }
  23.         else
  24.             return KEY_OFF;
  25.     }
  26.     else
  27.         return KEY_OFF;
  28. }
复制代码

主程序 main.c
  1. # include <stm32f4xx.h>
  2. # include <led.h>
  3. # include <key.h>
  4. # include <delay.h>
  5. int main() {
  6.     delay_init(168);
  7.     LED_Init(); // LED 初始化
  8.     key_Init(); // 按键初始化
  9.    
  10.     while(1) {
  11.         if(Key_Scan(GPIOF, GPIO_Pin_0) == KEY_ON) {
  12.             GPIO_SetBits(GPIOF, GPIO_Pin_8);
  13.         }
  14.         if(Key_Scan(GPIOF, GPIO_Pin_0) == KEY_OFF) {
  15.             GPIO_ResetBits(GPIOF, GPIO_Pin_8);
  16.         }
  17.     }
  18. }
复制代码

现象
按键按下后,LED0 小灯亮;松开按键,小灯灭。
文章出处: ZZZX 是你鸭鸭

收藏 评论1 发布时间:2020-12-31 09:27

举报

1个回答
radio2radio 回答时间:2020-12-31 13:31:56
为什么不用HAL库, 这很重要。

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版