微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32 按键EXTI方式 使用ST3.0.0库

STM32 按键EXTI方式 使用ST3.0.0库

时间:12-03 来源:互联网 点击:
把IO配置为 EXTI中断的步骤:

1:使能EXTI Line时钟和第二功能时钟。

2:配置EXTI Line中断优先级(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中断优先级*/
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); //初始化

/*
* 函数名:NVIC_Configuration
* 描述 :配置嵌套向量中断控制器NVIC
* 输入 :无
* 输出 :无
* 调用 :内部调用
*/
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);
}

接下来要写中断函数了

在stm32f10x_it.c中编写中断服务函数

/* I/O线中断,中断线为PB0 */
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET) //确保是否产生了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是每个中断线各自用一个中断函数,5-9是共用一个中断函数,10-15共用一个中断函数。

关于中断服务函数的命名的编写方法是:

在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 RTCAl

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top