Stm32 外部中断EXTI使用说明

Stm32 外部中断EXTI使用说明

AMENG
2018-12-25 / 0 评论 / 1 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年08月23日,已超过1027天没有更新,若内容或图片失效,请留言反馈。

需要明确的是,PxN管脚共用外部中断线EXTIN和外部中断向量EXTIN_IRQn和中断服务程序入口EXTIN_IRQHandler,但是需要注意的是[9...5]共用EXTI9_5_IRQn和EXTI9_5_IRQHandler、[15...10]共用EXTI15_10_IRQn和EXTI15_10_IRQHandler


//按键初始化函数

[cpp]void KEY_Init(void)
{

GPIO_InitTypeDef GPIO_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOB, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD, &GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOB, &GPIO_InitStructure);
} [/cpp]

//外部中断初始化函数

[cpp]void EXTIX_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
KEY_Init();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOD, EXTI_PinSource6);

/* ÅäÖÃEXTI_Line4 */
EXTI_InitStructure.EXTI_Line = EXTI_Line6;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);

NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x06;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}[/cpp]

//外部中断服务函数

[cpp]extern TaskHandle_t Task2Task_Handler;
void EXTI9_5_IRQHandler(void)
{
BaseType_t YieldRequired;

delay_xms(30);
if(KEY0==0)
{
YieldRequired=xTaskResumeFromISR(Task2Task_Handler);
printf("恢复任务2的运行\r\n");
if(YieldRequired==pdTRUE)
{
portYIELD_FROM_ISR(YieldRequired);
}
}
EXTI_ClearITPendingBit(EXTI_Line6);//Çå³ýLINE6ÉϵÄÖжϱê־λ
}[/cpp]

//按键处理函数
//返回按键值
//mode:0支持连按1不支持连按

[cpp]u8 KEY_Scan(u8 mode)
{
static u8 key_up=1;
if(mode)key_up=1;
if(key_up&&(KEY0==0||KEY2==0))
{
delay_ms(10);
key_up=0;
if(KEY0==0)return 1;
else if(KEY2==0)return 3;
}else if(KEY0==1&&KEY2==1)key_up=1;
return 0;
}[/cpp]

0

评论 (0)

取消