實驗原理:
按鍵的一端與STM32的GPIO(PB9)相連,且PB9外接一個1k大小的限流上接電阻。
初始化時把PB9設定成輸入模式,當按鍵彈起時,PB9由於上拉電阻的作用呈高電平(3.3V);
當按鍵按下時,PB9直接被按鍵短接到GND,呈低電平,因此PB9的電平變化產生下降
沿,從而進入中斷函式,進入中斷回撥。
實驗現象:
按鍵被按下一次,切換一個LED的狀態。
原始碼下載連結:
連結:http://pan.baidu.com/s/1hrCzb2s 密碼:21yp
核心程式碼:
int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); /* USER CODE BEGIN 2 */ LED_RED_ON; /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ }
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { static int led_work_status = 0; HAL_Delay(10); if(ARM_KEY_STATE == KEY_DOWN){ led_work_status += 1; if(led_work_status > 2)led_work_status = 0; } switch(led_work_status){ case 0: LED_RED_ON; LED_GREEN_OFF; LED_BLUE_OFF; break; case 1: LED_RED_OFF; LED_GREEN_ON; LED_BLUE_OFF; break; case 2: LED_RED_OFF; LED_GREEN_OFF; LED_BLUE_ON; break; default: break; } __HAL_GPIO_EXTI_CLEAR_FLAG(GPIO_PIN_9); }
iCore4連結: