STM32的第三天

  • 库函数与寄存器开发区别

寄存器

寄存器开发优点

直接操作寄存器,运行效率高。

寄存器开发缺点

  1. 开发难度大,开发周期长
  2. 代码可阅读性差,可移植差
  3. 后期维护难度高

库函数

库函数开优点

  1. 开发难度较小,开发周期短
  2. 代码可阅读性强,可移植高
  3. 后期维护难度低

库函数开缺点

相对于寄存器开发,运行效率略低

注意:库函数其实是ST公司对寄存器的进一步封装。

  • 库函数开发LED

库函数开发LED要添加的库函数文件:stm32f4xx_gpio.c

  1. 理解led灯原理图

LED0连接在PF9

PF9输出低电平(0),灯亮;PF9输出高电平(0),灯灭;

  1. 打开GPIOF组时钟

在STM32芯片中,所有的外设时钟是不打开,为了降低功耗

//打开GPIOF组时钟

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);

  1. 设置PF9灯为输出模式  输出推挽 上拉 速度(50MHZ)

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9; //引脚

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; //输出模式

GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //输出推挽

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHZ

GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉

GPIO_Init(GPIOF, &GPIO_InitStruct);

4、通过下面控制LED灯亮与灭

GPIO_SetBits()   引脚置1

GPIO_ResetBits() 引脚置0

  • 按键库函数开发

库函数开发按键要添加的库函数文件:stm32f4xx_gpio.c

1、理解控制原理

KEY0连接在PA0

按键未按下,PA0为高电平

按键 按下,PA0为低电平

 

  1. 打开GPIOA组时钟

//打开GPIOA组时钟

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

  1. 设置引脚为输入,上拉。

GPIO_InitTypeDef  GPIO_InitStruct;

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、通过下面函数获取引脚电平

uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)

  • 中断

中断概念

中断是指计算机运行过程中,出现某些意外情况需主机干预时,机器能自动停止正在运行的程序并转入处理新情况的程序,处理完毕后又返回原被暂停的程序继续运行(面试题)。

STM32外部中部需要注重知识点EXTI与NVIC

4.1、外部中断/事件控制器 (EXTI)

EXTI 控制器的主要特性如下:
每个中断/事件线上都具有独立的触发和屏蔽
每个中断线都具有专用的状态位
支持多达 23 个软件事件/中断请求
检测脉冲宽度低于 APB2 时钟宽度的外部信号。有关此参数的详细信息,请参见
STM32F4xx 数据手册的电气特性部分。

上升沿:数字电路中,数字电平从低电平(数字“0”)变为高电平(数字“1”)的那一瞬间(时刻)叫作上升沿。

下降沿:数字电路中,数字电平从高电平(数字“1”)变为低电平(数字“0”)的那一瞬间叫作下降沿。

 

4.2 NVIC

嵌套向量中断控制器 (NVIC)

NVIC 特性
嵌套向量中断控制器 NVIC 包含以下特性:
STM32F405xx/07xx STM32F415xx/17xx 具有 82 个可屏蔽中断通道, STM32F42xxx
STM32F43xxx 具有多达 86 个可屏蔽中断通道(不包括 Cortex™-M4F 16 根中
断线)
16 个可编程优先级(使用了 4 位中断优先级)
低延迟异常和中断处理
电源管理控制
系统控制寄存器的实现
嵌套向量中断控制器 (NVIC) 和处理器内核接口紧密配合,可以实现低延迟的中断处理和晚
到中断的高效处理。

外部中断配置流程

外部中断配置流程要添加的库函数:stm32f4xx_exti.c和stm32f4xx_syscfg.c

  1. 理解按键原理

KEY0 连接PA0,选择下降沿触发

 

  1. 设置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  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
  */
void RCC_AHB1PeriphClockCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState)
函数功能:使能AHB1外设时钟
返回值:无
uint32_t RCC_AHB1Periph:哪个外设
FunctionalState NewState:ENABLE or DISABLE


/**
  * @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
  */
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
函数功能:GPIO初始化
返回值:无
GPIO_TypeDef* GPIOx:哪个GPIO组
GPIO_InitTypeDef* GPIO_InitStruct:GPIO结构
typedef struct
{
  uint32_t GPIO_Pin;              //哪个引脚

  GPIOMode_TypeDef GPIO_Mode;     //模式

  GPIOSpeed_TypeDef GPIO_Speed;   //速度

  GPIOOType_TypeDef GPIO_OType;   //输出类型

  GPIOPuPd_TypeDef GPIO_PuPd;     //上下拉
}GPIO_InitTypeDef;

