【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();}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/150893.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

行业追踪,2023-10-26

自动复盘 2023-10-26 凡所有相&#xff0c;皆是虚妄。若见诸相非相&#xff0c;即见如来。 k 线图是最好的老师&#xff0c;每天持续发布板块的rps排名&#xff0c;追踪板块&#xff0c;板块来开仓&#xff0c;板块去清仓&#xff0c;丢弃自以为是的想法&#xff0c;板块去留让…

c++的4中类型转换操作符(static_cast,reinterpret_cast,dynamic_cast,const_cast),RTTI

目录 引入 介绍 static_cast 介绍 使用 reinterpret_cast 介绍 使用 const_cast 介绍 使用 dynamic_cast 介绍 使用 RTTI(运行时确定类型) 介绍 typeid运算符 dynamic_cast运算符 type_info类 引入 原本在c中,我们就已经接触到了很多类型转换 -- 隐式类型转…

虹科 | 解决方案 | 汽车示波器 索赔管理方案

索赔管理 Pico汽车示波器应用于主机厂/供应商与服务店/4S店的协作&#xff0c;实现产品索赔工作的高效管理&#xff1b;同时收集的故障波形数据&#xff0c;便于日后的产品优化和改进 故障记录 在索赔申请过程中&#xff0c;Pico汽车示波器的数据记录功能可以用于捕捉故障时的…

element-ui vue2 iframe 嵌入外链新解

效果如图 实现原理 在路由中通过 props 传值 {path: /iframe,component: Layout,meta: { title: 小助手, icon: example },children: [{path: chatglm,name: chatglm,props: { name: chatglm,url: https://chatglm.cn },component: () > import(/views/iframe/common),me…

shell脚本变量

目录 1.变量的定义 2.shell脚本中变量的定义方法 3.变量的转译 4.Linux中命令的别名设定 5.用户环境变量的更改 6.利用命令的执行结果设定变量 7.脚本函数 1.变量的定义 1&#xff09;定义本身 变量就是内存一片区域的地址 2)变量存在的意义 命令无法操作一直变化的目…

Sql Server中的表组织和索引组织(聚集索引结构,非聚集索引结构,堆结构)

正文 SqlServer用三种方法来组织其分区中的数据或索引页&#xff1a; 1、聚集索引结构 聚集索引是按B树结构进行组织的&#xff0c;B树中的每一页称为一个索引节点。每个索引行包含一个键值和一个指针。指针指向B树上的某一中间级页&#xff08;比如根节点指向中间级节点中的…

六零导航页SQL注入漏洞复现(CVE-2023-45951)

0x01 产品简介 LyLme Spage&#xff08;六零导航页&#xff09;是中国六零&#xff08;LyLme&#xff09;开源的一个导航页面。致力于简洁高效无广告的上网导航和搜索入口&#xff0c;支持后台添加链接、自定义搜索引擎&#xff0c;沉淀最具价值链接&#xff0c;全站无商业推广…

Druid 任意文件读取 (CVE-2021-36749)

Druid 任意文件读取 &#xff08;CVE-2021-36749&#xff09; 漏洞描述 由于用户指定 HTTP InputSource 没有做出限制&#xff0c;可以通过将文件 URL 传递给 HTTP InputSource 来绕过应用程序级别的限制。攻击者可利用该漏洞在未授权情况下&#xff0c;构造恶意请求执行文件…

【Pytorch】Pytorch学习笔记02 - 单变量时间序列 LSTM

目录 说明简单神经网络LSTM原理Pytorch LSTM生成数据初始化前向传播方法训练模型自动化模型构建 总结参考文献 说明 这篇文章主要介绍如何使用PyTorch的API构建一个单变量时间序列 LSTM。文章首先介绍了LSTM&#xff0c;解释了它们在时间序列数据中的简单性和有效性。然后&…

Chimera:混合的 RLWE-FHE 方案

参考文献&#xff1a; [HS14] S. Halevi and V. Shoup. Algorithms in HElib. In Advances in Cryptology–CRYPTO 2014, pages 554–571. Springer, 2014.[HS15] S. Halevi and V. Shoup. Bootstrapping for HElib. In Advances in Cryptology–EUROCRYPT 2015, pages 641–6…

泛微OA之获取每月固定日期

文章目录 1.需求及效果1.1需求1.2效果 2. 思路3. 实现 1.需求及效果 1.1需求 需要获取每个月的7号作为需发布日期&#xff0c;需要自动填充1.2效果 自动获取每个月的七号2. 思路 1.功能并不复杂&#xff0c;可以用泛微前端自带的插入代码块的功能来实现。 2.将这需要赋值的…

【Linux】解决缓存锁问题:无法获得锁 /var/lib/dpkg/lock-frontend

今天在运行apt-get update更新软件包后&#xff0c;突然发现安装新的软件出现了这个报错&#xff1a;正在等待缓存锁&#xff1a;无法获得锁 /var/lib/dpkg/lock-frontend。锁正由进程 1855&#xff08;unattended-upgr&#xff09;持有。如图。 这个错误通常是由于其他进程正在…