STM32学习笔记--按钮代码封装(2026.3.12)

0 阅读2分钟

1、初始化串口

void App_USART1_Init(void)
{
	//1.初始化 IO 引脚 PA9 TX PA10 RX
	// 使能 GPIOA 时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

	GPIO_InitTypeDef GPIO_InitStruct;
	// 配置 PA9 为复用推挽输出 (TX)
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_Init(GPIOA,&GPIO_InitStruct);

	// 配置 PA10 为上拉输入 (RX)
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
	// 初始化 PA10 引脚
	GPIO_Init(GPIOA,&GPIO_InitStruct);

	//2.初始化 USART1
	// 使能 USART1 时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);

	USART_InitTypeDef USART_InitStruct;
	// 设置波特率为 115200
	USART_InitStruct.USART_BaudRate = 115200;
	// 设置收发模式
	USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	// 设置数据位长度为 8 位
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;
	// 设置无奇偶校验
	USART_InitStruct.USART_Parity = USART_Parity_No;
	// 设置停止位为 1 位
	USART_InitStruct.USART_StopBits = USART_StopBits_1;
	USART_Init(USART1, &USART_InitStruct);

	// 使能 USART1
	USART_Cmd(USART1,ENABLE);

}

2、按钮初始化

3、实现效果

#include "stm32f10x.h"
#include "usart.h"
#include "button.h"

Button_TypeDef button1;
uint32_t cnt = 0;

void App_Button_Init(void);
void App_USART1_Init(void);
void button_clicked_cb(uint8_t clicks);
void button_long_pressed_cb(uint8_t ticks);

int main(void)
{
	// 初始化 USART1 串口
	App_USART1_Init();
	// 通过 USART1 发送字符串
//	My_USART_SendString(USART1,"Hellow world .\r\n");
	App_Button_Init();
	
	// 主循环,保持程序运行
	while(1)
	{
		My_Button_Proc(&button1);
	}
}

void App_USART1_Init(void)
{
	//1.初始化 IO 引脚 PA9 TX PA10 RX
	// 使能 GPIOA 时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

	GPIO_InitTypeDef GPIO_InitStruct;
	// 配置 PA9 为复用推挽输出 (TX)
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_Init(GPIOA,&GPIO_InitStruct);

	// 配置 PA10 为上拉输入 (RX)
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
	// 初始化 PA10 引脚
	GPIO_Init(GPIOA,&GPIO_InitStruct);

	//2.初始化 USART1
	// 使能 USART1 时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);

	USART_InitTypeDef USART_InitStruct;
	// 设置波特率为 115200
	USART_InitStruct.USART_BaudRate = 115200;
	// 设置收发模式
	USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	// 设置数据位长度为 8 位
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;
	// 设置无奇偶校验
	USART_InitStruct.USART_Parity = USART_Parity_No;
	// 设置停止位为 1 位
	USART_InitStruct.USART_StopBits = USART_StopBits_1;
	USART_Init(USART1, &USART_InitStruct);

	// 使能 USART1
	USART_Cmd(USART1,ENABLE);

}

void App_Button_Init(void)
{
	Button_InitTypeDef Button_InitStruct;

	Button_InitStruct.GPIOx = GPIOA;
	Button_InitStruct.GPIO_Pin = GPIO_Pin_0;
	Button_InitStruct.ClickInterval = 0;
	Button_InitStruct.LongPressTime = 0;
	Button_InitStruct.LongPressTickInterval = 0;
	Button_InitStruct.button_clicked_cb = button_clicked_cb;
	Button_InitStruct.button_long_pressed_cb = button_long_pressed_cb;
	Button_InitStruct.button_pressed_cb = 0;
	Button_InitStruct.button_released_cb = 0;
	My_Button_Init(&button1,&Button_InitStruct);

}

void button_clicked_cb(uint8_t clicks)
{
	if (clicks == 1)
	{
		cnt++;
		My_USART_Printf(USART1,"%d",cnt);
	}
	else if (clicks == 2)
	{
		cnt = 0;
		My_USART_Printf(USART1,"%d",cnt);
	}
}

void button_long_pressed_cb(uint8_t ticks)
{
	cnt++;
	My_USART_Printf(USART1,"%d",cnt);
}