标题:
STM32 4X4按键矩阵的小程序
[打印本页]
作者:
MrHuan991
时间:
2019-2-16 04:28
标题:
STM32 4X4按键矩阵的小程序
前篇帖子写了基本的GPIO复用技术,现在补上按键矩阵技术,由于STM32的GPIO口不像传统51,准双向口,故而不能使用线反转法,我采用了行列扫描的技术,GPIO口用了A组,故而库函数没法直接调用赋值和读取同时16位的函数,只能调用一个个pin读取函数和赋值函数,显得有点臃肿。但是STM32性能牛逼,这么多if的语句还跑的这么流畅,如果是C51早gg了,在STC12勉强可以看看。仅供参考。STM32学起来比51更费劲,英语不好的话,库函数弄死人。
此工程,主要用于编程4X4按键矩阵的识别和编码的功能
单片机源程序如下:
#include <STM32F10X.H>
#include <misc.h>
#define PA0_Low GPIO_ResetBits(GPIOA,GPIO_Pin_0)
#define PA0_High GPIO_SetBits(GPIOA,GPIO_Pin_0)
#define PA1_Low GPIO_ResetBits(GPIOA,GPIO_Pin_1)
#define PA1_High GPIO_SetBits(GPIOA,GPIO_Pin_1)
#define PA2_Low GPIO_ResetBits(GPIOA,GPIO_Pin_2)
#define PA2_High GPIO_SetBits(GPIOA,GPIO_Pin_2)
#define PA3_Low GPIO_ResetBits(GPIOA,GPIO_Pin_3)
#define PA3_High GPIO_SetBits(GPIOA,GPIO_Pin_3)
#define PA4 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4)
#define PA5 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5)
#define PA6 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6)
#define PA7 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7)
#define PC15_Low GPIO_WriteBit(GPIOC,GPIO_Pin_15,Bit_RESET)
#define PC15_High GPIO_WriteBit(GPIOC,GPIO_Pin_15,Bit_SET)
void RCC_Configuration(void); //时钟与复位寄存器
void GPIO_Configuration(void); //GPIO口初始化函数
void Delay_ms(u16 n); //ms级别延时函数
u16 Array_Key(void); //4X4按键矩阵检测函数
void Led_Show(u16 keystatus); //按键检测后的状态显示函数
u16 ledcount=0;
u16 status=0;
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
while(1)
{
Led_Show(Array_Key());
}
}
void RCC_Configuration(void)
{
SystemInit();
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_Struct; //定义一个GPIO初始化的结构体变量
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC, ENABLE);
GPIO_Struct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Struct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Struct.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_Init(GPIOA,&GPIO_Struct);
GPIO_Struct.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Struct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Struct.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_Init(GPIOA,&GPIO_Struct);
GPIO_Struct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Struct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Struct.GPIO_Pin=GPIO_Pin_15;
GPIO_Init(GPIOC,&GPIO_Struct);
PA0_High;PA1_High;PA2_High;PA3_High;
PC15_Low;
}
u16 Array_Key(void)
{
PA0_Low;PA1_High;PA2_High;PA3_High;
if(PA4 == 0)
{
Delay_ms(10);
if(PA4 == 0)
{
while(!PA4);
status = 1;
}
}
if(PA5 == 0)
{
Delay_ms(10);
if(PA5 == 0)
{
while(!PA5);
status = 2;
}
}
if(PA6 == 0)
{
Delay_ms(10);
if(PA6 == 0)
{
while(!PA6);
status = 3;
}
}
if(PA7 == 0)
{
Delay_ms(10);
if(PA7 == 0)
{
while(!PA7);
status = 4;
}
}
PA0_High;PA1_Low;PA2_High;PA3_High;
if(PA4 == 0)
{
Delay_ms(10);
if(PA4 == 0)
{
while(!PA4);
status = 5;
}
}
if(PA5 == 0)
{
Delay_ms(10);
if(PA5 == 0)
{
while(!PA5);
status = 6;
}
}
if(PA6 == 0)
{
Delay_ms(10);
if(PA6 == 0)
{
while(!PA6);
status = 7;
}
}
if(PA7 == 0)
{
Delay_ms(10);
if(PA7 == 0)
{
while(!PA7);
status = 8;
}
}
PA0_High;PA1_High;PA2_Low;PA3_High;
if(PA4 == 0)
{
Delay_ms(10);
if(PA4 == 0)
{
while(!PA4);
status = 9;
}
}
if(PA5 == 0)
{
Delay_ms(10);
if(PA5 == 0)
{
while(!PA5);
status = 10;
}
}
if(PA6 == 0)
{
Delay_ms(10);
if(PA6 == 0)
{
while(!PA6);
status = 11;
}
}
if(PA7 == 0)
{
Delay_ms(10);
if(PA7 == 0)
{
while(!PA7);
status = 12;
}
}
PA0_High;PA1_High;PA2_High;PA3_Low;
if(PA4 == 0)
{
Delay_ms(10);
if(PA4 == 0)
{
while(!PA4);
status = 13;
}
}
if(PA5 == 0)
{
Delay_ms(10);
if(PA5 == 0)
{
while(!PA5);
status = 14;
}
}
if(PA6 == 0)
{
Delay_ms(10);
if(PA6 == 0)
{
while(!PA6);
status = 15;
}
}
if(PA7 == 0)
{
Delay_ms(10);
if(PA7 == 0)
{
while(!PA7);
status = 16;
}
}
return status;
}
void Delay_ms(u16 n)
{
u16 i;
while(n--)
{
i=12000;
while(i--);
}
}
void Led_Show(u16 keystatus)
{
if(keystatus != 0)
{ledcount++;status=0;}
if(ledcount % 2 == 0)
PC15_Low;
else
PC15_High;
}
复制代码
所有资料51hei提供下载:
Key_Array.7z
(192.09 KB, 下载次数: 49)
2019-2-17 03:07 上传
点击文件名下载附件
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1