您的位置:首页 > 其它

STM32_外部中断

2016-04-22 21:46 387 查看

一般使用配置

选择中断优先级组

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_x);


以下是用到的宏定义。

#define NVIC_PriorityGroup_0         ((uint32_t)0x700) /*!< 0 bits for pre-emption priority
4 bits for subpriority */
#define NVIC_PriorityGroup_1         ((uint32_t)0x600) /*!< 1 bits for pre-emption priority
3 bits for subpriority */
#define NVIC_PriorityGroup_2         ((uint32_t)0x500) /*!< 2 bits for pre-emption priority
2 bits for subpriority */
#define NVIC_PriorityGroup_3         ((uint32_t)0x400) /*!< 3 bits for pre-emption priority
1 bits for subpriority */
#define NVIC_PriorityGroup_4         ((uint32_t)0x300) /*!< 4 bits for pre-emption priority
0 bits for subpriority */


关于中断优先级组的说明。

/**
@code
The table below gives the allowed values of the pre-emption priority and subpriority according
to the Priority Grouping configuration performed by NVIC_PriorityGroupConfig function
============================================================================================================================
NVIC_PriorityGroup   | NVIC_IRQChannelPreemptionPriority | NVIC_IRQChannelSubPriority  | Description
============================================================================================================================
NVIC_PriorityGroup_0  |                0                  |            0-15             |   0 bits for pre-emption priority
|                                   |                             |   4 bits for subpriority
----------------------------------------------------------------------------------------------------------------------------
NVIC_PriorityGroup_1  |                0-1                |            0-7              |   1 bits for pre-emption priority
|                                   |                             |   3 bits for subpriority
----------------------------------------------------------------------------------------------------------------------------
NVIC_PriorityGroup_2  |                0-3                |            0-3              |   2 bits for pre-emption priority
|                                   |                             |   2 bits for subpriority
----------------------------------------------------------------------------------------------------------------------------
NVIC_PriorityGroup_3  |                0-7                |            0-1              |   3 bits for pre-emption priority
|                                   |                             |   1 bits for subpriority
----------------------------------------------------------------------------------------------------------------------------
NVIC_PriorityGroup_4  |                0-15               |            0                |   4 bits for pre-emption priority
|                                   |                             |   0 bits for subpriority
============================================================================================================================
@endcode
*/


配置嵌套向量中断控制器NVIC

NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = x_x_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);


下面是用到结构体和宏定义。

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;


#ifdef STM32F10X_HD
ADC1_2_IRQn                 = 18,     /*!< ADC1 and ADC2 global Interrupt                       */
USB_HP_CAN1_TX_IRQn         = 19,     /*!< USB Device High Priority or CAN1 TX Interrupts       */
USB_LP_CAN1_RX0_IRQn        = 20,     /*!< USB Device Low Priority or CAN1 RX0 Interrupts       */
CAN1_RX1_IRQn               = 21,     /*!< CAN1 RX1 Interrupt                                   */
CAN1_SCE_IRQn               = 22,     /*!< CAN1 SCE Interrupt                                   */
EXTI9_5_IRQn                = 23,     /*!< External Line[9:5] Interrupts                        */
TIM1_BRK_IRQn               = 24,     /*!< TIM1 Break Interrupt                                 */
TIM1_UP_IRQn                = 25,     /*!< TIM1 Update Interrupt                                */
TIM1_TRG_COM_IRQn           = 26,     /*!< TIM1 Trigger and Commutation Interrupt               */
TIM1_CC_IRQn                = 27,     /*!< TIM1 Capture Compare Interrupt                       */
TIM2_IRQn                   = 28,     /*!< TIM2 global Interrupt                                */
TIM3_IRQn                   = 29,     /*!< TIM3 global Interrupt                                */
TIM4_IRQn                   = 30,     /*!< TIM4 global Interrupt                                */
I2C1_EV_IRQn                = 31,     /*!< I2C1 Event Interrupt                                 */
I2C1_ER_IRQn                = 32,     /*!< I2C1 Error Interrupt                                 */
I2C2_EV_IRQn                = 33,     /*!< I2C2 Event Interrupt                                 */
I2C2_ER_IRQn                = 34,     /*!< I2C2 Error Interrupt                                 */
SPI1_IRQn                   = 35,     /*!< SPI1 global Interrupt                                */
SPI2_IRQn                   = 36,     /*!< SPI2 global Interrupt                                */
USART1_IRQn                 = 37,     /*!< USART1 global Interrupt                              */
USART2_IRQn                 = 38,     /*!< USART2 global Interrupt                              */
USART3_IRQn                 = 39,     /*!< USART3 global Interrupt                              */
EXTI15_10_IRQn              = 40,     /*!< External Line[15:10] Interrupts                      */
RTCAlarm_IRQn               = 41,     /*!< RTC Alarm through EXTI Line Interrupt                */
USBWakeUp_IRQn              = 42,     /*!< USB Device WakeUp from suspend through EXTI Line Interrupt */
TIM8_BRK_IRQn               = 43,     /*!< TIM8 Break Interrupt                                 */
TIM8_UP_IRQn                = 44,     /*!< TIM8 Update Interrupt                                */
TIM8_TRG_COM_IRQn           = 45,     /*!< TIM8 Trigger and Commutation Interrupt               */
TIM8_CC_IRQn                = 46,     /*!< TIM8 Capture Compare Interrupt                       */
ADC3_IRQn                   = 47,     /*!< ADC3 global Interrupt                                */
FSMC_IRQn                   = 48,     /*!< FSMC global Interrupt                                */
SDIO_IRQn                   = 49,     /*!< SDIO global Interrupt                                */
TIM5_IRQn                   = 50,     /*!< TIM5 global Interrupt                                */
SPI3_IRQn                   = 51,     /*!< SPI3 global Interrupt                                */
UART4_IRQn                  = 52,     /*!< UART4 global Interrupt                               */
UART5_IRQn                  = 53,     /*!< UART5 global Interrupt                               */
TIM6_IRQn                   = 54,     /*!< TIM6 global Interrupt                                */
TIM7_IRQn                   = 55,     /*!< TIM7 global Interrupt                                */
DMA2_Channel1_IRQn          = 56,     /*!< DMA2 Channel 1 global Interrupt                      */
DMA2_Channel2_IRQn          = 57,     /*!< DMA2 Channel 2 global Interrupt                      */
DMA2_Channel3_IRQn          = 58,     /*!< DMA2 Channel 3 global Interrupt                      */
DMA2_Channel4_5_IRQn        = 59      /*!< DMA2 Channel 4 and Channel 5 global Interrupt        */
#endif /* STM32F10X_HD */


