基于ARM开发板,外部中断IQR实现LED灯的点亮
时间: 2024-03-28 07:37:43 浏览: 40
好的,下面是基于ARM开发板,外部中断IRQ实现LED灯点亮的代码:
```
#include "stm32f10x.h"
void EXTI0_IRQHandler(void) //定义中断服务函数
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
GPIO_SetBits(GPIOA, GPIO_Pin_0); //LED点亮
EXTI_ClearITPendingBit(EXTI_Line0); //清除中断标志位
}
}
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //使能AFIO时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //LED对应的引脚为GPIOA的引脚0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //输出速率为50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA的引脚0
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0); //配置外部中断为GPIOA的引脚0
EXTI_InitStructure.EXTI_Line = EXTI_Line0; //选择外部中断0
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; //选择外部中断0
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; //子优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断
NVIC_Init(&NVIC_InitStructure); //初始化外部中断
while(1);
return 0;
}
```
当外部中断0触发时,会进入中断服务函数`EXTI0_IRQHandler`,然后在函数中点亮LED灯,最后清除中断标志位。在`main`函数中,初始化GPIOA的引脚0为输出模式,将外部中断0配置为GPIOA的引脚0,并设置为上升沿触发。最后进入死循环,等待外部中断的触发。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)