【STM32】GPIO控制LED(HAL库版)

2023-10-26 11:01
文章标签 led stm32 控制 hal gpio 库版

本文主要是介绍【STM32】GPIO控制LED(HAL库版),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

STM32最新固件库v3.5/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/system_stm32f10x.c · 林何/STM32F103C8 - 码云 - 开源中国 (gitee.com)

STM32最新固件库v3.5/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_gpio.c · 林何/STM32F103C8 - 码云 - 开源中国 (gitee.com)

1.宏定义

/* ------------ GPIO registers bit address in the alias region ----------------*/
//AFIO:复用寄存器
#define AFIO_OFFSET                 (AFIO_BASE - PERIPH_BASE)/* --- EVENTCR Register -----*//* Alias word address of EVOE bit */
#define EVCR_OFFSET                 (AFIO_OFFSET + 0x00)
#define EVOE_BitNumber              ((uint8_t)0x07)
#define EVCR_EVOE_BB                (PERIPH_BB_BASE + (EVCR_OFFSET * 32) + (EVOE_BitNumber * 4))/* ---  MAPR Register ---*/ 
/* Alias word address of MII_RMII_SEL bit */ 
#define MAPR_OFFSET                 (AFIO_OFFSET + 0x04) 
#define MII_RMII_SEL_BitNumber      ((u8)0x17) 
#define MAPR_MII_RMII_SEL_BB        (PERIPH_BB_BASE + (MAPR_OFFSET * 32) + (MII_RMII_SEL_BitNumber * 4))//位掩码
#define EVCR_PORTPINCONFIG_MASK     ((uint16_t)0xFF80)
#define LSB_MASK                    ((uint16_t)0xFFFF)
#define DBGAFR_POSITION_MASK        ((uint32_t)0x000F0000)
#define DBGAFR_SWJCFG_MASK          ((uint32_t)0xF0FFFFFF)
#define DBGAFR_LOCATION_MASK        ((uint32_t)0x00200000)
#define DBGAFR_NUMBITS_MASK         ((uint32_t)0x00100000)

2..GPIO模块标准库

1.GPIO_DeInit

/*** @brief  Deinitializes the GPIOx peripheral registers to their default reset values.* @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.* @retval None*/
void GPIO_DeInit(GPIO_TypeDef* GPIOx)
{/* Check the parameters */assert_param(IS_GPIO_ALL_PERIPH(GPIOx));if (GPIOx == GPIOA){RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, ENABLE);RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, DISABLE);}else if (GPIOx == GPIOB){RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, ENABLE);RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, DISABLE);}else if (GPIOx == GPIOC){RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC, ENABLE);RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC, DISABLE);}else if (GPIOx == GPIOD){RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, ENABLE);RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, DISABLE);}    else if (GPIOx == GPIOE){RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOE, ENABLE);RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOE, DISABLE);} else if (GPIOx == GPIOF){RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOF, ENABLE);RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOF, DISABLE);}else{if (GPIOx == GPIOG){RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOG, ENABLE);RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOG, DISABLE);}}
}

 

 

2.GPIO_Init

1.参数1:GPIO_TypeDef*

表示哪一个端口是(CRL还是CRH还是IDR)

记录的是寄存器地址

定义在system.stm32f10x.h中【因为外部可能要使用到】

2.参数2:GPIO_InitTypeDef*

描述对GPIO的初始化(形容词)

定义在gpio.h

/** * @brief  GPIO Init structure definition  */typedef struct
{uint16_t GPIO_Pin;             /*!< Specifies the GPIO pins to be configured.This parameter can be any value of @ref GPIO_pins_define */GPIOSpeed_TypeDef GPIO_Speed;  /*!< Specifies the speed for the selected pins.This parameter can be a value of @ref GPIOSpeed_TypeDef */GPIOMode_TypeDef GPIO_Mode;    /*!< Specifies the operating mode for the selected pins.This parameter can be a value of @ref GPIOMode_TypeDef */
}GPIO_InitTypeDef;

3.步骤1:GPIO Mode Configuration

输入模式全是0,输出模式全是1

速度初始化

/*---------------------------- GPIO Mode Configuration -----------------------*///取出bit0-bit3currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00)//判断出等于1(输出模式)的进入{ /* Check the parameters */assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));/* Output mode */currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;}

4.步骤2:GPIO CRL Configuration

