写一个基于stm32f103c8t6的NVIC配置
时间: 2023-09-02 18:08:47 浏览: 306
以下是基于stm32f103c8t6的NVIC配置代码:
```
#include "stm32f10x.h"
void NVIC_Configuration(void) {
NVIC_InitTypeDef NVIC_InitStructure;
// 设置中断优先级分组为2位抢占优先级和2位响应优先级
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
// 配置USART1中断
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 抢占优先级为1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // 响应优先级为1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// 配置TIM2中断
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // 抢占优先级为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 响应优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
int main(void) {
NVIC_Configuration();
while (1) {
// 程序主逻辑
}
}
void USART1_IRQHandler(void) {
// USART1中断服务程序
}
void TIM2_IRQHandler(void) {
// TIM2中断服务程序
}
```
在上述代码中,`NVIC_Configuration`函数用于配置NVIC。首先调用`NVIC_PriorityGroupConfig`函数设置中断优先级分组为2位抢占优先级和2位响应优先级。然后配置USART1中断和TIM2中断,分别设置抢占优先级、响应优先级和使能中断。最后在`main`函数中调用`NVIC_Configuration`函数,使其生效。最后,定义了USART1和TIM2的中断服务程序,可根据实际需求进行修改。
阅读全文