我的板子使用
STM32F072C8, USB
电路如图,因为USB只用来充电,所以我想直接用PB1来检测USB是否连接电脑或者充电器,配置好IO口中断线等,可是程序无法中断,读取PB1的状态位也正常,在USB连接时为1,USB无连接时为0。配置代码如下:
void USB_CHECK_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EX
ti_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* enable the GPIO Clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
/* Configure GPIO */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* enable SYSCFG Clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* Connect EXTI Line to GPIO Pin */
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource1);
/* Configure EXTI Line */
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set EXTI Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI0_1_IRQHandler(void)
{
//清除中断标志位等其它操作......
if (EXTI_GetITStatus(EXTI_Line1)!=RESET)
{
//Deal
EXTI_ClearITPendingBit(EXTI_Line1);
}
}
一周热门 更多>