首页 > 技术文章 > GD32 ------ 使用外部中断,中断函数需要延时才能读到真正电平

god-of-death 2018-07-06 11:27 原文

MCU:GD32F103RCT6

中断引脚没有外界上拉电阻

 

中断配置如下:

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource9);
    
    EXTI_InitStructure.EXTI_Line=EXTI_Line9;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;    
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);

 

中断函数如下:

void EXTI9_5_IRQHandler(void)
{
    BaseType_t xHigherPriorityTaskWoken = pdFALSE;
    
    
    if(EXTI_GetITStatus(EXTI_Line9) != RESET)
    {
        debug("%d",GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_9));
        debug("%d",GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_9));
        debug("%d",GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_9));
        if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_9) != 0)
        {
            debug("relay power on, device_power_on is 1");
            xTaskNotifyFromISR( xTaskHandleLoadDatatoServerBuf, g_flag_event[RELAY_POWER_ON], eSetBits, &xHigherPriorityTaskWoken );
            g_data_info.power_on = 1;
        }else
        {
            debug("relay power off, device_power_on is 0");
            xTaskNotifyFromISR( xTaskHandleLoadDatatoServerBuf, g_flag_event[RELAY_POWER_OFF], eSetBits, &xHigherPriorityTaskWoken );
            g_data_info.power_on = 0;
        }
        EXTI_ClearITPendingBit(EXTI_Line9);
    }
    portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
}

当检测到上升沿,在中断函数中,第一次读电平是0,之后读的几次都是1

 

推荐阅读