nvic_irq_enable
时间: 2023-08-31 09:12:33 浏览: 215
stm32中的nvic
nvic_irq_enable is a function in the C programming language that enables interrupts for a specific interrupt request line (IRQ) in a microcontroller. The function is typically used in embedded systems programming to allow the microcontroller to respond to external events or signals in real-time.
The function takes an argument that specifies the IRQ line number to be enabled. Once the function is executed, the microcontroller's interrupt controller will be configured to allow interrupts on the specified line. This means that when an interrupt signal is received on that line, the microcontroller will pause its current task and jump to the interrupt service routine (ISR) associated with that line.
Here is an example of how nvic_irq_enable might be used in a program:
```
#include <stdio.h>
#include "stm32f4xx.h" // includes the appropriate header file for the specific microcontroller being used
void EXTI0_IRQHandler(void) // ISR for EXTI0 (external interrupt on line 0)
{
printf("External interrupt on line 0 has occurred!\n");
}
int main(void)
{
// enable clock for GPIO and EXTI peripherals
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
// configure GPIO pin as input with interrupt
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStruct);
// configure EXTI line for GPIO pin
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
EXTI_InitTypeDef EXTI_InitStruct;
EXTI_InitStruct.EXTI_Line = EXTI_Line0;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStruct);
// enable interrupt for EXTI0 line
NVIC_EnableIRQ(EXTI0_IRQn);
while(1) {
// main program loop
}
return 0;
}
```
In this example, the program configures an external interrupt on GPIO pin 0 of port A on an STM32F4 microcontroller. The nvic_irq_enable function is used to enable interrupts for EXTI line 0, which is associated with the GPIO pin. When the button attached to the pin is pressed, an interrupt signal is generated and the program jumps to the EXTI0_IRQHandler function, which prints a message to the console.
阅读全文