STM32单片机外接扩展板D2、D3接开关S2和S3,编程实现对按键进行循环加、减计数00-59。S1实现加1,S2实现减1,每次按下按键,计得当前值在串口上面显示。
时间: 2023-06-24 18:07:58 浏览: 395
首先,我们需要配置STM32单片机的GPIO口,使其能够检测到外接按键的状态变化。假设我们将S1连接到GPIOA的第0个引脚,S2连接到GPIOA的第1个引脚,D2连接到GPIOA的第2个引脚,D3连接到GPIOA的第3个引脚。
下面是初始化GPIO口的代码:
```c
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOA_CLK_ENABLE(); // 打开GPIOA时钟
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3; // 配置GPIOA的第0、1、2、3个引脚
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; // 输入模式
GPIO_InitStruct.Pull = GPIO_PULLUP; // 上拉电阻
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; // 高速模式
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // 初始化GPIOA
```
然后,我们需要在主循环中检测按键状态。如果按键被按下,则执行相应的操作,同时在串口上发送当前值。下面是主循环的代码:
```c
uint8_t count = 0; // 计数器,初值为0
while (1) {
if (!HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0)) { // 检测S1按键状态
count++; // 加1
if (count >= 60) { // 超过59则清零
count = 0;
}
printf("Current count: %02d\n", count); // 发送当前值到串口
HAL_Delay(100); // 延时去抖
}
if (!HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1)) { // 检测S2按键状态
count--; // 减1
if (count < 0) { // 小于0则变为59
count = 59;
}
printf("Current count: %02d\n", count); // 发送当前值到串口
HAL_Delay(100); // 延时去抖
}
HAL_Delay(10); // 延时等待下一次检测
}
```
这段代码会不断地检测S1和S2的状态,如果它们被按下,则执行相应的操作,同时在串口上发送当前值。这里使用了printf函数发送数据到串口,需要先通过串口初始化函数初始化串口。另外,为了避免按键抖动,需要加入适当的延时。
完整代码如下:
```c
#include "main.h"
#include "stdio.h"
UART_HandleTypeDef huart2;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
uint8_t count = 0; // 计数器,初值为0
while (1) {
if (!HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0)) { // 检测S1按键状态
count++; // 加1
if (count >= 60) { // 超过59则清零
count = 0;
}
printf("Current count: %02d\n", count); // 发送当前值到串口
HAL_Delay(100); // 延时去抖
}
if (!HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1)) { // 检测S2按键状态
count--; // 减1
if (count < 0) { // 小于0则变为59
count = 59;
}
printf("Current count: %02d\n", count); // 发送当前值到串口
HAL_Delay(100); // 延时去抖
}
HAL_Delay(10); // 延时等待下一次检测
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin : PA0 */
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void Error_Handler(void)
{
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)