新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > STM32 按鍵EXTI方式 使用ST3.0.0庫

STM32 按鍵EXTI方式 使用ST3.0.0庫

作者: 時間:2016-12-03 來源:網(wǎng)絡 收藏
把IO配置為 EXTI中斷的步驟:

1:使能EXTI Line時鐘和第二功能時鐘。

本文引用地址:http://m.butianyuan.cn/article/201612/325165.htm

2:配置EXTI Line中斷優(yōu)先級(NVIC)

3:配置EXTI Line IO (哪個引腳,輸入模式,初始化)

4:把引腳配置為EXTI Line的中斷源(GPIO操作),配置IO口的工作模式

5:配置EXTI Line的工作模式。

==============================================================================

例如:

GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;

/* config the extiline(PB0) clock and AFIO clock 使能extiline IO時鐘和第二功能時鐘*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);

/* config the NVIC(PB0) 配置extiline中斷優(yōu)先級*/
NVIC_Configuration();

/* EXTI line gpio config(PB0) 配置EXTI line gpio */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; // 上拉輸入
GPIO_Init(GPIOB, &GPIO_InitStructure);

/* EXTI line(PB0) mode config 選定IO EXTI的IO線和IO口的中斷模式 */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0); //選定PB0為中斷源

EXTI_InitStructure.EXTI_Line = EXTI_Line0;//把PB0連接到EXTI_Line0中斷線上
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //中斷模式
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿中斷
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//使能外部中斷
EXTI_Init(&EXTI_InitStructure); //初始化

/*
* 函數(shù)名:NVIC_Configuration
* 描述 :配置嵌套向量中斷控制器NVIC
* 輸入 :無
* 輸出 :無
* 調(diào)用 :內(nèi)部調(diào)用
*/
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

/* 配置P[A|B|C|D|E]0為中斷源 */
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

接下來要寫中斷函數(shù)了

在stm32f10x_it.c中編寫中斷服務函數(shù)

/* I/O線中斷,中斷線為PB0 */
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET) //確保是否產(chǎn)生了EXTI Line中斷
{
// LED1 取反
GPIO_WriteBit(GPIOC, GPIO_Pin_3,
(BitAction)((1-GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_3))));
EXTI_ClearITPendingBit(EXTI_Line0); //清除中斷標志位
}
}

要注意的是在中斷服務程序完成之后還要記得清除中斷標志位.

這里要說明一下中斷的分陪

EXTI0

EXTI1

EXTI2

EXTI3

EXTI4

EXTI5-9

EXTI10-15

中斷0-4是每個中斷線各自用一個中斷函數(shù),5-9是共用一個中斷函數(shù),10-15共用一個中斷函數(shù)。

關于中斷服務函數(shù)的命名的編寫方法是:

在startup_stm32f10x_hd.s文件中查找外部中斷源

; External Interrupts
DCD WWDG_IRQHandler ; Window Watchdog
DCD PVD_IRQHandler ; PVD through EXTI Line detect
DCD TAMPER_IRQHandler ; Tamper
DCD RTC_IRQHandler ; RTC
DCD FLASH_IRQHandler ; Flash
DCD RCC_IRQHandler ; RCC
DCD EXTI0_IRQHandler ; EXTI Line 0
DCD EXTI1_IRQHandler ; EXTI Line 1
DCD EXTI2_IRQHandler ; EXTI Line 2
DCD EXTI3_IRQHandler ; EXTI Line 3
DCD EXTI4_IRQHandler ; EXTI Line 4
DCD DMA1_Channel1_IRQHandler ; DMA1 Channel 1
DCD DMA1_Channel2_IRQHandler ; DMA1 Channel 2
DCD DMA1_Channel3_IRQHandler ; DMA1 Channel 3
DCD DMA1_Channel4_IRQHandler ; DMA1 Channel 4
DCD DMA1_Channel5_IRQHandler ; DMA1 Channel 5
DCD DMA1_Channel6_IRQHandler ; DMA1 Channel 6
DCD DMA1_Channel7_IRQHandler ; DMA1 Channel 7
DCD ADC1_2_IRQHandler ; ADC1 & ADC2
DCD USB_HP_CAN1_TX_IRQHandler ; USB High Priority or CAN1 TX
DCD USB_LP_CAN1_RX0_IRQHandler ; USB Low Priority or CAN1 RX0
DCD CAN1_RX1_IRQHandler ; CAN1 RX1
DCD CAN1_SCE_IRQHandler ; CAN1 SCE
DCD EXTI9_5_IRQHandler ; EXTI Line 9..5
DCD TIM1_BRK_IRQHandler ; TIM1 Break
DCD TIM1_UP_IRQHandler ; TIM1 Update
DCD TIM1_TRG_COM_IRQHandler ; TIM1 Trigger and Commutation
DCD TIM1_CC_IRQHandler ; TIM1 Capture Compare
DCD TIM2_IRQHandler ; TIM2
DCD TIM3_IRQHandler ; TIM3
DCD TIM4_IRQHandler ; TIM4
DCD I2C1_EV_IRQHandler ; I2C1 Event
DCD I2C1_ER_IRQHandler ; I2C1 Error
DCD I2C2_EV_IRQHandler ; I2C2 Event
DCD I2C2_ER_IRQHandler ; I2C2 Error
DCD SPI1_IRQHandler ; SPI1
DCD SPI2_IRQHandler ; SPI2
DCD USART1_IRQHandler ; USART1
DCD USART2_IRQHandler ; USART2
DCD USART3_IRQHandler ; USART3
DCD EXTI15_10_IRQHandler ; EXTI Line 15..10
DCD RTCAlarm_IRQHandler ; RTC Alarm through EXTI Line
DCD USBWakeUp_IRQHandler ; USB Wakeup from suspend
DCD TIM8_BRK_IRQHandler ; TIM8 Break
DCD TIM8_UP_IRQHandler ; TIM8 Update
DCD TIM8_TRG_COM_IRQHandler ; TIM8 Trigger and Commutation
DCD TIM8_CC_IRQHandler ; TIM8 Capture Compare
DCD ADC3_IRQHandler ; ADC3
DCD FSMC_IRQHandler ; FSMC
DCD SDIO_IRQHandler ; SDIO
DCD TIM5_IRQHandler ; TIM5
DCD SPI3_IRQHandler ; SPI3
DCD UART4_IRQHandler ; UART4
DCD UART5_IRQHandler ; UART5
DCD TIM6_IRQHandler ; TIM6
DCD TIM7_IRQHandler ; TIM7
DCD DMA2_Channel1_IRQHandler ; DMA2 Channel1
DCD DMA2_Channel2_IRQHandler ; DMA2 Channel2
DCD DMA2_Channel3_IRQHandler ; DMA2 Channel3
DCD DMA2_Channel4_5_IRQHandler ; DMA2 Channel4 & Channel5

============================================================================

void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource)

把IO引腳配置為 EXTI Line,把IO引腳作為EXTI中斷線使用,是IO的第二功能。

GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0); //選定PB0為中斷源(中斷線)

==============================================================================

獲取中斷線的狀態(tài)

ITStatus EXTI_GetITStatus(uint32_t EXTI_Line)

例如:
if(EXTI_GetITStatus(EXTI_Line0) != RESET) //確保是否產(chǎn)生了EXTI Line中斷,也可以不加這條if語句(本人的理解)
{
// LED1 取反
GPIO_WriteBit(GPIOC, GPIO_Pin_3,(BitAction)((1-GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_3))));
EXTI_ClearITPendingBit(EXTI_Line0); //清除中斷標志位
}

============================================================================================

void EXTI_ClearITPendingBit(uint32_t EXTI_Line)

例如:

EXTI_ClearITPendingBit(EXTI_Line0); //清除中斷標志位 Clears the EXTI line pending bits. 清EXTI Line0的中斷標志位。



關鍵詞: STM32按鍵EXTI方

評論


技術專區(qū)

關閉