The NVIC helps the processor handle many interrupt signals quickly and in order. It decides which interrupt to handle first so the system works smoothly.
NVIC (Nested Vectored Interrupt Controller) in ARM Architecture
NVIC_EnableIRQ(IRQn_Type IRQn); NVIC_DisableIRQ(IRQn_Type IRQn); NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority); NVIC_GetPriority(IRQn_Type IRQn); NVIC_ClearPendingIRQ(IRQn_Type IRQn); NVIC_SetPendingIRQ(IRQn_Type IRQn);
IRQn_Type is the interrupt number or name for a specific interrupt source.
Priority is usually a number where lower means higher priority.
NVIC_EnableIRQ(TIM2_IRQn);
NVIC_SetPriority(USART1_IRQn, 1);NVIC_DisableIRQ(EXTI0_IRQn);
This simple program enables the interrupt for an external line (like a button). It sets the priority so the processor knows how important this interrupt is compared to others. The main loop waits for interrupts to happen.
#include "stm32f4xx.h" int main(void) { // Enable interrupt for external line 0 (e.g., a button press) NVIC_EnableIRQ(EXTI0_IRQn); // Set priority to 2 (lower number means higher priority) NVIC_SetPriority(EXTI0_IRQn, 2); // Main loop while (1) { // Wait for interrupts } return 0; }
NVIC is part of ARM Cortex-M processors and manages interrupts efficiently.
Lower priority numbers mean higher priority interrupts.
Always enable the interrupt and set its priority before expecting it to work.
NVIC controls how the processor handles multiple interrupts.
It allows setting priorities to decide which interrupt to handle first.
Use NVIC functions to enable, disable, and prioritize interrupts in ARM Cortex-M systems.
Practice
NVIC in ARM microcontrollers?Solution
Step 1: Understand NVIC's role
NVIC stands for Nested Vectored Interrupt Controller, which manages interrupts.Step 2: Identify its main function
It enables, disables, and prioritizes interrupts so the processor can respond quickly to important events.Final Answer:
To manage and prioritize multiple interrupts efficiently -> Option BQuick Check:
NVIC controls interrupts = B [OK]
- Confusing NVIC with clock or memory functions
- Thinking NVIC stores program code
- Assuming NVIC increases processor speed
Solution
Step 1: Identify function purpose
NVIC_EnableIRQ is used to enable a specific interrupt request (IRQ).Step 2: Compare other options
NVIC_DisableIRQ disables interrupts, NVIC_SetPriority sets priority, NVIC_ClearPendingIRQ clears pending interrupts.Final Answer:
NVIC_EnableIRQ(IRQn); -> Option CQuick Check:
Enable interrupt function = NVIC_EnableIRQ [OK]
- Confusing enable with disable functions
- Mixing priority setting with enabling
- Using clear pending instead of enable
NVIC_SetPriority(TIM2_IRQn, 2); NVIC_EnableIRQ(TIM2_IRQn); NVIC_SetPriority(USART1_IRQn, 1); NVIC_EnableIRQ(USART1_IRQn);Which interrupt has higher priority?
Solution
Step 1: Understand priority values
Lower numeric priority value means higher priority in NVIC.Step 2: Compare priorities set
TIM2_IRQn is set to 2, USART1_IRQn is set to 1, so USART1_IRQn has higher priority.Final Answer:
USART1_IRQn has higher priority -> Option AQuick Check:
Lower number = higher priority = USART1_IRQn [OK]
- Assuming higher number means higher priority
- Ignoring priority values and focusing on enable order
- Confusing IRQ names
NVIC_EnableIRQ(EXTI0_IRQn); NVIC_DisableIRQ(EXTI1_IRQn);What is the likely mistake?
Solution
Step 1: Analyze the code
The code enables EXTI0_IRQn but disables EXTI1_IRQn, which are different interrupts.Step 2: Identify the mistake
If the goal was to disable EXTI0_IRQn, disabling EXTI1_IRQn is incorrect.Final Answer:
They enabled EXTI0_IRQn but disabled EXTI1_IRQn instead -> Option DQuick Check:
Mismatch in IRQ numbers = mistake [OK]
- Disabling wrong interrupt number
- Believing disable function doesn't work
- Thinking priority affects disabling
SENSOR_IRQn and COMM_IRQn?Solution
Step 1: Recall NVIC priority rules
Lower numeric priority means higher priority and can preempt others.Step 2: Apply to SENSOR and COMM interrupts
To let SENSOR_IRQn preempt COMM_IRQn, SENSOR_IRQn must have a lower priority number.Final Answer:
Set SENSOR_IRQn priority lower than COMM_IRQn (e.g., SENSOR=0, COMM=1) -> Option AQuick Check:
Lower number = higher priority = SENSOR_IRQn preempts [OK]
- Assigning higher number for higher priority
- Disabling interrupts instead of prioritizing
- Setting equal priorities expecting preemption
Ready to prove it?
Pass the test and this pattern moves unknown
