標(biāo)題:
STM32驅(qū)動4*4矩陣鍵盤程序,無上拉電阻,直接可用
[打印本頁]
作者:
咸魚王啊
時間:
2019-8-5 09:26
標(biāo)題:
STM32驅(qū)動4*4矩陣鍵盤程序,無上拉電阻,直接可用
STM32單片機源程序如下:
#include "Keyboard.h"
#include "stm32f10x.h"
#define Key_GPIO GPIOD
#define ReKey RCC_APB2Periph_GPIOD
#define Key_Hang 0x000f
#define Key_Lie 0x00f0
/**
* 函數(shù)功能:矩陣鍵盤初始化
* 參 數(shù) :無
* 返回值 :無
*/
void KEY_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(ReKey,ENABLE); //使能
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉輸入
GPIO_InitStructure.GPIO_Pin = Key_Hang;
GPIO_Init(Key_GPIO,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
GPIO_InitStructure.GPIO_Pin = Key_Lie;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(Key_GPIO,&GPIO_InitStructure);
GPIO_SetBits(Key_GPIO,Key_Lie);
}
/**
* 函數(shù)功能:松手檢測
* 參 數(shù) :無
* 返回值 :成功-0 失敗-1
*/
uint8_t keyScan(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin)
{
if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == 0)
{
while(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == 0); //松手檢測
return 0;
}
else
return 1;
}
/**
* 函數(shù)功能:按鍵檢測
* 參 數(shù) :無
* 返回值 :int型鍵值
*/
int Key_Scan(void)
{
static u16 pin[4] = {GPIO_Pin_4 , GPIO_Pin_5 , GPIO_Pin_6 , GPIO_Pin_7};
static uint8_t i = 0;
int temp = -1;
GPIO_ResetBits(Key_GPIO,pin[i]); //輪流置低
if(keyScan(Key_GPIO,GPIO_Pin_0) == 0) //如果第一行有按鍵按下
{
switch(i)
{
case 0:return 0;
case 1:return 1;
case 2:return 2;
case 3:return 3;
}
}
else if(keyScan(Key_GPIO,GPIO_Pin_1) == 0) //如果第二行有按鍵按下
{
switch(i)
{
case 0:return 4;
case 1:return 5;
case 2:return 6;
case 3:return 7;
}
}
else if(keyScan(Key_GPIO,GPIO_Pin_2) == 0) //如果第三行有按鍵按下
{
switch(i)
{
case 0:return 8;
case 1:return 9;
case 2:return 10;
case 3:return 11;
}
}
else if(keyScan(Key_GPIO,GPIO_Pin_3) == 0) //如果第四行有按鍵按下
{
switch(i)
{
case 0:return 12;
case 1:return 13;
case 2:return 14;
case 3:return 15;
}
}
GPIO_SetBits(Key_GPIO,pin[i]); //置高
i++;
if(i >= 4)
i = 0;
return temp;
}
復(fù)制代碼
所有資料51hei提供下載:
4×4矩陣鍵盤.7z
(175.25 KB, 下載次數(shù): 78)
2019-8-5 21:59 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1