本帖最后由 无痕丶 于 2018-6-14 14:34 编辑
这是一个关于外部中断的实验,实验会出现很奇怪的现象,附件里面有小视频。
我按键还没按下,手靠近按键,灯就自己亮了。
这是外部中断配置的代码。
#include "EXTI.h"
#include "LED.h"
#include "delay.h"
#include "stdio.h"
void EXTI_2_4_init(void)
{
//配置PE4(KEY0)和PE2 (KEY2)为浮空输入
RCC->APB2ENR |= (0x1<<6);
//PE2
GPIOE->CRL &= ~(0xf<<4*2);
GPIOE->CRL |= (0x4<<4*2);
//PE4
GPIOE->CRL &= ~(0xf<<4*4);
GPIOE->CRL |= (0x4<<4*4);
//开启复用功能时钟
RCC->APB2ENR |= (0x1<<0);
//外部中断线2
AFIO->EXTICR[0] &= ~(0xf<<4*2);
AFIO->EXTICR[0] |= (0x4<<4*2);
//外部中断线4
AFIO->EXTICR[1] &= ~(0xf<<4*0);
AFIO->EXTICR[1] |= (0x4<<4*0);
//外部中断线2位下降沿触发
EXTI->FTSR |= (0x1<<2);
EXTI->RTSR &= ~(0x1<<2); //关闭上升沿触发
//外部中断线4位下降沿触发
EXTI->FTSR |= (0x1<<4);
EXTI->RTSR &= ~(0x1<<4); //关闭上升沿触发
//开启外部中断触发
EXTI->IMR |= (0x1<<2);
EXTI->IMR |= (0x1<<4);
//配置NVIC
NVIC_SetPriority(EXTI2_IRQn,NVIC_EncodePriority(NVIC_GetPriorityGrouping(),2,4));
NVIC_SetPriority(EXTI4_IRQn,NVIC_EncodePriority(NVIC_GetPriorityGrouping(),2,3));
NVIC_EnableIRQ(EXTI2_IRQn);
NVIC_EnableIRQ(EXTI4_IRQn);
}
void EXTI2_IRQHandler()
{
delay_init(72);
delay_ms(10);
if( EXTI->PR & (0x1<<2))
{
EXTI->PR |= (0x1<<2);
LED1=!LED1;
EXTI->PR |= (0x1<<2);
}
}
void EXTI4_IRQHandler()
{
delay_init(72);
delay_ms(10);
if( EXTI->PR & (0x1<<4))
{
EXTI->PR |= (0x1<<4);
LED0=!LED0;
EXTI->PR |= (0x1<<4);
}
}
图片是按键连接图,用按键测试外部中断。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>