STM32F103固件庫編程(4)—中斷服務

STM32F103固件庫編程(4)—中斷服務

系統異常,體現在內核水平
外部中斷,體現在外設水平
NVIC:嵌套向量中斷控制器,屬於內核外設

(一)中斷編程的順序

  1. 使能中斷請求
  2. 配置中斷優先級分組
  3. 配置NVIC寄存器,初始化NVIC_InitTypeDef;
  4. 編寫中斷服務函數
typedef struct
{
  uint8_t NVIC_IRQChannel;                     /*!< Specifies the IRQ channel to be enabled or disabled.
                                                   This parameter can be a value of @ref IRQn_Type 
                                                   (For the complete STM32 Devices IRQ Channels list, please
                                                    refer to stm32f10x.h file) */

  uint8_t NVIC_IRQChannelPreemptionPriority;  /*!< Specifies the pre-emption priority for the IRQ channel
                                                   specified in NVIC_IRQChannel. This parameter can be a value
                                                   between 0 and 15 as described in the table @ref NVIC_Priority_Table */

  uint8_t NVIC_IRQChannelSubPriority;         /*!< Specifies the subpriority level for the IRQ channel specified
                                                   in NVIC_IRQChannel. This parameter can be a value
                                                   between 0 and 15 as described in the table @ref NVIC_Priority_Table */

  FunctionalState NVIC_IRQChannelCmd;         /*!< Specifies whether the IRQ channel defined in NVIC_IRQChannel
                                                   will be enabled or disabled. 
                                                   This parameter can be set either to ENABLE or DISABLE */   
} NVIC_InitTypeDef;

(二)EXTI

EXTI(External interrupt/event controller)—外部中斷/事件控制器,管理了控制器的 20 箇中斷/事件線。每個中斷/事件線都對應有一個邊沿檢測器,可以實現輸入信號的上升沿 檢測和下降沿的檢測。EXTI 可以實現對每個中斷/事件線進行單獨配置,可以單獨配置爲 中斷或者事件,以及觸發事件的屬性
在這裏插入圖片描述

在這裏插入圖片描述中斷屏蔽寄存器(EXTI_IMR)
事件屏蔽寄存器(EXTI_EMR)
上升沿觸發選擇寄存器(EXTI_RTSR)
下降沿觸發選擇寄存器(EXTI_FTSR)
軟件中斷事件寄存器(EXTI_SWIER)
掛起寄存器(EXTI_PR)
在這裏插入圖片描述
在這裏插入圖片描述

typedef struct
{
  uint32_t EXTI_Line;               /*!< Specifies the EXTI lines to be enabled or disabled.
                                         This parameter can be any combination of @ref EXTI_Lines */
   
  EXTIMode_TypeDef EXTI_Mode;       /*!< Specifies the mode for the EXTI lines.
                                         This parameter can be a value of @ref EXTIMode_TypeDef */

  EXTITrigger_TypeDef EXTI_Trigger; /*!< Specifies the trigger signal active edge for the EXTI lines.
                                         This parameter can be a value of @ref EXTIMode_TypeDef */

  FunctionalState EXTI_LineCmd;     /*!< Specifies the new state of the selected EXTI lines.
                                         This parameter can be set either to ENABLE or DISABLE */ 
}EXTI_InitTypeDef;

(三)外部中斷配置寄存器 1(AFIO_EXTICR1)

在這裏插入圖片描述
這些位可由軟件讀寫,用於選擇EXTIx外部中斷的輸入源

(四)代碼部分

main.c

#include "stm32f10x.h"
#include "exti.h"

int main(void)
{
  EXTI_Key_Config();
  while(1);
}

exti.c

#include "exti.h"

//初始化NVIC
static void EXYI_NVIC_Config(void)
{
	NVIC_InitTypeDef NVIC_InitStruct;
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
	
	NVIC_InitStruct.NVIC_IRQChannel=EXTI0_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	
	NVIC_Init(&NVIC_InitStruct);
}

void EXTI_Key_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	EXTI_InitTypeDef EXTI_InitStruct;
	
	//配置中斷優先級
	EXYI_NVIC_Config();
	
	//初始化GPIO口
	RCC_APB2PeriphClockCmd(KEY1_GPIO_CLK, ENABLE);
	
	GPIO_InitStruct.GPIO_Pin=KEY1_GPIO_PIN;
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;

	GPIO_Init(KEY1_GPIO_PORT,&GPIO_InitStruct);
	
	//初始化EXTI
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	
	EXTI_InitStruct.EXTI_Line=EXTI_Line0;
	EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
	EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Rising;
	EXTI_InitStruct.EXTI_LineCmd=ENABLE;
	
	EXTI_Init(&EXTI_InitStruct);
}

exti.h

#ifndef __EXTI_H

#include "stm32f10x.h"

#define KEY1_GPIO_PIN          GPIO_Pin_0
#define KEY1_GPIO_PORT         GPIOA
#define KEY1_GPIO_CLK          RCC_APB2Periph_GPIOA

void EXTI_Key_Config(void);

#define __EXTI_H
#endif

中斷服務函數(在stm32f10x_it.h操作)

首先,#include “exti.h”

void EXTI0_IRQHandler(void)
{
	if(EXTI_GetITStatus(EXTI_Line0)!=0)
		{
			//中斷函數
		}
	//清除中斷標誌
	EXTI_ClearITPendingBit(EXTI_Line0);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章