F103矩阵键盘并串口打印
时间: 2023-06-19 19:10:34 浏览: 40
您好,以下是F103矩阵键盘并串口打印的代码示例:
```c
#include "stm32f10x.h"
#define ROW_PORT GPIOA
#define ROW1_PIN GPIO_Pin_0
#define ROW2_PIN GPIO_Pin_1
#define ROW3_PIN GPIO_Pin_2
#define ROW4_PIN GPIO_Pin_3
#define COL_PORT GPIOB
#define COL1_PIN GPIO_Pin_0
#define COL2_PIN GPIO_Pin_1
#define COL3_PIN GPIO_Pin_2
#define COL4_PIN GPIO_Pin_3
#define UART_PORT GPIOA
#define UART_TX_PIN GPIO_Pin_9
#define UART_RX_PIN GPIO_Pin_10
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 打开 GPIOA 和 GPIOB 的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
// 配置行 GPIO 为上拉输入模式
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = ROW1_PIN | ROW2_PIN | ROW3_PIN | ROW4_PIN;
GPIO_Init(ROW_PORT, &GPIO_InitStructure);
// 配置列 GPIO 为推挽输出模式
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = COL1_PIN | COL2_PIN | COL3_PIN | COL4_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(COL_PORT, &GPIO_InitStructure);
// 配置 USART1 的 GPIO 为复用推挽输出模式
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = UART_TX_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(UART_PORT, &GPIO_InitStructure);
// 配置 USART1 的 GPIO 为上拉输入模式
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = UART_RX_PIN;
GPIO_Init(UART_PORT, &GPIO_InitStructure);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
// 打开 USART1 的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
// 配置 USART1 的参数
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_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
// 打开 USART1
USART_Cmd(USART1, ENABLE);
}
void delay_ms(uint32_t ms)
{
while (ms--)
{
uint32_t i = 16000;
while (i--)
__NOP();
}
}
uint8_t get_key(void)
{
uint8_t row_pin[4] = {ROW1_PIN, ROW2_PIN, ROW3_PIN, ROW4_PIN};
uint8_t col_pin[4] = {COL1_PIN, COL2_PIN, COL3_PIN, COL4_PIN};
uint8_t key_map[4][4] = {{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}};
uint8_t row, col;
// 判断哪一行被按下
for (row = 0; row < 4; row++)
{
GPIO_ResetBits(ROW_PORT, row_pin[row]);
for (col = 0; col < 4; col++)
{
if (!GPIO_ReadInputDataBit(ROW_PORT, row_pin[row]))
{
GPIO_SetBits(ROW_PORT, row_pin[row]);
return key_map[row][col];
}
GPIO_SetBits(COL_PORT, col_pin[col]);
}
GPIO_SetBits(ROW_PORT, row_pin[row]);
}
return 0;
}
void USART_SendString(USART_TypeDef *USARTx, char *str)
{
while (*str)
{
USART_SendData(USARTx, *str++);
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET)
;
}
}
int main(void)
{
GPIO_Configuration();
USART_Configuration();
while (1)
{
uint8_t key = get_key();
if (key)
{
char str[2] = {key, 0};
USART_SendString(USART1, str);
}
delay_ms(50);
}
}
```
代码中用到了按键扫描函数 `get_key()`,该函数会返回当前按下的按键字符,如果没有按键按下则返回 0。同时,每隔一段时间会检查一次是否有按键按下,以免频繁发送数据导致串口阻塞。
您需要将代码中的引脚定义和波特率等参数适配到您的硬件平台,然后将代码编译烧录到您的 F103 开发板中即可。