/*---------------------------- GPIO CRL Configuration ------------------------*//* Configure the eight low port pins *///判断此时的控制器在CRL(bit0-bit7)还是CRH(bit7-bit15)if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00)//如果进入表示bit0-bit7有为1的,则表示要使用CRL{tmpreg = GPIOx->CRL;//pinpos < 0x08:表示从bit0-bit7开始找for (pinpos = 0x00; pinpos < 0x08; pinpos++){pos = ((uint32_t)0x01) << pinpos;/* Get the port pins position *///判断当前的Pin是哪一个currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;if (currentpin == pos)//表示这位Pin是1,找到了{pos = pinpos << 2;/* Clear the corresponding low control register bits */pinmask = ((uint32_t)0x0F) << pos;tmpreg &= ~pinmask;//清零/* Write the mode configuration in the corresponding bits */tmpreg |= (currentmode << pos);/* Reset the corresponding ODR bit */if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD){GPIOx->BRR = (((uint32_t)0x01) << pinpos);}else{/* Set the corresponding ODR bit */if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU){GPIOx->BSRR = (((uint32_t)0x01) << pinpos);}}}}GPIOx->CRL = tmpreg;}

5.步骤3:GPIO CRH Configuration

/*---------------------------- GPIO CRH Configuration ------------------------*//* Configure the eight high port pins */if (GPIO_InitStruct->GPIO_Pin > 0x00FF){tmpreg = GPIOx->CRH;for (pinpos = 0x00; pinpos < 0x08; pinpos++){pos = (((uint32_t)0x01) << (pinpos + 0x08));/* Get the port pins position */currentpin = ((GPIO_InitStruct->GPIO_Pin) & pos);if (currentpin == pos){pos = pinpos << 2;/* Clear the corresponding high control register bits */pinmask = ((uint32_t)0x0F) << pos;tmpreg &= ~pinmask;/* Write the mode configuration in the corresponding bits */tmpreg |= (currentmode << pos);/* Reset the corresponding ODR bit */if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD){GPIOx->BRR = (((uint32_t)0x01) << (pinpos + 0x08));}/* Set the corresponding ODR bit */if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU){GPIOx->BSRR = (((uint32_t)0x01) << (pinpos + 0x08));}}}GPIOx->CRH = tmpreg;}

3.GPIO_StructInit

对GPIO初始化类型

GPIO_Pin_All:是用于判断此时这个GPIO是否初始化

/*** @brief  Fills each GPIO_InitStruct member with its default value.* @param  GPIO_InitStruct : pointer to a GPIO_InitTypeDef structure which will*         be initialized.* @retval None*/
void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
{/* Reset GPIO init structure parameters values */
//GPIO_Pin_All:表示此时初始化到的这个pin是不存在的,表示此时还未指向GPIO_InitStruct->GPIO_Pin  = GPIO_Pin_All;GPIO_InitStruct->GPIO_Speed = GPIO_Speed_2MHz;GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN_FLOATING;
}

4.GPIO_ReadInputDataBit

读取哪一个引脚的值

/*** @brief  Reads the specified input port pin.* @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.* @param  GPIO_Pin:  specifies the port bit to read.*   This parameter can be GPIO_Pin_x where x can be (0..15).* @retval The input port pin value.*/
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{uint8_t bitstatus = 0x00;/* Check the parameters */assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GET_GPIO_PIN(GPIO_Pin)); if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)Bit_RESET){bitstatus = (uint8_t)Bit_SET;}else{bitstatus = (uint8_t)Bit_RESET;}return bitstatus;
}

5.GPIO_ReadInputData

读取整个端口

/*** @brief  Reads the specified GPIO input data port.* @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.* @retval GPIO input data port value.*/
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx)
{/* Check the parameters */assert_param(IS_GPIO_ALL_PERIPH(GPIOx));return ((uint16_t)GPIOx->IDR);
}

6.GPIO_ReadOutputDataBit/GPIO_ReadOutputData

/*** @brief  Reads the specified output data port bit.* @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.* @param  GPIO_Pin:  specifies the port bit to read.*   This parameter can be GPIO_Pin_x where x can be (0..15).* @retval The output port pin value.*/
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{uint8_t bitstatus = 0x00;/* Check the parameters */assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GET_GPIO_PIN(GPIO_Pin)); if ((GPIOx->ODR & GPIO_Pin) != (uint32_t)Bit_RESET){bitstatus = (uint8_t)Bit_SET;}else{bitstatus = (uint8_t)Bit_RESET;}return bitstatus;
}/*** @brief  Reads the specified GPIO output data port.* @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.* @retval GPIO output data port value.*/
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx)
{/* Check the parameters */assert_param(IS_GPIO_ALL_PERIPH(GPIOx));return ((uint16_t)GPIOx->ODR);
}

7. GPIO_EventOutputConfig

