高级点的来了,Key作为外部中断,用中断方式检测Key是否按下。
首先配置下NVIC和EXTI,然后当检测到中断时点亮LED。
int main(void) { NVIC_InitPara NVIC_InitStructure; EXTI_InitPara EXTI_InitStructure; /* Enable the GPIO_LED Clock */ RCC_AHBPeriphClock_Enable( RCC_AHBPERIPH_GPIOA , ENABLE ); //配置GPIO { GPIO_InitPara GPIO_InitStructure; /* Configure the GPIO_LED pin */ GPIO_InitStructure.GPIO_Pin = GPIO_PIN_1; GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ; GPIO_Init( GPIOA , &GPIO_InitStructure ); GPIO_InitStructure.GPIO_Pin = GPIO_PIN_0; GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IN; GPIO_Init( GPIOA , &GPIO_InitStructure ); } //配置NVIC { //使能NVIC时钟 NVIC_PRIGroup_Enable( NVIC_PRIGROUP_1 ); NVIC_InitStructure.NVIC_IRQ = EXTI0_1_IRQn ; NVIC_InitStructure.NVIC_IRQEnable = ENABLE; NVIC_InitStructure.NVIC_IRQPreemptPriority = 0; NVIC_InitStructure.NVIC_IRQSubPriority = 0; NVIC_Init(&NVIC_InitStructure); } //配置外部中断 { //使能EXTI时钟 RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_CFG, ENABLE); //将EXTI0连接到PA0(Key引脚) SYSCFG_EXTILine_Config(EXTI_SOURCE_GPIOA,EXTI_SOURCE_PIN0); EXTI_InitStructure.EXTI_LINE = EXTI_LINE0; EXTI_InitStructure.EXTI_LINEEnable =ENABLE; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_Init(&EXTI_InitStructure); } while(1); }中断配置完毕。添加中断响应函数,使得当外部中断进来时候就点亮LED灯。
void EXTI0_1_IRQHandler(void) { if(EXTI_GetINTBitState(EXTI_LINE0) != RESET) { EXTI_ClearINTBitState(EXTI_LINE0); GPIO_SetBits(GPIOA,GPIO_PIN_1); } }这样,我们又点了一次灯。
下一个实验是看门狗。MCU复位后配置看门狗,延时一段时间之后让LED点亮。 如果不喂狗,当看门狗时间到达之后会复位MCU。于是我们就会看到LED在以一定的频率闪烁。