stm32中断学习篇(2)——以外部中断为例与多个中断的使用

上一篇简单写了一下中断的理论,给了一个定时器的例子。
这一篇主要写一下外部中断的例子。这个例子中使用了两个外部中断,使用多个的话道理差不多。

还是对照着理论把程序写出来。

配置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

  • 8
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值