上一篇简单写了一下中断的理论,给了一个定时器的例子。
这一篇主要写一下外部中断的例子。这个例子中使用了两个外部中断,使用多个的话道理差不多。
还是对照着理论把程序写出来。
配置GPIO的部分就不说了。程序在后面给出,有详细注释。
直接开始说配置NVIC和EXTI。
Stm32的这些配置都是以结构体的形式进行的。
EXTI配置的是EXTI_InitTypeDef这个结构体,其定义如下:
*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;*
// 首先要清除线上的挂起位:
EXTI_ClearITPendingBit(EXTI_Line1);
EXTI_ClearITPendingBit(EXTI_Line2);
// 选择外部中断线路:
EXTI_InitStructure.EXTI_Line=EXTI_Line1;
EXTI_InitStructure.EXTI_Line=EXTI_Line2;
// 设置中断模式为外部中断触发:
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
// 设置中断触发方式:
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
// 中断使能:
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
// 之后要使这些配置生效:
EXTI_Init(&EXTI_InitStructure);
// 并加载到相应的IO上:
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource1);
至此中断配置结束。
下面开始配置NVIC。
这也是一个结构体,同样的:
*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. T