/*** @brief  Selects the GPIO pin used as Event output.* @param  GPIO_PortSource: selects the GPIO port to be used as source*   for Event output.*   This parameter can be GPIO_PortSourceGPIOx where x can be (A..E).* @param  GPIO_PinSource: specifies the pin for the Event output.*   This parameter can be GPIO_PinSourcex where x can be (0..15).* @retval None*/
void GPIO_EventOutputConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource)
{uint32_t tmpreg = 0x00;/* Check the parameters */assert_param(IS_GPIO_EVENTOUT_PORT_SOURCE(GPIO_PortSource));assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));tmpreg = AFIO->EVCR;/* Clear the PORT[6:4] and PIN[3:0] bits */tmpreg &= EVCR_PORTPINCONFIG_MASK;tmpreg |= (uint32_t)GPIO_PortSource << 0x04;tmpreg |= GPIO_PinSource;AFIO->EVCR = tmpreg;
}

8.GPIO_PinRemapConfig

当我们要使用的引脚不够时,就要将其他不需要使用到的引脚进行复用

void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState)
{uint32_t tmp = 0x00, tmp1 = 0x00, tmpreg = 0x00, tmpmask = 0x00;/* Check the parameters */assert_param(IS_GPIO_REMAP(GPIO_Remap));assert_param(IS_FUNCTIONAL_STATE(NewState));  if((GPIO_Remap & 0x80000000) == 0x80000000){tmpreg = AFIO->MAPR2;}else{tmpreg = AFIO->MAPR;}tmpmask = (GPIO_Remap & DBGAFR_POSITION_MASK) >> 0x10;tmp = GPIO_Remap & LSB_MASK;if ((GPIO_Remap & (DBGAFR_LOCATION_MASK | DBGAFR_NUMBITS_MASK)) == (DBGAFR_LOCATION_MASK | DBGAFR_NUMBITS_MASK)){tmpreg &= DBGAFR_SWJCFG_MASK;AFIO->MAPR &= DBGAFR_SWJCFG_MASK;}else if ((GPIO_Remap & DBGAFR_NUMBITS_MASK) == DBGAFR_NUMBITS_MASK){tmp1 = ((uint32_t)0x03) << tmpmask;tmpreg &= ~tmp1;tmpreg |= ~DBGAFR_SWJCFG_MASK;}else{tmpreg &= ~(tmp << ((GPIO_Remap >> 0x15)*0x10));tmpreg |= ~DBGAFR_SWJCFG_MASK;}if (NewState != DISABLE){tmpreg |= (tmp << ((GPIO_Remap >> 0x15)*0x10));}if((GPIO_Remap & 0x80000000) == 0x80000000){AFIO->MAPR2 = tmpreg;}else{AFIO->MAPR = tmpreg;}  
}

9.GPIO_EXTILineConfig

与外部中断有关

/*** @brief  Selects the GPIO pin used as EXTI Line.* @param  GPIO_PortSource: selects the GPIO port to be used as source for EXTI lines.*   This parameter can be GPIO_PortSourceGPIOx where x can be (A..G).* @param  GPIO_PinSource: specifies the EXTI line to be configured.*   This parameter can be GPIO_PinSourcex where x can be (0..15).* @retval None*/
void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource)
{uint32_t tmp = 0x00;/* Check the parameters */assert_param(IS_GPIO_EXTI_PORT_SOURCE(GPIO_PortSource));assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));tmp = ((uint32_t)0x0F) << (0x04 * (GPIO_PinSource & (uint8_t)0x03));AFIO->EXTICR[GPIO_PinSource >> 0x02] &= ~tmp;AFIO->EXTICR[GPIO_PinSource >> 0x02] |= (((uint32_t)GPIO_PortSource) << (0x04 * (GPIO_PinSource & (uint8_t)0x03)));
}

3.使用GPIO库函数点亮LED

1.接线

我们连接的是GPIOB8-GPIOB15

注意点:我们这里是反着接的【要将引脚一一对应】

2.代码实现