/**
  * @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)
函数说明:NVIC初始化
返回值:无
NVIC_InitTypeDef* NVIC_InitStruct:NVIC结构体
typedef struct
{
  uint8_t NVIC_IRQChannel;                    //中断通道,可在stm32f4xx.h文件当中查找

  uint8_t NVIC_IRQChannelPreemptionPriority;  //抢占优先级

  uint8_t NVIC_IRQChannelSubPriority;         //响应优先级

  FunctionalState NVIC_IRQChannelCmd;         //中断通道使能   
} NVIC_InitTypeDef;

中断按键代码

main.c

#include "stm32f4xx.h"
#include "led.h"
#include "key.h"
#include "exti.h"

void delay(int n)
{
	int i, j;

	for(i=0; i<n; i++)
		for(j=0; j<40000; j++);
}


int main(void)
{
	
	//设置NVIC分组(一个工程只能有一个分组) 抢占优先级范围:0~3  响应优先级范围:0~3 
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	Led_Init();
	Exti_PA0_Init();
	
	while(1)
	{
		delay(1000);
		//GPIO_ToggleBits(GPIOE, GPIO_Pin_14);
	}
	
	return 0;
}

led.c

#include "led.h"

/*************************************************
引脚说明:
LED0连接在PF9
PF9输出低电平(0),灯亮;PF9输出高电平(1),灯灭;

LED0 -- PF9
LED1 -- PF10
LED2 -- PE13
LED3 -- PE14

**************************************************/

void Led_Init(void)
{
	GPIO_InitTypeDef  GPIO_InitStruct;

	//打开GPIOE组时钟 
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);	
	//打开GPIOF组时钟 
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
	
	GPIO_InitStruct.GPIO_Pin	= GPIO_Pin_9|GPIO_Pin_10;		//引脚9 10
	GPIO_InitStruct.GPIO_Mode	= GPIO_Mode_OUT;				//输出模式
	GPIO_InitStruct.GPIO_OType	= GPIO_OType_PP;				//推挽输出
	GPIO_InitStruct.GPIO_PuPd	= GPIO_PuPd_UP;					//上拉
	GPIO_InitStruct.GPIO_Speed	= GPIO_Speed_50MHz;				//速度
	
	GPIO_Init(GPIOF, &GPIO_InitStruct);
	
	
	GPIO_InitStruct.GPIO_Pin	= GPIO_Pin_13|GPIO_Pin_14;		//引脚13 14
	GPIO_InitStruct.GPIO_Mode	= GPIO_Mode_OUT;				//输出模式
	GPIO_InitStruct.GPIO_OType	= GPIO_OType_PP;				//推挽输出
	GPIO_InitStruct.GPIO_PuPd	= GPIO_PuPd_UP;					//上拉
	GPIO_InitStruct.GPIO_Speed	= GPIO_Speed_50MHz;				//速度
	
	GPIO_Init(GPIOE, &GPIO_InitStruct);	
	
	//LED0灯灭
	GPIO_SetBits(GPIOF, GPIO_Pin_9);   //引脚置1
	//LED1灯灭
	GPIO_SetBits(GPIOF, GPIO_Pin_10); //引脚置1		
	//LED2灯灭
	GPIO_SetBits(GPIOE, GPIO_Pin_13); //引脚置1
	//LED3灯灭
	GPIO_SetBits(GPIOE, GPIO_Pin_14); //引脚置1
					
}

key.c

#include "key.h"


/***************************************
引脚说明:
KEY0连接在PA0
按键未按下,PA0为高电平
按键 按下,PA0为低电平
****************************************/

void Key_Init(void)
{
	GPIO_InitTypeDef  GPIO_InitStruct;

	//打开GPIOA组时钟 
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);	

	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);

}

exti.c

#include "exti.h"

/**************************************
KEY0 -- PA0

KEY0 连接PA0,选择下降沿触发

**************************************/

void Exti_PA0_Init(void)
{
	GPIO_InitTypeDef  GPIO_InitStruct;
	EXTI_InitTypeDef  EXTI_InitStruct;
	NVIC_InitTypeDef  NVIC_InitStruct;
	
	//使能SYSCFG时钟: 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
	//打开GPIOA组时钟 
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);	

	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);
	
	//设置IO口与中断线的映射关系。PA0 -- EXTI0
    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
	
	EXTI_InitStruct.EXTI_Line	= EXTI_Line0;			//中断线0
	EXTI_InitStruct.EXTI_Mode	= EXTI_Mode_Interrupt;	//中断
	EXTI_InitStruct.EXTI_Trigger= EXTI_Trigger_Falling; //下降沿
	EXTI_InitStruct.EXTI_LineCmd= ENABLE;				//使能
	//初始化线上中断,设置触发条件等。
    EXTI_Init(&EXTI_InitStruct);	
		
	NVIC_InitStruct.NVIC_IRQChannel						= EXTI0_IRQn;   //中断通道
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority	= 1;			//抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority			= 1;			//响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd					= ENABLE;		//使能
	//配置中断分组(NVIC),并使能中断。
    NVIC_Init(&NVIC_InitStruct);	
	
}


void delays(int n)
{
	int i, j;

	for(i=0; i<n; i++)
		for(j=0; j<40000; j++);
}



/********************************
EXTI0中断服务函数
所有中断函数可在startup_stm32f40_41xxx.s中查找 
中断函数不需要自主调用,当满足中断条件,CPU自动调用
********************************/
void EXTI0_IRQHandler(void)
{
	//判断中断标志位是否置为1
	if(EXTI_GetITStatus(EXTI_Line0) == SET)
	{
		
		delays(15);
		
		if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET)
		{
			GPIO_ToggleBits(GPIOF, GPIO_Pin_9);
		}
		//清除中断标志位
		EXTI_ClearITPendingBit(EXTI_Line0);
	}
}

  • 11
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值