使能外部中断引脚时钟和复用IO时钟

RCC_APB2PeriphClockCmd( (RCC_APB2Periph_GPIOx | RCC_APB2Periph_AFIO), ENABLE);


配置外部中断引脚

GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_x;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;    //下拉输入
GPIO_Init(GPIOx, &GPIO_InitStructure);


配置外部中断

EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Linexx;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);


下面是使用到的结构体和宏定义。

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;


#define EXTI_Line0       ((uint32_t)0x00001)  /*!< External interrupt line 0 */
#define EXTI_Line1       ((uint32_t)0x00002)  /*!< External interrupt line 1 */
#define EXTI_Line2       ((uint32_t)0x00004)  /*!< External interrupt line 2 */
#define EXTI_Line3       ((uint32_t)0x00008)  /*!< External interrupt line 3 */
#define EXTI_Line4       ((uint32_t)0x00010)  /*!< External interrupt line 4 */
#define EXTI_Line5       ((uint32_t)0x00020)  /*!< External interrupt line 5 */
#define EXTI_Line6       ((uint32_t)0x00040)  /*!< External interrupt line 6 */
#define EXTI_Line7       ((uint32_t)0x00080)  /*!< External interrupt line 7 */
#define EXTI_Line8       ((uint32_t)0x00100)  /*!< External interrupt line 8 */
#define EXTI_Line9       ((uint32_t)0x00200)  /*!< External interrupt line 9 */
#define EXTI_Line10      ((uint32_t)0x00400)  /*!< External interrupt line 10 */
#define EXTI_Line11      ((uint32_t)0x00800)  /*!< External interrupt line 11 */
#define EXTI_Line12      ((uint32_t)0x01000)  /*!< External interrupt line 12 */
#define EXTI_Line13      ((uint32_t)0x02000)  /*!< External interrupt line 13 */
#define EXTI_Line14      ((uint32_t)0x04000)  /*!< External interrupt line 14 */
#define EXTI_Line15      ((uint32_t)0x08000)  /*!< External interrupt line 15 */
#define EXTI_Line16      ((uint32_t)0x10000)  /*!< External interrupt line 16 Connected to the PVD Output */
#define EXTI_Line17      ((uint32_t)0x20000)  /*!< External interrupt line 17 Connected to the RTC Alarm event */
#define EXTI_Line18      ((uint32_t)0x40000)  /*!< External interrupt line 18 Connected to the USB Device/USB OTG FS
Wakeup from suspend event */
#define EXTI_Line19      ((uint32_t)0x80000)  /*!< External interrupt line 19 Connected to the Ethernet Wakeup event */


typedef enum
{
EXTI_Mode_Interrupt = 0x00,  //中断请求
EXTI_Mode_Event = 0x04       //事件请求
}EXTIMode_TypeDef;


typedef enum
{
EXTI_Trigger_Rising = 0x08,          //上升沿触发
EXTI_Trigger_Falling = 0x0C,         //下降沿触发
EXTI_Trigger_Rising_Falling = 0x10   //上升沿和下降沿均触发
}EXTITrigger_TypeDef;


在stm32f10x_it.c中配置中断函数

void x_x_IRQHandler(){
//再次判断是否触发中断(进入中断函数已经是触发中断了)
if(EXTI_GetITStatus(EXTI_Linexx) != RESET){
/*--------do what--------*/
//清除中断标志位
EXTI_ClearITPendingBit(EXTI_Linexx);
}
}


记得在stm32f10x_it.h中添加函数声明。

EXTI_GetITStatus()
检查指定的EXTI线路触发请求发生与否;
EXTI_GetFlagStatus()
检查指定的EXTI线路标志位的设置与否。

开关总中断

core_m3.c

/**
* @brief  Set the Priority Mask value
*
* @param  priMask  PriMask
*
* Set the priority mask bit in the priority mask register
*/
__ASM void __set_PRIMASK(uint32_t priMask)
{
msr primask, r0
bx lr
}


可以写成宏定义。

#define SEI() __set_PRIMASK(0); //开放总中断
#define CLI() __set_PRIMASK(1); //关闭总中断
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: