PA11 as a Interrupt Source
Modify the code as following snippet, we will set PA11 as interrupt invoke.1
2
3
4
5GPIO_InitStruct.Pin = GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
Handle the Interrupt
We must modify the code in interrupt handler.1
2
3
4
5
6
7
8void EXTI15_10_IRQHandler(void){
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_11)!=0){
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_11);
}
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_12)!=0){
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_12);
}
}
We need to recognize the interrupt source in the handler.
Callback
Hence, we could separate the GPIO_Pin in the callback function.1
2
3
4
5
6
7
8
9
10
11void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
switch (GPIO_Pin){
case GPIO_PIN_11:
printf("Pressed\r\n");
mode=!mode;
break;
case GPIO_PIN_12:
counter++;
break;
}
}
Enter the Sleep Mode
First, we must diable system tick interrupt. And then, the program must sleep again after the interrupt is handled.1
2
3
4
5
6
7
8HAL_SuspendTick();
HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
HAL_PWR_EnableSleepOnExit();
while (1)
{
printf("CPU running!\r\n");
HAL_Delay(100);
}
Result
As you can see, the CPU running! in a dead loop only execute once. And other function run well.