矩阵键盘通过串口3输出

USART.c文件

#include "USART3.h"
#include "stdio.h"


//重定义fputc函数 
int fputc(int ch, FILE *f)
{      
while((USART3->SR&0X40)==0);//循环发送,直到发送完毕   
    USART3->DR = (u8) ch;      
return ch;
}
void USART3_Init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
 USART_InitTypeDef USART_InitStructure;
    //开启GPIOB和复用功能的时钟
 RCC_APB1PeriphClockCmd(RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO,ENABLE);

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); 
//PB10->TX 配置为复用推挽式输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
   //PB11->RX 配置为浮空输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);


USART_InitStructure.USART_BaudRate = bound;
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(USART3, &USART_InitStructure);

    USART_Cmd(USART3,ENABLE);

USART_ClearFlag(USART3,USART_FLAG_TC);//解决发送第一个字节丢失的问题

}

矩阵键盘.c文件

#include "KEY44.h"
#include "sys.h"
#include "delay.h"


//PA0~PA3 为推挽式输出
//PA4~PA7 为上拉输入


void  KEY44_Init(void)
{
 GPIO_InitTypeDef GPIO_InitStructure;//结构体声明

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0 |GPIO_Pin_1 |GPIO_Pin_2 |GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);   
   
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4 |GPIO_Pin_5 |GPIO_Pin_6 |GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure); 
 
}
unsigned char key44_Scan(void)
{
H_1 = 0;
H_2 = 1;
H_3 = 1;
  H_4 = 1;


if(L_1 == 0)
{
delay_ms(10);
if(L_1 == 0)
{
while(!L_1);
return 1;
}
}
if(L_2 == 0)
{
delay_ms(10);
if(L_2 == 0)
{
while(!L_2);
return 2;
}
}
if(L_3 == 0)
{
delay_ms(10);
if(L_3 == 0)
{
while(!L_3);
return 3;
}
}
if(L_4 == 0)
{
delay_ms(10);
if(L_4 == 0)
{
while(!L_4);
return 4;
}
}


H_1 = 1;
H_2 = 0;
H_3 = 1;
H_4 = 1;


if(L_1 == 0)
{
delay_ms(10);
if(L_1 == 0)
{
while(!L_1);
return 5;
}
}
if(L_2 == 0)
{
delay_ms(10);
if(L_2 == 0)
{
while(!L_2);
return 6;
}
}
if(L_3 == 0)
{
delay_ms(10);
if(L_3 == 0)
{
while(!L_3);
return 7;
}
}
if(L_4 == 0)
{
delay_ms(10);
if(L_4 == 0)
{
while(!L_4);
return 8;
}
}


H_1 = 1;
H_2 = 1;
H_3 = 0;
H_4 = 1;


if(L_1 == 0)
{
delay_ms(10);
if(L_1 == 0)
{
while(!L_1);
return 9;
}
}
if(L_2 == 0)
{
delay_ms(10);
if(L_2 == 0)
{
while(!L_2);
return 10;
}
}
if(L_3 == 0)
{
delay_ms(10);
if(L_3 == 0)
{
while(!L_3);
return 11;
}
}
if(L_4 == 0)
{
delay_ms(10);
if(L_4 == 0)
{
while(!L_4);
return 12;
}
}


H_1 = 1;
H_2 = 1;
H_3 = 1;
H_4 = 0;


if(L_1 == 0)
{
delay_ms(10);
if(L_1 == 0)
{
while(!L_1);
return 13;
}
}
if(L_2 == 0)
{
delay_ms(10);
if(L_2 == 0)
{
while(!L_2);
return 14;
}
}
if(L_3 == 0)
{
delay_ms(10);
if(L_3 == 0)
{
while(!L_3);
return 15;
}
}
if(L_4 == 0)
{
delay_ms(10);
if(L_4 == 0)
{
while(!L_4);
return 16;
}
}
return 0;
}

主函数

#include "sys.h"
#include "USART3.h"
#include "KEY44.h"
#include "delay.h"
#include "string.h"
#include "stdio.h"


u8 key_num = 0;




int main()
{
   USART3_Init(115200);
delay_init();
KEY44_Init();

//键盘扫入并输出
while(1)
{
key_num = key44_Scan();
if(key_num != 0)
{
printf("KEY NUM IS %d\r\n",key_num);
}
}

}


下面是实证照片


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用F103单片机控制矩阵键盘并通过串口打印键值的示例代码: ```c #include "stm32f10x.h" #include "stdio.h" #define ROWS 4 #define COLS 4 char keymap[ROWS][COLS] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; GPIO_InitTypeDef GPIO_InitStructure; void init_keyboard(void); char get_key(void); void init_usart(void); void usart_send_char(char ch); int main() { SystemInit(); init_keyboard(); init_usart(); char key; while(1) { key = get_key(); if(key != '\0') { printf("%c\n", key); } } } void init_keyboard(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); } char get_key(void) { int row, col; char key = '\0'; for(row = 0; row < ROWS; row++) { GPIO_WriteBit(GPIOB, GPIO_Pin_0, Bit_RESET); GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_RESET); GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_RESET); GPIO_WriteBit(GPIOB, GPIO_Pin_3, Bit_RESET); GPIO_WriteBit(GPIOB, GPIO_Pin_0 + row, Bit_SET); if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET) { col = 0; key = keymap[row][col]; break; } if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == Bit_RESET) { col = 1; key = keymap[row][col]; break; } if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2) == Bit_RESET) { col = 2; key = keymap[row][col]; break; } if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == Bit_RESET) { col = 3; key = keymap[row][col]; break; } } return key; } void init_usart(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE); // Configure USART1 Tx (PA.9) as alternate function push-pull GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); // Configure USART1 Rx (PA.10) as input floating GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); // USART1 configuration 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_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); // Enable USART1 USART_Cmd(USART1, ENABLE); } void usart_send_char(char ch) { while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); USART_SendData(USART1, ch); } ``` 在该示例代码中,我们首先定义了一个4x4的键值矩阵,并通过变量`keymap`来表示键值。然后在`init_keyboard`函数中,我们初始化了4个行引脚(GPIOB0-B3)和4个列引脚(GPIOA0-A3),其中行引脚设置为输出模式,列引脚设置为输入模式。在`get_key`函数中,我们轮询每一个行引脚,将其置为高电平,然后依次读取每一个列引脚的状态,如果有列引脚被按下,则通过`keymap`数组来返回相应的键值。 在主函数中,我们不断轮询键盘,并使用串口输出键值。为了输出键值,我们还需要初始化串口,并定义一个`usart_send_char`函数来发送字符。在`usart_send_char`函数中,我们首先等待USART1的数据寄存器空位标志位被置位,然后将待发送字符通过USART1发送出去即可。 要编译和下载该程序,你需要使用Keil MDK或者其他类似的IDE,并将该代码保存为`main.c`文件。然后将F103单片机与电脑连接,并使用ST-Link或者其他支持SWD协议的调试器进行下载和调试。如果一切顺利,你应该能够在串口工具中看到按键对应的字符被打印出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值