本文介紹了如何使用按鍵控制 MCU 引腳的輸出電平。
原理
由原理圖可知
板載用戶按鍵 K1 和 K2 分別與主控的 PB0 和 PB1 相連。
代碼
#define _MAIN_C_#include "platform.h"
#include "gpio_key_input.h"
#include "main.h"int main(void)
{PLATFORM_Init();GPIO_KEY_Input_Sample();while (1){}
}
函數 GPIO_KEY_Input_Sample()
void GPIO_KEY_Input_Sample(void)
{static uint8_t KeyState[2] ={0, 0,};static uint8_t KeyCount[2] ={0, 0,};printf("\r\nTest %s", __FUNCTION__);GPIO_Configure();printf("\r\nPress KEY1 or KEY2...");while (1){KEY_FSM_Handler(&KeyState[0], &KeyCount[0], GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0), Bit_RESET, "KEY1");KEY_FSM_Handler(&KeyState[1], &KeyCount[1], GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1), Bit_RESET, "KEY2");PLATFORM_LED_Enable(LED1, (FunctionalState)GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0));PLATFORM_LED_Enable(LED2, (FunctionalState)GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1));PLATFORM_DelayMS(10);}
}
效果
按鍵控制 LED
總結
本文展示了板載按鍵控制 LED 的項目實現。