基于stm32单片机的简易计算器4*4矩阵键盘,只进行两个小于10的数进行运算,可用的GPIO有PE7到PE14,结果显示在串口调试助手,用言编写
时间: 2024-03-26 22:36:30 浏览: 72
好的,这个问题我可以回答。以下是基于 STM32 单片机的简易计算器 4*4 矩阵键盘的代码:
```c
#include "stm32f10x.h"
#include <stdio.h>
#define ROW_NUM 4
#define COL_NUM 4
#define BUF_SIZE 16
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
char buf[BUF_SIZE];
int idx = 0;
// 矩阵键盘的按键值
char keys[ROW_NUM][COL_NUM] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'C', '0', '=', '/'}
};
// 初始化GPIO口
void GPIO_Config(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
// 初始化行
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOE, &GPIO_InitStructure);
// 初始化列
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
// 初始化串口
void USART_Config(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
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);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
// 发送数据到串口
void USART_SendString(char *str)
{
while (*str) {
USART_SendData(USART1, *str++);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
// 获取按键值
char GetKeyValue(void)
{
int i, j;
char ch = '\0';
for (i = 0; i < COL_NUM; i++) {
GPIO_ResetBits(GPIOE, GPIO_Pin_11 << i);
for (j = 0; j < ROW_NUM; j++) {
if (GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_7 << j) == RESET) {
ch = keys[j][i];
while (GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_7 << j) == RESET);
}
}
GPIO_SetBits(GPIOE, GPIO_Pin_11 << i);
}
return ch;
}
// 处理按键值
void HandleKeyValue(char ch)
{
if (ch == 'C') {
idx = 0;
buf[idx] = '\0';
} else if (ch == '=') {
int num1 = buf[0] - '0';
int num2 = buf[2] - '0';
char op = buf[1];
int result = 0;
switch (op) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}
sprintf(buf, "%d", result);
idx = 0;
} else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
buf[++idx] = ch;
buf[++idx] = ' ';
} else if (ch >= '0' && ch <= '9') {
buf[idx] = ch;
if (idx == 0 || idx == 2) {
buf[++idx] = ' ';
} else {
idx++;
}
}
}
int main(void)
{
GPIO_Config();
USART_Config();
while (1) {
char ch = GetKeyValue();
if (ch != '\0') {
HandleKeyValue(ch);
USART_SendString(buf);
}
}
}
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET) {
USART_ClearITPendingBit(USART1, USART_IT_TXE);
}
}
```
这个代码实现了基于 STM32 单片机的简易计算器 4*4 矩阵键盘,只进行两个小于10的数进行运算,可用的 GPIO 有 PE7 到 PE14,结果显示在串口调试助手。
阅读全文