Files

39 lines
679 B
C
Raw Permalink Normal View History

2025-08-02 19:53:22 +08:00
#include "key.h"
#include "delay.h"
void Key_Port_Init(void)
{
2025-08-05 16:06:46 +08:00
GPIO_InitTypeDef GPIO_InitStruct = {0};
2025-08-02 19:53:22 +08:00
2025-08-05 16:06:46 +08:00
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
2025-08-02 19:53:22 +08:00
2025-08-05 16:06:46 +08:00
/*Configure GPIO pin : PA0 */
GPIO_InitStruct.Pin = KEYB_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(KEYB_PORT, &GPIO_InitStruct);
2025-08-02 19:53:22 +08:00
}
uint8_t KeyScan(uint8_t mode)
{
static uint8_t key_up = 1;
uint8_t keyvalue=0;
if(mode) key_up = 1;
2025-08-05 16:06:46 +08:00
if( key_up && !KEYB)
2025-08-02 19:53:22 +08:00
{
delay_ms(3);//ensure the key is down
2025-08-05 16:06:46 +08:00
if(!KEYB) keyvalue = 1;
2025-08-02 19:53:22 +08:00
if(keyvalue) key_up = 0;
}
else
{
delay_ms(3);//ensure the key is up
2025-08-05 16:06:46 +08:00
if(KEYB)
2025-08-02 19:53:22 +08:00
key_up = 1;
}
return keyvalue;
}