#define configInputPullUp(port, pin, GPIO_InitStruct) { \
\
(GPIO_InitStruct)->Pin = pin ; \
(GPIO_InitStruct)->Mode = GPIO_MODE_INPUT ; \
(GPIO_InitStruct)->Pull = GPIO_PULLUP ; \
(GPIO_InitStruct)->Speed = GPIO_SPEED_FREQ_LOW; \
HAL_GPIO_Init(port, GPIO_InitStruct) ; \
}
#define configOutputLow(port, pin, GPIO_InitStruct) { \
(GPIO_InitStruct)->Pin = pin ; \
(GPIO_InitStruct)->Mode = GPIO_MODE_OUTPUT_PP ; \
(GPIO_InitStruct)->Pull = GPIO_NOPULL ; \
(GPIO_InitStruct)->Speed = GPIO_SPEED_FREQ_LOW; \
HAL_GPIO_Init(port, GPIO_InitStruct) ; \
HAL_GPIO_WritePin(port, pin, GPIO_PIN_RESET); \
}
u8 Key_Value;
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_TIM2_Init();
MX_TIM3_Init();
while (1)
{
while(App_LEAVE != currState->loop());
if(currState == &colScanningPressed)
{
printf("KEY:%d \r\n",Key_Value);
}
currState = currState == &rowScanning ? &colScanning : currState == &colScanning ? &colScanningPressed : &rowScanning;
currState->enter();
}
}
或者main中也可以用这个
currState=&rowScanning;
currState->enter();
while(App_LEAVE != currState->loop());
currState= &colScanning;
currState->enter();
currState->loop();
currState= &colScanningPressed;
currState->enter();
while(App_LEAVE != currState->loop());
printf("KEY:%d \r\n",Key_Value);
#include "Key.h"
extern u8 Key_Value;
APP_ScanningState rowScanning = {rowScanningEnter, rowScanningLoop };
APP_ScanningState colScanning = {colScanningEnter, colScanningLoop };
APP_ScanningState colScanningPressed = {colScanningPressedEnter, colScanningPressedLoop };
APP_ScanningState *currState = &rowScanning;
void rowScanningEnter()
{
GPIO_InitTypeDef GPIO_InitStruct;
configInputPullUp(ROW3_GPIO_Port,ROW3_Pin,&GPIO_InitStruct);
configInputPullUp(ROW2_GPIO_Port,ROW2_Pin,&GPIO_InitStruct);
configInputPullUp(ROW1_GPIO_Port,ROW1_Pin,&GPIO_InitStruct);
configInputPullUp(ROW0_GPIO_Port,ROW0_Pin,&GPIO_InitStruct);
configOutputLow(COL3_GPIO_Port,COL3_Pin,&GPIO_InitStruct);
configOutputLow(COL2_GPIO_Port,COL2_Pin,&GPIO_InitStruct);
configOutputLow(COL1_GPIO_Port,COL1_Pin,&GPIO_InitStruct);
configOutputLow(COL0_GPIO_Port,COL0_Pin,&GPIO_InitStruct);
}
GPIO_PinState checkPressedLow(GPIO_TypeDef *port, uint16_t pin)
{
if(GPIO_PIN_RESET == HAL_GPIO_ReadPin(port, pin))
{
HAL_Delay(DEBOUNCE_DELAY);
if(GPIO_PIN_RESET == HAL_GPIO_ReadPin(port, pin))
return HAL_GPIO_ReadPin(port, pin);
}
if(GPIO_PIN_SET == HAL_GPIO_ReadPin(port, pin))
{
HAL_Delay(DEBOUNCE_DELAY);
if(GPIO_PIN_SET == HAL_GPIO_ReadPin(port, pin))
return HAL_GPIO_ReadPin(port, pin);
}
return GPIO_PIN_SET;
}
uint8_t rowScanningLoop()
{
if(GPIO_PIN_RESET == checkPressedLow(ROW0_GPIO_Port,ROW0_Pin))
{
Key_Value = 0;
return App_LEAVE;
}
if(GPIO_PIN_RESET == checkPressedLow(ROW1_GPIO_Port,ROW1_Pin))
{
Key_Value = 1<<2;
return App_LEAVE;
}
if(GPIO_PIN_RESET == checkPressedLow(ROW2_GPIO_Port,ROW2_Pin))
{
Key_Value = 2<<2;
return App_LEAVE;
}
if(GPIO_PIN_RESET == checkPressedLow(ROW3_GPIO_Port,ROW3_Pin))
{
Key_Value = 3<<2;
return App_LEAVE;
}
return App_STAY;
}
void colScanningEnter()
{
GPIO_InitTypeDef GPIO_InitStruct;
configOutputLow(ROW3_GPIO_Port,ROW3_Pin,&GPIO_InitStruct);
configOutputLow(ROW2_GPIO_Port,ROW2_Pin,&GPIO_InitStruct);
configOutputLow(ROW1_GPIO_Port,ROW1_Pin,&GPIO_InitStruct);
configOutputLow(ROW0_GPIO_Port,ROW0_Pin,&GPIO_InitStruct);
configInputPullUp(COL3_GPIO_Port,COL3_Pin,&GPIO_InitStruct);
configInputPullUp(COL2_GPIO_Port,COL2_Pin,&GPIO_InitStruct);
configInputPullUp(COL1_GPIO_Port,COL1_Pin,&GPIO_InitStruct);
configInputPullUp(COL0_GPIO_Port,COL0_Pin,&GPIO_InitStruct);
}
uint8_t colScanningLoop()
{
if(GPIO_PIN_RESET == checkPressedLow(COL0_GPIO_Port,COL0_Pin))
{
Key_Value |= 0;
return App_LEAVE;
}
if(GPIO_PIN_RESET == checkPressedLow(COL1_GPIO_Port,COL1_Pin))
{
Key_Value |= 1;
return App_LEAVE;
}
if(GPIO_PIN_RESET == checkPressedLow(COL2_GPIO_Port,COL2_Pin))
{
Key_Value |= 2;
return App_LEAVE;
}
if(GPIO_PIN_RESET == checkPressedLow(COL3_GPIO_Port,COL3_Pin))
{
Key_Value |= 3;
return App_LEAVE;
}
return App_STAY;
}
void colScanningPressedEnter()
{
colScanningEnter();
}
uint8_t colScanningPressedLoop()
{
int col = 3 & Key_Value;
if(0 == col)
{
if(GPIO_PIN_SET == checkPressedLow(COL0_GPIO_Port, COL0_Pin))
{
return App_LEAVE;
}
}
else if(1 == col)
{
if(GPIO_PIN_SET == checkPressedLow(COL1_GPIO_Port, COL1_Pin))
{
return App_LEAVE;
}
}
else if(2 == col)
{
if(GPIO_PIN_SET == checkPressedLow(COL2_GPIO_Port, COL2_Pin))
{
return App_LEAVE;
}
}
else
{
if(GPIO_PIN_SET == checkPressedLow(COL3_GPIO_Port, COL3_Pin))
{
return App_LEAVE;
}
}
return App_STAY;
}
#ifndef __KEY_H__
#define __KEY_H__
#include "main.h"
#include "gpio.h"
typedef struct{
void (*enter)();
u8 (*loop)();
} APP_ScanningState;
#define App_STAY 0
#define App_LEAVE 1
#define DEBOUNCE_DELAY 10
void rowScanningEnter(void);
uint8_t rowScanningLoop(void);
void colScanningEnter(void);
uint8_t colScanningLoop(void);
void colScanningPressedEnter(void);
uint8_t colScanningPressedLoop(void);
extern APP_ScanningState rowScanning ;
extern APP_ScanningState colScanning;
extern APP_ScanningState colScanningPressed;
extern APP_ScanningState *currState;
#endif