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

STM32按键控制LED灯

[复制链接]
STMCU小助手 发布时间:2022-11-28 15:05
知识介绍工程文件放置
640 (6).png

640 (5).png
只有在添加进这一串之后,才能使用头文件集合 stm32f10x_conf.h

640 (4).png

选择类型
640 (3).png
初始化时钟从哪找
在stm32f10x_rcc.c中。

640 (2).png

断言函数
640 (1).png

此工程需要编写以下五个文档

LED.h
  1. #ifndef __LED_H
  2. #define __LED_H
  3. #include "stm32f10x.h"
  4. #define LED_B_GPIO_PIN              GPIO_Pin_1
  5. #define LED_R_GPIO_PIN              GPIO_Pin_5
  6. #define LED_GPIO_PORT               GPIOB
  7. #define LED_GPIO_CLK                RCC_APB2Periph_GPIOB
  8. //反转
  9. #define LED_R_TOGGLE        {LED_GPIO_PORT->ODR ^= LED_R_GPIO_PIN;}//异或可以改变原来的状态
  10. #define LED_B_TOGGLE        {LED_GPIO_PORT->ODR ^= LED_B_GPIO_PIN;}//异或可以改变原来的状态
  11. void LED_GPIO_Config(void);
  12. #endif /* __LED_H */
复制代码

LED.c
  1. #include "YANG_LED.h"
  2. void LED_GPIO_Config(void)
  3. {
  4. /*定义3个GPIO_InitTypeDef 类型的结构体*/
  5. GPIO_InitTypeDef  GPIO_InitStruct1;
  6. GPIO_InitTypeDef  GPIO_InitStruct2;
  7. /*开启 LED 相关的 GPIO 外设时钟*/
  8. RCC_APB2PeriphClockCmd(LED_GPIO_CLK, ENABLE);
  9. GPIO_InitStruct1.GPIO_Pin = LED_B_GPIO_PIN;
  10. GPIO_InitStruct2.GPIO_Pin = LED_R_GPIO_PIN;
  11.   GPIO_InitStruct1.GPIO_Mode = GPIO_Mode_Out_PP;
  12. GPIO_InitStruct1.GPIO_Speed = GPIO_Speed_50MHz;
  13.   GPIO_InitStruct2.GPIO_Mode = GPIO_Mode_Out_PP;
  14. GPIO_InitStruct2.GPIO_Speed = GPIO_Speed_50MHz;
  15. GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct1);
  16. GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct2);
  17. }
复制代码

640.jpg


KEY.h
  1. #ifndef __KEY_H
  2. #d#include "stm32f10x.h"
  3. #define  KEY_ON      1
  4. #define  KEY_OFF     0
  5. #define KEY1_GPIO_CLK   RCC_APB2Periph_GPIOA
  6. #define KEY1_GPIO_PORT   GPIOA
  7. #define KEY1_GPIO_PIN   GPIO_Pin_0
  8. #define KEY2_GPIO_CLK   RCC_APB2Periph_GPIOC
  9. #define KEY2_GPIO_PORT   GPIOC
  10. #define KEY2_GPIO_PIN   GPIO_Pin_13
  11. void Key_GPIO_Config(void);
  12. uint8_t Key_Scan(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin);
  13. #endif /* __KEY_H */
复制代码

KEY.c
  1. #include "KEY.h"
  2. void Key_GPIO_Config(void)
  3. {
  4. GPIO_InitTypeDef GPIO_InitStructure;
  5. RCC_APB2PeriphClockCmd(KEY1_GPIO_CLK|KEY2_GPIO_CLK,ENABLE);
  6.   //定义第一个按键
  7. GPIO_InitStructure.GPIO_Pin = KEY1_GPIO_PIN;
  8. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  9. GPIO_Init(KEY1_GPIO_PORT, &GPIO_InitStructure);
  10. //定义第二个按键
  11. GPIO_InitStructure.GPIO_Pin = KEY2_GPIO_PIN;
  12. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  13. GPIO_Init(KEY2_GPIO_PORT, &GPIO_InitStructure);

  14. }
  15. uint8_t Key_Scan(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin)
  16. {
  17. if( GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) == KEY_ON )
  18. {
  19.   while( GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) == KEY_ON );
  20.   return KEY_ON;
  21. }
  22. else return KEY_OFF;
  23. }
复制代码

main.c
  1. #include "stm32f10x.h"   // 相当于51单片机中的  #include <reg51.h>
  2. #include "LED.h"
  3. #include "KEY.h"
  4. int main(void)
  5. {
  6. LED_GPIO_Config();
  7. Key_GPIO_Config();
  8.    while (1)
  9. {
  10.     if ( Key_Scan(KEY1_GPIO_PORT,KEY1_GPIO_PIN) == KEY_ON )        
  11.       LED_B_TOGGLE;
  12.     if ( Key_Scan(KEY2_GPIO_PORT,KEY2_GPIO_PIN) == KEY_ON )
  13.         LED_R_TOGGLE;  
  14.   }
复制代码

仿真图
640.png

转载自: 跋扈洋


收藏 评论0 发布时间:2022-11-28 15:05

举报

0个回答

所属标签

相似分享

官网相关资源

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