#include "led.h"
#include "stm32f10x.h"                  // Device headerstatic void delay(){unsigned int i=0,j=0;for(i=0;i<1000;i++){for(j=0;j<2000;j++){}}
}
/*
void led_init(){//因为RCC部分还没有定义结构体,所以还是按照原来的方式去操作RCC->APB2ENR = 0x00000008;GPIOB->CRH=0x33333333;GPIOB->ODR=0x00000000;
}void delay(){unsigned int i=0,j=0;for(i=0;i<1000;i++){for(j=0;j<2000;j++){}}
}
void led_flash(void){unsigned int i=0;for(i=0;i<3;i++){GPIOB->ODR = 0x00000000;//全亮delay();GPIOB->ODR = 0x0000ff00;//全灭delay();} 
}*///硬件实际用到的是GPIOB8-GPIOB15
//使用标准库中的GPIO进行封装API来进行GPIO设置,以点亮LED//定义一个结构体:描述GPIO的速度,频率等
GPIO_InitTypeDef gpio_InitStructure;/**注意点:如果我们没有去操纵某一个引脚的时候默认上电后,灯全部亮
*/
void led_init(){//一、通过GPIO初始化来实现//记得一定要加上时钟RCC->APB2ENR=0x00000008;//单独设置//实际操纵寄存器去将GPB8设置为推挽输出模式,并且速率50MHZgpio_InitStructure.GPIO_Pin=GPIO_Pin_9;gpio_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;gpio_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)GPIO_Init(GPIOB,&gpio_InitStructure);//二、设置LED亮,则需要输出模式//此时操纵一个引脚//操纵BSRR寄存器设置GPB8输出1,让LED点亮//void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)GPIO_SetBits(GPIOB,GPIO_Pin_9);}void led_flash(){unsigned int i=0;for(i=0;i<3;i++){GPIO_SetBits(GPIOB,GPIO_Pin_9);//GPB9亮delay();GPIO_SetBits(GPIOB,GPIO_Pin_9);//GPB9灭delay();}}

这篇关于【STM32】GPIO控制LED(HAL库版)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/285577

相关文章

用js控制视频播放进度基本示例代码

《用js控制视频播放进度基本示例代码》写前端的时候,很多的时候是需要支持要网页视频播放的功能,下面这篇文章主要给大家介绍了关于用js控制视频播放进度的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言html部分:JavaScript部分:注意:总结前言在javascript中控制视频播放

Python异步编程中asyncio.gather的并发控制详解

《Python异步编程中asyncio.gather的并发控制详解》在Python异步编程生态中,asyncio.gather是并发任务调度的核心工具,本文将通过实际场景和代码示例,展示如何结合信号量... 目录一、asyncio.gather的原始行为解析二、信号量控制法:给并发装上"节流阀"三、进阶控制

使用DrissionPage控制360浏览器的完美解决方案

《使用DrissionPage控制360浏览器的完美解决方案》在网页自动化领域,经常遇到需要保持登录状态、保留Cookie等场景,今天要分享的方案可以完美解决这个问题:使用DrissionPage直接... 目录完整代码引言为什么要使用已有用户数据?核心代码实现1. 导入必要模块2. 关键配置(重点!)3.

SpringSecurity 认证、注销、权限控制功能(注销、记住密码、自定义登入页)

《SpringSecurity认证、注销、权限控制功能(注销、记住密码、自定义登入页)》SpringSecurity是一个强大的Java框架,用于保护应用程序的安全性,它提供了一套全面的安全解决方案... 目录简介认识Spring Security“认证”(Authentication)“授权” (Auth

python之流程控制语句match-case详解

《python之流程控制语句match-case详解》:本文主要介绍python之流程控制语句match-case使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录match-case 语法详解与实战一、基础值匹配(类似 switch-case)二、数据结构解构匹

Spring Security注解方式权限控制过程

《SpringSecurity注解方式权限控制过程》:本文主要介绍SpringSecurity注解方式权限控制过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、摘要二、实现步骤2.1 在配置类中添加权限注解的支持2.2 创建Controller类2.3 Us

Python中如何控制小数点精度与对齐方式

《Python中如何控制小数点精度与对齐方式》在Python编程中,数据输出格式化是一个常见的需求,尤其是在涉及到小数点精度和对齐方式时,下面小编就来为大家介绍一下如何在Python中实现这些功能吧... 目录一、控制小数点精度1. 使用 round() 函数2. 使用字符串格式化二、控制对齐方式1. 使用

Springboot控制反转与Bean对象的方法

《Springboot控制反转与Bean对象的方法》文章介绍了SpringBoot中的控制反转(IoC)概念,描述了IoC容器如何管理Bean的生命周期和依赖关系,它详细讲解了Bean的注册过程,包括... 目录1 控制反转1.1 什么是控制反转1.2 SpringBoot中的控制反转2 Ioc容器对Bea

浅析如何使用Swagger生成带权限控制的API文档

《浅析如何使用Swagger生成带权限控制的API文档》当涉及到权限控制时,如何生成既安全又详细的API文档就成了一个关键问题,所以这篇文章小编就来和大家好好聊聊如何用Swagger来生成带有... 目录准备工作配置 Swagger权限控制给 API 加上权限注解查看文档注意事项在咱们的开发工作里,API

Spring IOC控制反转的实现解析

《SpringIOC控制反转的实现解析》:本文主要介绍SpringIOC控制反转的实现,IOC是Spring的核心思想之一,它通过将对象的创建、依赖注入和生命周期管理交给容器来实现解耦,使开发者... 目录1. IOC的基本概念1.1 什么是IOC1.2 IOC与DI的关系2. IOC的设计目标3. IOC