当前位置:网站首页>STM32 3*3矩阵按键(寄存器版本)
STM32 3*3矩阵按键(寄存器版本)
2022-06-13 01:36:00 【夜路难行々】
我使用的引脚为:PA2-7
电路图片:(仅供电路图参考)

思路:(行列扫描法),起始可以将2,3,4设置为上拉输入(下拉输入),5,6,7设置为输出低电平(输出高点平)当按键按下的时候,电平会由1->0,这样就可以检测到电平的变化,从而获得对应的按键接口值
首先,使能你对应的时钟,然后需要把你需要的引脚置零
RCC->APB2ENR|=1<<2;
GPIOA->CRL&=0XFF; // 清零GPIOA->CRL|=0X33388800;注意的是你设置为1000的时候,你不能确定是上拉还是下拉,所以你需要进行ODR操作
GPIOA->ODR|=1<<2; //上拉
GPIOA->ODR|=1<<3;
GPIOA->ODR|=1<<4;当你需要下拉的时候将1换成0即可,并左移至向相应的引脚
然后将5,6,7设置为低电平输出
PAout(5) = 0;
PAout(6) = 0; //设置为低电平
PAout(7) = 0;然后进行检测,当2,3,4其中任何一个检测到低电平的时候,进行相应的返回值(若是二,则知证明是在第一行,三则是在第二行.....)
if(PAin(2) == 0||PAin(3) == 0||PAin(4) == 0)
{
delay_ms(10);
if(PAin(2) == 0)
{
i = 1;
}
if(PAin(3) == 0)
{
i = 2;
}
if(PAin(4) == 0)
{
i = 3;
}
}这样就得知了按键是在哪一行,然后接下里就是寻找是在哪一列,将输入和输出进行反转
GPIOA->CRL&=0X000000FF; // 清零//将pa2,3,4设置为输出,5,6,7设置为输入
GPIOA->CRL|=0X88833300;
GPIOA->ODR|=1<<5; //上拉
GPIOA->ODR|=1<<6;
GPIOA->ODR|=1<<7;
PAout(2) = 0;
PAout(3) = 0; //设置为低电平
PAout(4) = 0;
if(PAin(5) == 0||PAin(6) == 0||PAin(7) == 0)
{
delay_ms(10);
if(PAin(5) == 0)
{
j = 1;
}
if(PAin(6) == 0)
{
j = 2;
}
if(PAin(7) == 0)
{
j = 3;
}
}最后在借助i和j判断是在哪个位置
if(i == 1)
{
if(j == 1)
{
return 1;
}
if(j == 2)
{
return 2;
}
if(j == 3)
{
return 3;
}
}
if(i == 2)
{
if(j == 1)
{
return 4;
}
if(j == 2)
{
return 5;
}
if(j == 3)
{
return 6;
}
}
if(i == 3)
{
if(j == 1)
{
return 7;
}
if(j == 2)
{
return 8;
}
if(j == 3)
{
return 9;
}
}
if(i==0&&j==0)
return 0;在主函数中进行接受相应的字符,然后在串口打印相应的值
u8 t;
Stm32_Clock_Init(9); //系统时钟设置
delay_init(72); //延时初始化
uart_init(72,9600); //串口初始化为9600
while(1)
{
t = KEY_Init();
switch(t)
{
case '1':
delay_ms(1000);
printf("1\r\n");
break;
case '2':
delay_ms(1000);
printf("2\r\n");
break;
case '3':
delay_ms(1000);
printf("3\r\n");
break;
case '4':
delay_ms(1000);
printf("4\r\n");
break;
case '5':
delay_ms(1000);
printf("5\r\n");
break;
case '6':
delay_ms(1000);
printf("6\r\n");
break;
case '7':
delay_ms(1000);
printf("7\r\n");
break;
case '8':
delay_ms(1000);
printf("8\r\n");
break;
case '9':
delay_ms(1000);
printf("9\r\n");
break;
case '0':
delay_ms(1000);
printf("没有按键输入\r\n");
break;
}
}边栏推荐
- Phaser3 load
- MySQL related summary
- Crypto JS reports uglifyjs error
- leetode. 242. valid Letter heteronyms
- Database query user mailbox
- ES6解构赋值
- Mathematical knowledge arrangement: extremum & maximum, stagnation point, Lagrange multiplier
- [wsl2] restrict wsl2 accessible hardware resources (cpu/ memory)
- Project training (XVII) -- personal work summary
- Sonarqube local installation
猜你喜欢

【MathType】利用MathType输出LaTex样式的公式

Stmarl: a spatio temporal multi agentreinforcement learning approach for cooperative traffic
![[leetcode] valid phone number Bash](/img/f8/cecb74f9d8f7c589e62e3a9a874f82.jpg)
[leetcode] valid phone number Bash

Alexnet implements image classification of caltech101 dataset (pytorch Implementation)

The second round of mesa

Record the VMware installation process of VMware Tools and some problems encountered

Network communication tcp/ip

工作与生活

Introduction to convolutional neural network

MySQL related summary
随机推荐
Logical operation bit operation
Vector|hdu-4841 round table questions
Wangdao machine test - Chapter 6 - maximum common divisor GCD and minimum common multiple LCM
leetcode743. 网络延迟时间(中等, dijkstra)
pycharm add configutions
How to solve the problem of database?
Work and life
Use koa to mock data and set cross domain issues
How to turn on the hotspot for the mobile phone after the computer is connected to the network cable
路径字段是什么? ——竞价广告
Downloading wiki corpus and aligning with multilingual wikis
【斯坦福计网CS144项目】Lab1: StreamReassembler
MySQL related summary
RSA encryption colloquial explanation
Web Application & applet application deployment
D template instance does not match declaration
Calculate sentence confusion ppl using Bert and gpt-2
[soft test] software designer knowledge points sorting (to be updated)
leetcode 142. Circular linked list II
[learn FPGA programming from scratch -22]: Advanced chapter - Architecture - Design and modeling of FPGA internal hardware circuit