当前位置:网站首页>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;
}
}边栏推荐
- About the proposed signature file migration to industry standard format pkcs12
- Unity jsonutility failed to serialize list
- 4K sea bottom and water surface fabrication method and ocean bump displacement texture Download
- Calculate sentence confusion ppl using Bert and gpt-2
- 谷歌的受众群体是如何发挥作用的?
- Three paradigms of database
- Large end storage and small end storage
- Phaser3 load
- How many times does the constructor execute?
- Network communication tcp/ip
猜你喜欢

谷歌加大型文字广告是什么?怎么用?

Leetcode-19- delete the penultimate node of the linked list (medium)

Leetcode question brushing 06 bit operation
![[WSL2]WSL2迁移虚拟磁盘文件ext4.vhdx](/img/e9/4e08e07c2de2f99c2938e79f7f1c44.png)
[WSL2]WSL2迁移虚拟磁盘文件ext4.vhdx

FLIP动画实现思路

How to solve the problems when using TV focusable to package APK in uni app

Summary of various installation methods of Lab View

Set and array conversion, list, array

Leetcode question brushing 02 linked list operation

The storage structure of a tree can adopt the parent representation, that is, the parent pointer array representation. Try to give the corresponding class definition. Each tree node contains two membe
随机推荐
谷歌加大型文字广告是什么?怎么用?
Differences among bio, NiO and AIO
[learn FPGA programming from scratch -21]: Advanced - Architecture - VerilogHDL coding specification
np. Understanding of axis in concatenate
On February 26, 2022, the latest news of national oil price adjustment today
leetcode. 151. flip the words in the string
About the proposed signature file migration to industry standard format pkcs12
Leetcode-78- subset (medium)
Leetcode question brushing 07 double pointer
[wsl2] restrict wsl2 accessible hardware resources (cpu/ memory)
About constructive code blocks, static code blocks, and constructor execution order
受众群体应该选择观察模式还是定位模式?
【斯坦福計網CS144項目】Lab1: StreamReassembler
使用Pygame创建一个简单游戏界面
Go JWT learning summary
Ecological convergence NFT attacks, metaverse ape leads the new paradigm revolution of Web 3.0 meta universe
707. design linked list
Simple operation of MySQL database
V-inline-date, similar to Ctrip, flying pig, time selection with price
Leetcode-19- delete the penultimate node of the linked list (medium)