stm32 中断嵌套实验源码
时间: 2023-09-23 14:12:09 浏览: 75
下面是基于STM32F4系列芯片,使用Keil MDK编写的中断嵌套实验源码,希望对你有所帮助:
```
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "misc.h"
void Delay(__IO uint32_t nCount);
int main(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* 配置外部中断 */
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* 配置定时器中断 */
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 500;
TIM_TimeBaseStructure.TIM_Prescaler = 42000;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
while(1)
{
GPIO_SetBits(GPIOD, GPIO_Pin_12);
Delay(1000000);
GPIO_ResetBits(GPIOD, GPIO_Pin_12);
Delay(1000000);
GPIO_SetBits(GPIOD, GPIO_Pin_13);
Delay(1000000);
GPIO_ResetBits(GPIOD, GPIO_Pin_13);
Delay(1000000);
GPIO_SetBits(GPIOD, GPIO_Pin_14);
Delay(1000000);
GPIO_ResetBits(GPIOD, GPIO_Pin_14);
Delay(1000000);
GPIO_SetBits(GPIOD, GPIO_Pin_15);
Delay(1000000);
GPIO_ResetBits(GPIOD, GPIO_Pin_15);
Delay(1000000);
}
}
void Delay(__IO uint32_t nCount)
{
while(nCount--);
}
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line0);
GPIO_ToggleBits(GPIOD, GPIO_Pin_12);
}
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
GPIO_ToggleBits(GPIOD, GPIO_Pin_13);
}
}
```
在这个例子中,我们使用了一个外部中断和一个定时器中断来演示中断嵌套的实现。在 `main` 函数中,我们首先初始化了GPIO口(用于闪烁LED),然后配置了外部中断和定时器中断的相关参数。在 `EXTI0_IRQHandler` 中,我们实现了外部中断的处理函数,当外部中断触发时,我们会将LED12的状态进行翻转。在 `TIM2_IRQHandler` 中,我们实现了定时器中断的处理函数,当定时器中断触发时,我们会将LED13的状态进行翻转。这个例子中,我们还使用了一个简单的延时函数 `Delay` 来控制LED的闪烁速度。
需要注意的是,中断嵌套的实现需要根据具体的应用场景进行调整,本例中只是一个简单的实现。此外,在实际应用中,还需要考虑中断优先级和中断处理时间等因素,以确保中断嵌套的可靠性和稳定性。
阅读全文