基于ST库函数开发LED
一、寄存器与库函数开发优缺点
寄存器开发缺点
1、开发难度大
2、可移植性差
3、程序维护难度大
优点:
1、运行速度快
库函数开发缺点
1、运行速度相对寄存器开发慢
优点:
1、开发难度较小
2、可移植性强
3、程序维护难度较小
二、LED开发步骤
库函数开发要添加对应的stm32f4xx_gpio.c文件
1、理解LED电路原理
LED0连接在PF9引脚
当PF9输出高电平(1),灯灭
当PF9输出低电平(0),灯亮
小结论:
如果引脚电平由程序员来改,则为输出
如果引脚电平由外部电路来改变,则为输入
2、打开GPIO F组时钟(系统默认不打开外设时钟,为了功耗)
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOx, ENABLE);
3、设置PF9的模式 输出类型 上下拉电阻 及速度
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9; //引脚9
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; //输出
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //速度
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOF, &GPIO_InitStruct);
4、通过下面库函数,从而控制LED0亮与灭
GPIO_SetBits()/GPIO_ResetBits()
三、按键开发步骤
1、理解按键电路原理
按键KEY0连接PA0
当按键按下时, PA0电平为低电平(0)
当按键未按下时,PA0电平为高电平(1)
2、打开GPIO A组时钟(系统默认不打开外设时钟,为了功耗)
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOx, ENABLE);
3、设置PA0的模式 上下拉电阻 及速度
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; //引脚0
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; //输入
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA, &GPIO_InitStruct);
4、使用库函数获取电平值
GPIO_ReadInputDataBit()
函数说明:
/**
* @brief Enables or disables the AHB1 peripheral clock.
* @note After reset, the peripheral clock (used for registers read/write access)
* is disabled and the application software has to enable this clock before
* using it.
* @param RCC_AHBPeriph: specifies the AHB1 peripheral to gates its clock.
* This parameter can be any combination of the following values:
* @arg RCC_AHB1Periph_GPIOA: GPIOA clock
* @arg RCC_AHB1Periph_GPIOB: GPIOB clock
* @arg RCC_AHB1Periph_GPIOC: GPIOC clock
* @arg RCC_AHB1Periph_GPIOD: GPIOD clock
* @arg RCC_AHB1Periph_GPIOE: GPIOE clock
* @arg RCC_AHB1Periph_GPIOF: GPIOF clock
* @arg RCC_AHB1Periph_GPIOG: GPIOG clock
* @arg RCC_AHB1Periph_GPIOG: GPIOG clock
* @arg RCC_AHB1Periph_GPIOI: GPIOI clock
* @arg RCC_AHB1Periph_GPIOJ: GPIOJ clock (STM32F42xxx/43xxx devices)
* @arg RCC_AHB1Periph_GPIOK: GPIOK clock (STM32F42xxx/43xxx devices)
* @arg RCC_AHB1Periph_CRC: CRC clock
* @arg RCC_AHB1Periph_BKPSRAM: BKPSRAM interface clock
* @arg RCC_AHB1Periph_CCMDATARAMEN CCM data RAM interface clock
* @arg RCC_AHB1Periph_DMA1: DMA1 clock
* @arg RCC_AHB1Periph_DMA2: DMA2 clock
* @arg RCC_AHB1Periph_DMA2D: DMA2D clock (STM32F429xx/439xx devices)
* @arg RCC_AHB1Periph_ETH_MAC: Ethernet MAC clock
* @arg RCC_AHB1Periph_ETH_MAC_Tx: Ethernet Transmission clock
* @arg RCC_AHB1Periph_ETH_MAC_Rx: Ethernet Reception clock
* @arg RCC_AHB1Periph_ETH_MAC_PTP: Ethernet PTP clock
* @arg RCC_AHB1Periph_OTG_HS: USB OTG HS clock
* @arg RCC_AHB1Periph_OTG_HS_ULPI: USB OTG HS ULPI clock
* @param NewState: new state of the specified peripheral clock.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
//初始化AHB1总线外设
void RCC_AHB1PeriphClockCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState)
uint32_t RCC_AHB1Periph:外设选择
FunctionalState NewState:是否使能外设
/**
* @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_InitStruct.
* @param GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F405xx/407xx and STM32F415xx/417xx devices
* x can be (A..I) to select the GPIO peripheral for STM32F42xxx/43xxx devices.
* x can be (A, B, C, D and H) to select the GPIO peripheral for STM32F401xx devices.
* @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that contains
* the configuration information for the specified GPIO peripheral.
* @retval None
*/
//初始化GPIO
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
GPIO_TypeDef* GPIOx:选择IO组(A B C D E F G )
typedef struct
{
uint32_t GPIO_Pin; //引脚
GPIOMode_TypeDef GPIO_Mode; //模式
GPIOSpeed_TypeDef GPIO_Speed; //速度
GPIOOType_TypeDef GPIO_OType; //输出类型
GPIOPuPd_TypeDef GPIO_PuPd; //上下拉电阻
}GPIO_InitTypeDef;
===================STM32中断=============================================
一、什么叫中断
CPU执行程序时,由于发生了某种随机的事件(外部或内部),引起CPU暂时中断正在运行的程序,转去执行一段特殊的服务程序(中断服务子程序或中断处理程序),以处理该事件,该事件处理完后又返回被中断的程序继续执行,这一过程称为中断。
二、EXTI:外部中断/事件控制器
外部中断/事件控制器包含多达 23 个用于产生事件/中断请求的边沿检测器。每根输入线都可单独进行配置,以选择类型(中断或事件)和相应的触发事件(上升沿触发、下降沿触发或边沿触发)。每根输入线还可单独屏蔽。挂起寄存器用于保持中断请求的状态线。
上升沿:数字电平从低电平(0)到高电平(1)那一瞬间
下降沿:数字电平从高电平(1)到低电平(0)那一瞬间
三、NVIC:嵌套向量中断控制器
嵌套向量中断控制器 NVIC 包含以下特性:
● STM32F405xx/07xx 和 STM32F415xx/17xx 具有 82 个可屏蔽中断通道, STM32F42xxx
和 STM32F43xxx 具有多达 86 个可屏蔽中断通道(不包括 Cortex™-M4F 的 16 根中
断线)
● 16 个可编程优先级(使用了 4 位中断优先级)
● 低延迟异常和中断处理
● 电源管理控制
● 系统控制寄存器的实现
数字越小,优先级别越高
抢占优先级 & 响应优先级区别:
高优先级的抢占优先级是可以打断正在进行的低抢占优先级中断的。
抢占优先级相同的中断,高响应优先级不可以打断低响应优先级的中断。
抢占优先级相同的中断,当两个中断同时发生的情况下,哪个响应优先级高,哪个先执行。
如果两个中断的抢占优先级和响应优先级都是一样的话,则看哪个中断先发生就先执行;
四、外部中断PA0设置步骤
1、理解按键电路原理
按键KEY0连接PA0
当按键按下时, PA0电平为低电平(0)
当按键未按下时,PA0电平为高电平(1)
2、设置NVIC分组(记住,整个工程只能设置一次分组)
void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
3、使能SYSCFG时钟:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
4、 初始化IO口为输入。
GPIO_Init();
5、设置IO口与中断线的映射关系。
void SYSCFG_EXTILineConfig();
6、初始化线上中断,设置触发条件等。
EXTI_Init();
7、配置中断分组(NVIC),并使能中断。
NVIC_Init();
8、 编写中断服务函数。
EXTIx_IRQHandler();
9、清除中断标志位
EXTI_ClearITPendingBit();
函数说明:
/**
* @brief Selects the GPIO pin used as EXTI Line.
* @param EXTI_PortSourceGPIOx : selects the GPIO port to be used as source for
* EXTI lines where x can be (A..K) for STM32F42xxx/43xxx devices, (A..I)
* for STM32F405xx/407xx and STM32F415xx/417xx devices or (A, B, C, D and H)
* for STM32401xx devices.
*
* @param EXTI_PinSourcex: specifies the EXTI line to be configured.
* This parameter can be EXTI_PinSourcex where x can be (0..15, except
* for EXTI_PortSourceGPIOI x can be (0..11) for STM32F405xx/407xx
* and STM32F405xx/407xx devices and for EXTI_PortSourceGPIOK x can
* be (0..7) for STM32F42xxx/43xxx devices.
*
* @retval None
*/
//设置IO口与中断线的映射关系。
void SYSCFG_EXTILineConfig(uint8_t EXTI_PortSourceGPIOx, uint8_t EXTI_PinSourcex)
uint8_t EXTI_PortSourceGPIOx:GPIO组
uint8_t EXTI_PinSourcex:映射引脚
/**
* @brief Initializes the EXTI peripheral according to the specified
* parameters in the EXTI_InitStruct.
* @param EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure
* that contains the configuration information for the EXTI peripheral.
* @retval None
*/
void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct)
typedef struct
{
uint32_t EXTI_Line; //中断线
EXTIMode_TypeDef EXTI_Mode; //模式
EXTITrigger_TypeDef EXTI_Trigger; //边沿检测
FunctionalState EXTI_LineCmd; //中断线使能
}EXTI_InitTypeDef;
/**
* @brief Initializes the NVIC peripheral according to the specified
* parameters in the NVIC_InitStruct.
* @note To configure interrupts priority correctly, the NVIC_PriorityGroupConfig()
* function should be called before.
* @param NVIC_InitStruct: pointer to a NVIC_InitTypeDef structure that contains
* the configuration information for the specified NVIC peripheral.
* @retval None
*/
void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)
typedef struct
{
uint8_t NVIC_IRQChannel; //中断请求通道,可在stm32f4xx.h中查阅
uint8_t NVIC_IRQChannelPreemptionPriority; //抢占优先级
uint8_t NVIC_IRQChannelSubPriority; //响应优先级
FunctionalState NVIC_IRQChannelCmd; //NVIC通道使能
} NVIC_InitTypeDef;