STM32标准库之点灯

server/2024/12/15 15:39:06/

1.使能时钟

使能时钟函数原代码如下:

/*** @brief  Enables or disables the High Speed APB (APB2) peripheral clock.* @param  RCC_APB2Periph: specifies the APB2 peripheral to gates its clock.*   This parameter can be any combination of the following values:*     @arg RCC_APB2Periph_AFIO, RCC_APB2Periph_GPIOA, RCC_APB2Periph_GPIOB,*          RCC_APB2Periph_GPIOC, RCC_APB2Periph_GPIOD, RCC_APB2Periph_GPIOE,*          RCC_APB2Periph_GPIOF, RCC_APB2Periph_GPIOG, RCC_APB2Periph_ADC1,*          RCC_APB2Periph_ADC2, RCC_APB2Periph_TIM1, RCC_APB2Periph_SPI1,*          RCC_APB2Periph_TIM8, RCC_APB2Periph_USART1, RCC_APB2Periph_ADC3,*          RCC_APB2Periph_TIM15, RCC_APB2Periph_TIM16, RCC_APB2Periph_TIM17,*          RCC_APB2Periph_TIM9, RCC_APB2Periph_TIM10, RCC_APB2Periph_TIM11     * @param  NewState: new state of the specified peripheral clock.*   This parameter can be: ENABLE or DISABLE.* @retval None*/
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
{/* Check the parameters */assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));assert_param(IS_FUNCTIONAL_STATE(NewState));if (NewState != DISABLE){RCC->APB2ENR |= RCC_APB2Periph;}else{RCC->APB2ENR &= ~RCC_APB2Periph;}
}

根据源代码可知,需要两个参数,第一个参数为使能的寄存器,第二个参数为使能状态

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);

2.设置/初始化端口模式

初始化代码如下,有两个参数,第一个参数选择初始化的端口,第二个参数设置模式

/*** @brief  Initializes the GPIOx peripheral according to the specified*         parameters in the GPIO_InitStruct.* @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.* @param  GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that*         contains the configuration information for the specified GPIO peripheral.* @retval None*/
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
{uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;uint32_t tmpreg = 0x00, pinmask = 0x00;/* Check the parameters */assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));  /*---------------------------- GPIO Mode Configuration -----------------------*/currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00){ /* Check the parameters */assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));/* Output mode */currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;}
/*---------------------------- GPIO CRL Configuration ------------------------*//* Configure the eight low port pins */if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00){tmpreg = GPIOx->CRL;for (pinpos = 0x00; pinpos < 0x08; pinpos++){pos = ((uint32_t)0x01) << pinpos;/* Get the port pins position */currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;if (currentpin == pos){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;}
/*---------------------------- 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;}
}

模式的源代码如下,包含引脚,速度,模式,三项内容。

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.输出

输出高电平的源代码为

/*** @brief  Sets the selected data port bits.* @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.* @param  GPIO_Pin: specifies the port bits to be written.*   This parameter can be any combination of GPIO_Pin_x where x can be (0..15).* @retval None*/
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{/* Check the parameters */assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GPIO_PIN(GPIO_Pin));GPIOx->BSRR = GPIO_Pin;
}

输出低电平的源代码为

/*** @brief  Clears the selected data port bits.* @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.* @param  GPIO_Pin: specifies the port bits to be written.*   This parameter can be any combination of GPIO_Pin_x where x can be (0..15).* @retval None*/
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{/* Check the parameters */assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GPIO_PIN(GPIO_Pin));GPIOx->BRR = GPIO_Pin;
}

需要两个参数,第一个参数指明哪个寄存器组,第二个参数指明哪个端口

最终点亮的代码为

GPIO_ResetBits(GPIOC,GPIO_Pin_13);


http://www.ppmy.cn/server/150380.html

相关文章

特斯拉公布汽车连接器设计,助推行业标准化

【哔哥哔特导读】近日&#xff0c;特斯拉公开线束连接器设计并邀请行业共同推动统一标准&#xff0c;这将给连接器行业带来何种影响&#xff1f; 「目前&#xff0c;线束和电子连接器行业仍是汽车行业自动化程度最低、标准化统一程度最低的元器件领域之一」 近日&#xff0c;…

夹耳式耳机与骨传导什么区别?夹耳式耳机推荐款式更新!

夹耳式耳机常被错认为骨传导耳机&#xff0c;实际并非如此。它独特地夹在耳廓传播声音&#xff0c;多采用气传导&#xff0c;借空气振动传声&#xff0c;虽少数用骨传导技术&#xff0c;但不能等同。 与骨传导相比&#xff0c;夹耳式耳机优势明显。其声音还原精准&#xff0c;能…

【电子元器件】电感基础知识

本文章是笔者整理的备忘笔记。希望在帮助自己温习避免遗忘的同时&#xff0c;也能帮助其他需要参考的朋友。如有谬误&#xff0c;欢迎大家进行指正。 一、 电感的基本工作原理 1. 电感的基本工作原理如下&#xff1a; &#xff08;1&#xff09; 当线圈中有电流通过时&#…

2024年【高压电工】最新解析及高压电工考试总结

高压电工考试是电力行业从业人员必须通过的资格考试之一&#xff0c;它不仅检验了考生对高压电技术的掌握程度&#xff0c;还考验了考生在实际操作中的安全意识和应急处理能力。为了帮助广大考生更好地备考&#xff0c;本文整理了10道2024年高压电工考试的最新解析及总结试题&a…

leetcode--hash表2

349.两个数组的交集 给定两个数组 nums1 和 nums2 &#xff0c;返回 它们的 交集 。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。 示例 1&#xff1a; 输入&#xff1a;nums1 [1,2,2,1], nums2 [2,2] 输出&#xff1a;[2]示例 2&#xff1a; 输…

基于ZYNQ 7z010开发板 oled点亮的实现

dc拉高的时候就是发送128字节数据的时候 发送指令dc拉低 模式是00 sck先置低再置高 复位是与开发板上的按键一样都是低有效 25位字节指令 加 3字节的 页地址加起始结束 b0,00,10, timescale 1ns / 1ps module top0(input wire clk ,input wire rst_n,// out…

CPU性能优化--基于处理器事件的采样

基于处理器事件的采样processor event based sampling PEBS 是CPU的另一种非常有用的特性&#xff0c;PEBS被用来在每个采样点获取更多的补充数据。在Intel处理器中&#xff0c;PEBS是在NetBrust微架构开始i引入的&#xff0c;在AMD处理器中&#xff0c;类似的特性叫基于指令的…

Please activate LaTeX Workshop sidebar item to render the thumbnail of a PDF

Latex代码中使用pdf图片&#xff0c;无法预览&#xff0c;提示&#xff1a; Please activate LaTeX Workshop sidebar item to render the thumbnail of a PDF 解决办法&#xff1a; 点击左边这个刷新下即可