移植开源MultiButton来测试按键的几种操作状态。
一、硬件部分
开发板上的按键部分电路图
KEY1和KEY2按键使用了PB0和PB1端口。
二、MultiButton
MultiButton 是一个小巧简单易用的事件驱动型按键驱动模块,可无限量扩展按键,按键事件的回调异步处理方式可以简化你的程序结构,去除冗余的按键处理硬编码,让你的按键业务逻辑更清晰。
下载地址:
https://github.com/0x1abin/Mu...
按照例程移植程序,即可实现按键的操作。
三、程序部分
3.1、key.c
#include "main.h"
uint8_t button_tick_cnt=0;
enum Button_IDs
{
btn1_id,
btn2_id,
};
struct Button btn1;
struct Button btn2;
uint8_t read_button_gpio(uint8_t button_id)
{
switch(button_id)
{
case btn1_id:
return GPIO_ReadInputDataBit(KEY1_GPIO_PORT, KEY1_PIN);
case btn2_id:
return GPIO_ReadInputDataBit(KEY2_GPIO_PORT, KEY2_PIN);
default:
return 0;
}
}
//BTN1
void BTN1_PRESS_DOWN_Handler(void* btn)
{
printf("KEY1 DOWN! \r\n");
}
void BTN1_PRESS_UP_Handler(void* btn)
{
printf("KEY1 UP! \r\n");
}
void BTN1_PRESS_REPEAT_Handler(void* btn)
{
printf("KEY1 PRESS_REPEAT! \r\n");
}
void BTN1_SINGLE_Click_Handler(void* btn)
{
printf("KEY1 SINGLE_ClICK! \r\n");
}
//void BTN2_DOUBLE_Click_Handler(void* btn)
//{
// printf("KEY2 DOUBLE_ClICK! \r\n");
//}
void BTN1_LONG_PRESS_START_Handler(void* btn)
{
printf("KEY1 LONG_PRESS_START! \r\n");
}
void BTN1_LONG_PRESS_HOLD_Handler(void* btn)
{
printf("KEY1 LONG_PRESS_HOLD! \r\n");
}
//BTN2
void BTN2_PRESS_DOWN_Handler(void* btn)
{
printf("KEY2 DOWN! \r\n");
}
void BTN2_PRESS_UP_Handler(void* btn)
{
printf("KEY2 UP! \r\n");
}
void BTN2_PRESS_REPEAT_Handler(void* btn)
{
printf("KEY2 PRESS_REPEAT! \r\n");
}
void BTN2_SINGLE_Click_Handler(void* btn)
{
printf("KEY2 SINGLE_ClICK! \r\n");
}
//void BTN2_DOUBLE_Click_Handler(void* btn)
//{
// printf("KEY2 DOUBLE_ClICK! \r\n");
//}
void BTN2_LONG_PRESS_START_Handler(void* btn)
{
printf("KEY2 LONG_PRESS_START! \r\n");
}
void BTN2_LONG_PRESS_HOLD_Handler(void* btn)
{
printf("KEY2 LONG_PRESS_HOLD! \r\n");
}
void button_tick_hdl(void)
{
button_tick_cnt++;
if(button_tick_cnt>=5)
{
button_tick_cnt=0;
button_ticks();
}
}
void init_key(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
KEY1_GPIO_CLK_ENABLE();
KEY2_GPIO_CLK_ENABLE();
GPIO_StructInit(&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = KEY1_PIN;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(KEY1_GPIO_PORT, &GPIO_InitStruct);
GPIO_StructInit(&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = KEY2_PIN;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(KEY2_GPIO_PORT, &GPIO_InitStruct);
//BTN1
button_init(&btn1, read_button_gpio, 0, btn1_id);
button_attach(&btn1, PRESS_DOWN, BTN1_PRESS_DOWN_Handler);
button_attach(&btn1, PRESS_UP, BTN1_PRESS_UP_Handler);
button_attach(&btn1, PRESS_REPEAT, BTN1_PRESS_REPEAT_Handler);
// button_attach(&btn1, SINGLE_CLICK, BTN1_SINGLE_Click_Handler);
// button_attach(&btn1, DOUBLE_CLICK, BTN1_DOUBLE_Click_Handler);
button_attach(&btn1, LONG_PRESS_START, BTN1_LONG_PRESS_START_Handler);
button_attach(&btn1, LONG_PRESS_HOLD, BTN1_LONG_PRESS_HOLD_Handler);
button_start(&btn1);
//BTN2
button_init(&btn2, read_button_gpio, 0, btn2_id);
button_attach(&btn2, PRESS_DOWN, BTN2_PRESS_DOWN_Handler);
button_attach(&btn2, PRESS_UP, BTN2_PRESS_UP_Handler);
button_attach(&btn2, PRESS_REPEAT, BTN2_PRESS_REPEAT_Handler);
// button_attach(&btn2, SINGLE_CLICK, BTN2_SINGLE_Click_Handler);
// button_attach(&btn2, DOUBLE_CLICK, BTN2_DOUBLE_Click_Handler);
button_attach(&btn2, LONG_PRESS_START, BTN2_LONG_PRESS_START_Handler);
button_attach(&btn2, LONG_PRESS_HOLD, BTN2_LONG_PRESS_HOLD_Handler);
button_start(&btn2);
}
3.2、key.h
#ifndef __KEY_H
#define __KEY_H
#define KEY1_GPIO_CLK RCC_AHBPeriph_GPIOB
#define KEY1_GPIO_PORT GPIOB
#define KEY1_PIN GPIO_Pin_0
#define KEY2_GPIO_CLK RCC_AHBPeriph_GPIOB
#define KEY2_GPIO_PORT GPIOB
#define KEY2_PIN GPIO_Pin_1
#define KEY1_GPIO_CLK_ENABLE() do{ RCC_AHBPeriphClockCmd(KEY1_GPIO_CLK, ENABLE); }while(0)
#define KEY1_GPIO_CLK_DISABLE() do{ RCC_AHBPeriphClockCmd(KEY1_GPIO_CLK, DISABLE); }while(0)
#define KEY2_GPIO_CLK_ENABLE() do{ RCC_AHBPeriphClockCmd(KEY2_GPIO_CLK, ENABLE); }while(0)
#define KEY2_GPIO_CLK_DISABLE() do{ RCC_AHBPeriphClockCmd(KEY2_GPIO_CLK, DISABLE); }while(0)
#define KEY_UP_PRES 1
#define KEY_DOWN_PRES 2
#define KEY_LEFT_PRES 3
#define KEY_RIGHT_PRES 4
#define KEY_SEL_PRES 5
#define KEY_B2_PRES 6
void init_key(void);
void button_tick_hdl(void);
#endif
3.3、main.c
#include "main.h"
int main(void)
{
SysTick_Init();
init_usart(115200);
init_led();
init_key();
printf("mm32F5265_ob_printf_test!\r\n");
while (1)
{
}
}
3.4、调用计时函数
在systick中断函数中调用计时函数
void SysTick_Handler(void)
{
TimingDelay_Decrement();
button_tick_hdl();
}
四、程序运行
下载程序后,操作按键,串口输出内容
4.1、操作KEY1按键,串口输出
4.2、操作KEY2按键,串口输出