cortex m0 lpc1114的NVIC中断如何使用

立创EDA,中国自主研发的PCB设计工具,免费使用!云端设计!

LPC1114单片机的NVIC中断函数,有开中断、关中断、设置优先级、挂起等操作函数。这些函数位于core_cm0.h文件里面。比如开中断的函数如下:

/** \brief Enable External Interrupt
The function enables a device-specific interrupt in the NVIC interrupt controller.
\param [in] IRQn External interrupt number. Value cannot be negative.
 */
__STATIC_INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
{
 NVIC->ISER[0] = (1 << ((uint32_t)(IRQn) & 0x1F));
}

/**/里面的注释告诉我们,这是一个中断函数,函数的功能是允许一个中断,也就是开中断的意思。

比如我们要开P1口的中断,可以这样使用这个函数:NVIC_EnableIRQ(EINT1_IRQn);

该函数里面,NVIC_EnableIRQ是函数名,EINT1_IRQn是参数,表示P1口的中断,这个参数可以在头文件lpc11xx.h文件中找到。如下所示:

* ==========================================================================
* ---------- Interrupt Number Definition -----------------------------------
* ==========================================================================
*/
typedef enum IRQn
{
/****** Cortex-M0 Processor Exceptions Numbers ******************************/
Reset_IRQn = -15, /*!< 1 Reset Vector, invoked on Power up and warm reset */
NonMaskableInt_IRQn = -14, /*!< 2 Non maskable Interrupt, cannot be stopped or preempted */
HardFault_IRQn = -13, /*!< 3 Hard Fault, all classes of Fault */
SVCall_IRQn = -5, /*!< 11 System Service Call via SVC instruction */
PendSV_IRQn = -2, /*!< 14 Pendable request for system service */
SysTick_IRQn = -1, /*!< 15 System Tick Timer */

/****** LPC11Cxx or LPC11xx Specific Interrupt Numbers *************************/
WAKEUP0_IRQn = 0, /*!< All I/O pins can be used as wakeup source. */
WAKEUP1_IRQn = 1, /*!< There are 13 pins in total for LPC11xx */
WAKEUP2_IRQn = 2,
WAKEUP3_IRQn = 3,
WAKEUP4_IRQn = 4,
WAKEUP5_IRQn = 5,
WAKEUP6_IRQn = 6,
WAKEUP7_IRQn = 7,
WAKEUP8_IRQn = 8,
WAKEUP9_IRQn = 9,
WAKEUP10_IRQn = 10,
WAKEUP11_IRQn = 11,
WAKEUP12_IRQn = 12,
CAN_IRQn = 13, /*!< CAN Interrupt */
SSP1_IRQn = 14, /*!< SSP1 Interrupt */
I2C_IRQn = 15, /*!< I2C Interrupt */
TIMER_16_0_IRQn = 16, /*!< 16-bit Timer0 Interrupt */
TIMER_16_1_IRQn = 17, /*!< 16-bit Timer1 Interrupt */
TIMER_32_0_IRQn = 18, /*!< 32-bit Timer0 Interrupt */
TIMER_32_1_IRQn = 19, /*!< 32-bit Timer1 Interrupt */
SSP0_IRQn = 20, /*!< SSP0 Interrupt */
UART_IRQn = 21, /*!< UART Interrupt */
Reserved0_IRQn = 22, /*!< Reserved Interrupt */
Reserved1_IRQn = 23,
ADC_IRQn = 24, /*!< A/D Converter Interrupt */
WDT_IRQn = 25, /*!< Watchdog timer Interrupt */
BOD_IRQn = 26, /*!< Brown Out Detect(BOD) Interrupt */
FMC_IRQn = 27, /*!< Flash Memory Controller Interrupt */
EINT3_IRQn = 28, /*!< External Interrupt 3 Interrupt */
EINT2_IRQn = 29, /*!< External Interrupt 2 Interrupt */
EINT1_IRQn = 30, /*!< External Interrupt 1 Interrupt */
EINT0_IRQn = 31, /*!< External Interrupt 0 Interrupt */
} IRQn_Type;

可以看到,共有32种中断,在使用的时候,你要开什么中断,就用NVIC开中断函数把对应的中断打开。

 

开了中断以后,中断函数怎么写呢?

例如P1口的中断函数这样写:

void PIOINT1_IRQHandler()
{
  // 进中断以后执行的代码
}

为什么P1口的中断函数名称是PIOINT1_IRQHandler,这个其实在startup_lpc11xx.s文件中已经定义好了:

 ; External Interrupts
 DCD WAKEUP_IRQHandler ; 16+ 0: Wakeup PIO0.0
 DCD WAKEUP_IRQHandler ; 16+ 1: Wakeup PIO0.1
 DCD WAKEUP_IRQHandler ; 16+ 2: Wakeup PIO0.2
 DCD WAKEUP_IRQHandler ; 16+ 3: Wakeup PIO0.3
 DCD WAKEUP_IRQHandler ; 16+ 4: Wakeup PIO0.4
 DCD WAKEUP_IRQHandler ; 16+ 5: Wakeup PIO0.5
 DCD WAKEUP_IRQHandler ; 16+ 6: Wakeup PIO0.6
 DCD WAKEUP_IRQHandler ; 16+ 7: Wakeup PIO0.7
 DCD WAKEUP_IRQHandler ; 16+ 8: Wakeup PIO0.8
 DCD WAKEUP_IRQHandler ; 16+ 9: Wakeup PIO0.9
 DCD WAKEUP_IRQHandler ; 16+10: Wakeup PIO0.10
 DCD WAKEUP_IRQHandler ; 16+11: Wakeup PIO0.11
 DCD WAKEUP_IRQHandler ; 16+12: Wakeup PIO1.0
 DCD CAN_IRQHandler ; 16+13: CAN
 DCD SSP1_IRQHandler ; 16+14: SSP1
 DCD I2C_IRQHandler ; 16+15: I2C
 DCD TIMER16_0_IRQHandler ; 16+16: 16-bit Counter-Timer 0
 DCD TIMER16_1_IRQHandler ; 16+17: 16-bit Counter-Timer 1
 DCD TIMER32_0_IRQHandler ; 16+18: 32-bit Counter-Timer 0
 DCD TIMER32_1_IRQHandler ; 16+19: 32-bit Counter-Timer 1
 DCD SSP0_IRQHandler ; 16+20: SSP0
 DCD UART_IRQHandler ; 16+21: UART
 DCD USB_IRQHandler ; 16+22: USB IRQ
 DCD USB_FIQHandler ; 16+24: USB FIQ
 DCD ADC_IRQHandler ; 16+24: A/D Converter
 DCD WDT_IRQHandler ; 16+25: Watchdog Timer
 DCD BOD_IRQHandler ; 16+26: Brown Out Detect
 DCD FMC_IRQHandler ; 16+27: IP2111 Flash Memory Controller
 DCD PIOINT3_IRQHandler ; 16+28: PIO INT3
 DCD PIOINT2_IRQHandler ; 16+29: PIO INT2
 DCD PIOINT1_IRQHandler ; 16+30: PIO INT1
 DCD PIOINT0_IRQHandler ; 16+31: PIO INT0

总结:NVIC的函数使用,就是这么简单,KEIL已经给我们写好,我们直接使用即可!

NVIC相关文件名称

 


关注我的微信公众号,可以第一时间看到我的文章

微信公众号科技老顽童