请使用stm32f103写一个按键计数的程序,使用PA0引脚和PA1引脚分别进行两位数计数,pA3引脚进行计数的确定发送
时间: 2024-04-15 13:32:15 浏览: 279
好的,以下是使用STM32F103芯片、PA0和PA1引脚进行两位数计数,并通过PA3引脚发送计数结果的程序示例:
```c
#include "stm32f10x.h"
#include "stdio.h"
void USART1_Init(void);
void USART1_SendChar(char ch);
void USART1_SendString(const char* str);
int main(void)
{
// 初始化PA0和PA1引脚为输入模式
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 初始化PA3引脚为输出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 初始化计数器
uint8_t count = 0;
// 初始化串口
USART1_Init();
while (1)
{
// 检测按键1是否按下
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 0)
{
// 延时一段时间以避免抖动
for (int i = 0; i < 10000; i++);
// 再次检测按键1状态,确认按键仍然按下
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 0)
{
// 按键计数增加
count++;
if (count > 99)
{
count = 0;
}
}
}
// 检测按键2是否按下
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == 0)
{
// 延时一段时间以避免抖动
for (int i = 0; i < 10000; i++);
// 再次检测按键2状态,确认按键仍然按下
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == 0)
{
// 按键计数减少
count--;
if (count > 99)
{
count = 99;
}
}
}
// 发送计数结果到串口
char str[3];
sprintf(str, "%02d", count);
USART1_SendString(str);
// 延时一段时间
for (int i = 0; i < 100000; i++);
}
}
void USART1_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // USART1.TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void USART1_SendChar(char ch)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, ch);
}
void USART1_SendString(const char* str)
{
while (*str)
{
USART1_SendChar(*str++);
}
}
```
该程序会在PA0引脚检测到按键1按下时,计数增加;在PA1引脚检测到按键2按下时,计数减少。计数结果会通过串口发送,使用波特率为115200。计数范围为00-99,超过99会回到00。你可以通过串口监视器或其他设备接收发送的计数结果。希望对你有所帮助!
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.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)
![](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)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)