当前位置:网站首页>HAL库笔记——通过按键来控制LED(基于正点原子STM32F103ZET6精英板)
HAL库笔记——通过按键来控制LED(基于正点原子STM32F103ZET6精英板)
2022-08-02 03:33:00 【@C#&】
首先我们来看看控制LED灯和按键的IO口
LED初始化
#include "bsp_led.h"
void LED_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStructure.Pin = GPIO_PIN_5;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}
KEY初始化
void KEY_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
__HAL_RCC_GPIOE_CLK_ENABLE();
GPIO_InitStructure.Pin = GPIO_PIN_3 | GPIO_PIN_4;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT; //读取电平需要输入模式
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
}
main函数
#include "main.h"
#include "gpio.h"
#include "bsp_led.h"
#include "bsp_key.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/** * @brief The application entry point. * @retval int */
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
LED_GPIO_Init();
KEY_GPIO_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(Key_Scan(GPIOE,GPIO_PIN_3) == KEY_ON)
{
LED_RED_ON;
}
if(Key_Scan(GPIOE,GPIO_PIN_4) == KEY_ON)
{
while(1)
{
LED_RED_ON;
HAL_Delay(500); //500 ms
LED_RED_OFF;
HAL_Delay(500); //500 ms
if(Key_Scan(GPIOE,GPIO_PIN_4) == KEY_ON)
break;
}
}
}
}
边栏推荐
- 分割回文串 DP+回溯 (LeetCode-131)
- 引擎开发日志:场景编辑器开发难点
- 【TCS3200 color sensor and Arduino realize color recognition】
- 【科普贴】I2C通讯协议详解——偏软件分析和逻辑分析仪实例分析
- Anaconda(Jupyter)里发现不能识别自己的GPU该怎么办?
- TC358860XBG BGA65 东芝桥接芯片 HDMI转MIPI
- IoT solution
- 【Connect the heart rate sensor to Arduino to read the heart rate data】
- 【LeetCode】求和
- proteus数字电路仿真——入门实例
猜你喜欢
How to remotely debug PLC?
ICN6211:MIPI DSI转RGB视频转换芯片方案介绍 看完涨知识了呢
idea中创建jsp项目详细步骤
Flame sensor connected with Arduino
基础IO(下):软硬链接和动静态库
[Popular Science Post] I2C Communication Protocol Detailed Explanation - Partial Software Analysis and Logic Analyzer Example Analysis
【Arduino connects SD card module to realize data reading and writing】
蛮力法求解凸包问题
Application of electronic flow on business trip
简单的RC滤波电路
随机推荐
vector的使用和模拟实现:
HDMI转MIPI CSI东芝转换芯片-TC358743XBG/TC358749XBG
增量编译技术在Lightly中的实践
【面试必看】链表的常见笔试题
Lightly:新一代的C语言IDE
【LeetCode】链表相加 进位
使用pyqt弹出消息提示框
Anaconda(Jupyter)里发现不能识别自己的GPU该怎么办?
[Popular Science Post] I2C Communication Protocol Detailed Explanation - Partial Software Analysis and Logic Analyzer Example Analysis
TeamCode 产品 UI 全新升级,快来体验吧
idea中创建jsp项目详细步骤
蛮力法求解凸包问题
【科普贴】UART接口通讯协议
本地数据库 sqlite3 编译和使用
引擎开发日志:OpenGL资源多线程加载
如何使用 PHP 实现网页交互
GM8775C规格书,MIPI转LVDS,MIPI转双路LVDS分享
Type c PD 电路设计
【LeetCode】求和
关于IIC SDA毛刺的那些事