当前位置:网站首页>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;
}
}
}
}
边栏推荐
猜你喜欢

进程(中):进程状态、进程地址空间

【Arduino connects SD card module to realize data reading and writing】

TeamCode 产品 UI 全新升级,快来体验吧

Introduction and mock implementation of list:list

【Connect the heart rate sensor to Arduino to read the heart rate data】

IDEA2021.2安装与配置(持续更新)

idea中创建jsp项目详细步骤

Case | industrial iot solutions, steel mills high-performance security for wisdom

如何快速搭建属于自己的物联网平台?

IDEA2021.2安装与配置(持续更新)
随机推荐
78XX 79XX多路输出电源
2020 - AAAI - 图像修复 Image Inpainting论文导读 -《Region Normalization for Image Inpainting》
分割回文串 DP+回溯 (LeetCode-131)
【Popular Science Post】Detailed explanation of MDIO interface
【Connect the heart rate sensor to Arduino to read the heart rate data】
如何用 Lightly 进行 Debug 断点调试?
电子密码锁_毕设‘指导’
I2C无法访问ATEC508A加密芯片问题
TC358860XBG BGA65 东芝桥接芯片 HDMI转MIPI
写博客的原因。
【科普贴】MDIO接口详解
将ORCAD原理图导入allegro中进行PCB设计
振芯GM7123C:功能RGB转VGA芯片方案简介
【Popular Science Post】UART Interface Communication Protocol
ICN6211:MIPI DSI转RGB视频转换芯片方案介绍 看完涨知识了呢
uniCloud use
USB3.0一致性测试方法
电脑基本知识
TeamCode 产品 UI 全新升级,快来体验吧
使用buildroot制作根文件系统(龙芯1B使用)