久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费
標題:
STM32F103ZET6核心板 不連續IO口4*4 矩陣鍵盤測試程序
[打印本頁]
作者:
XLXUN
時間:
2021-9-10 14:15
標題:
STM32F103ZET6核心板 不連續IO口4*4 矩陣鍵盤測試程序
/*****接口如下
C1---B9;C2-C4----B6 ;R1---B5;2----15;3---12;4---11
***/
#include "key4_4.h"
#include "delay.h"
#include "sys.h"
//8個引腳 4個為行 4個為列
//行輸出端口定義
#define X1_GPIO_PORT GPIOA
#define X2_GPIO_PORT GPIOA
#define X3_GPIO_PORT GPIOA
#define X4_GPIO_PORT GPIOB
//列輸入端口定義
#define Y1_GPIO_PORT GPIOA
#define Y2_GPIO_PORT GPIOA
#define Y3_GPIO_PORT GPIOA
#define Y4_GPIO_PORT GPIOA
//行輸出引腳定義
#define X1_GPIO_PIN GPIO_Pin_5
#define X2_GPIO_PIN GPIO_Pin_6
#define X3_GPIO_PIN GPIO_Pin_7
#define X4_GPIO_PIN GPIO_Pin_10
//列輸入引腳定義
#define Y1_GPIO_PIN GPIO_Pin_4
#define Y2_GPIO_PIN GPIO_Pin_3
#define Y3_GPIO_PIN GPIO_Pin_2
#define Y4_GPIO_PIN GPIO_Pin_1
//行輸出時鐘定義
#define X1_RCC RCC_APB2Periph_GPIOA
#define X2_RCC RCC_APB2Periph_GPIOA
#define X3_RCC RCC_APB2Periph_GPIOA
#define X4_RCC RCC_APB2Periph_GPIOB
//列輸入時鐘定義
#define Y1_RCC RCC_APB2Periph_GPIOA
#define Y2_RCC RCC_APB2Periph_GPIOA
#define Y3_RCC RCC_APB2Periph_GPIOA
#define Y4_RCC RCC_APB2Periph_GPIOA
//移植代碼只需要修改上面的端口和引腳和時鐘即可,下面的代碼不用修改。
//矩陣鍵盤所用的8個引腳可連續可不連續,看實際需要和個人愛好自己定義。
unsigned char Y1,Y2,Y3,Y4;
void Key_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(X1_RCC|X2_RCC|X3_RCC|X4_RCC|Y1_RCC|Y2_RCC|Y3_RCC|Y4_RCC|RCC_APB2Periph_AFIO, ENABLE);
// GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//開啟AFIO時鐘
//
//GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE);
////改變指定管腳的映射 GPIO_Remap_SWJ_Disable SWJ 完全禁用(JTAG+SW-DP)
//
//GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
//改變指定管腳的映射 GPIO_Remap_SWJ_JTAGDisable ,JTAG-DP 禁用 + SW-DP 使能
/*****************************4行輸出*********************************************/
GPIO_InitStructure.GPIO_Pin = X1_GPIO_PIN ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(X1_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = X2_GPIO_PIN ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(X2_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = X3_GPIO_PIN ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(X3_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = X4_GPIO_PIN ;
GPIO_Init(X4_GPIO_PORT, &GPIO_InitStructure);
/**************************************4列輸入*************************************/
GPIO_InitStructure.GPIO_Pin = Y1_GPIO_PIN ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(Y1_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Y2_GPIO_PIN ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(Y2_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Y3_GPIO_PIN ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(Y3_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = Y4_GPIO_PIN;
GPIO_Init(Y4_GPIO_PORT, &GPIO_InitStructure);
}
int Key_Scan(void)
{
uchar KeyVal;
GPIO_SetBits(X1_GPIO_PORT,X1_GPIO_PIN); //先讓X1輸出高
GPIO_SetBits(X2_GPIO_PORT,X2_GPIO_PIN); //先讓X2輸出高
GPIO_SetBits(X3_GPIO_PORT,X3_GPIO_PIN); //先讓X3輸出高
GPIO_SetBits(X4_GPIO_PORT,X4_GPIO_PIN); //先讓X4輸出高
if((GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN)|GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN)|GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN)|GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN))==0x0000)
return -1; //如果X1到X4全為零則沒有按鍵按下
else
{
delay_ms(5); //延時5ms去抖動
if((GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN)|GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN)|GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN)|GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN))==0x0000)
return -1;
}
GPIO_ResetBits(X1_GPIO_PORT,X1_GPIO_PIN);
GPIO_ResetBits(X2_GPIO_PORT,X2_GPIO_PIN);
GPIO_ResetBits(X3_GPIO_PORT,X3_GPIO_PIN);
GPIO_SetBits(X4_GPIO_PORT,X4_GPIO_PIN);
Y1=GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN);Y2=GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN);
Y3=GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN);Y4=GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN);
if(Y1==1&&Y2==0&&Y3==0&&Y4==0)
KeyVal='*';
if(Y1==0&&Y2==1&&Y3==0&&Y4==0)
KeyVal=0;
if(Y1==0&&Y2==0&&Y3==0&&Y4==1)
KeyVal='D';
if(Y1==0&&Y2==0&&Y3==1&&Y4==0)
KeyVal='#';
while(((GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN))|(GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN))|(GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN))|(GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN))) > 0);
//等待按鍵釋放
GPIO_SetBits(X1_GPIO_PORT,X1_GPIO_PIN);
GPIO_ResetBits(X2_GPIO_PORT,X2_GPIO_PIN);
GPIO_ResetBits(X3_GPIO_PORT,X3_GPIO_PIN);
GPIO_ResetBits(X4_GPIO_PORT,X4_GPIO_PIN);
Y1=GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN);Y2=GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN);
Y3=GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN);Y4=GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN);
if(Y1==1&&Y2==0&&Y3==0&&Y4==0)
KeyVal=1;
if(Y1==0&&Y2==1&&Y3==0&&Y4==0)
KeyVal=2;
if(Y1==0&&Y2==0&&Y3==1&&Y4==0)
KeyVal=3;
if(Y1==0&&Y2==0&&Y3==0&&Y4==1)
KeyVal='A';
while(((GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN))|(GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN))|(GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN))|(GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN))) > 0);
GPIO_ResetBits(X1_GPIO_PORT,X1_GPIO_PIN);
GPIO_SetBits(X2_GPIO_PORT,X2_GPIO_PIN);
GPIO_ResetBits(X3_GPIO_PORT,X3_GPIO_PIN);
GPIO_ResetBits(X4_GPIO_PORT,X4_GPIO_PIN);
Y1=GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN);Y2=GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN);
Y3=GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN);Y4=GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN);
if(Y1==1&&Y2==0&&Y3==0&&Y4==0)
KeyVal=4;
if(Y1==0&&Y2==1&&Y3==0&&Y4==0)
KeyVal=5;
if(Y1==0&&Y2==0&&Y3==1&&Y4==0)
KeyVal=6;
if(Y1==0&&Y2==0&&Y3==0&&Y4==1)
KeyVal='B';
while(((GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN))|(GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN))|(GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN))|(GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN))) > 0);
GPIO_ResetBits(X1_GPIO_PORT,X1_GPIO_PIN);
GPIO_ResetBits(X2_GPIO_PORT,X2_GPIO_PIN);
GPIO_SetBits(X3_GPIO_PORT,X3_GPIO_PIN);
GPIO_ResetBits(X4_GPIO_PORT,X4_GPIO_PIN);
Y1=GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN);Y2=GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN);
Y3=GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN);Y4=GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN);
if(Y1==1&&Y2==0&&Y3==0&&Y4==0)
KeyVal=7;
if(Y1==0&&Y2==1&&Y3==0&&Y4==0)
KeyVal=8;
if(Y1==0&&Y2==0&&Y3==1&&Y4==0)
KeyVal=9;
if(Y1==0&&Y2==0&&Y3==0&&Y4==1)
KeyVal='C';
while(((GPIO_ReadInputDataBit(Y1_GPIO_PORT,Y1_GPIO_PIN))|(GPIO_ReadInputDataBit(Y2_GPIO_PORT,Y2_GPIO_PIN))|(GPIO_ReadInputDataBit(Y3_GPIO_PORT,Y3_GPIO_PIN))|(GPIO_ReadInputDataBit(Y4_GPIO_PORT,Y4_GPIO_PIN))) > 0);
return KeyVal;
}
/************************************
按鍵表盤為: 1 2 3 A
4 5 6 B
7 8 9 C
* 0 # D
************************************/
void Key_Test(void)
{
int num;
num = Key_Scan();
// printf("%d",num) ;
switch(num)
{
// { case -1:printf("0\n"); break;
case 0: printf("0\n"); break;
case 1: printf("1\n"); break;
case 2: printf("2\n"); break;
case 3: printf("3\n"); break;
case 4: printf("4\n"); break;
case 5: printf("5\n"); break;
case 6: printf("6\n"); break;
case 7: printf("7\n"); break;
case 8: printf("8\n"); break;
case 9: printf("9\n"); break;
case 'A': printf("A\n"); break;
case 'B': printf("B\n"); break;
case 'C': printf("C\n"); break;
case 'D': printf("D\n"); break;
case '#': printf("#\n"); break;
case '*': printf("*\n"); break;
}
}
復制代碼
代碼下載:
代碼.7z
(219.59 KB, 下載次數: 26)
2021-9-10 18:11 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://m.zg4o1577.cn/bbs/)
Powered by Discuz! X3.1
主站蜘蛛池模板:
婷婷伊人网
|
国产1级片
|
国产第二区
|
黄色大片免费在线观看
|
中文字幕欧美日韩
|
99热精品在线观看
|
91精品久久久久久久久久
|
免费欧美视频
|
99av
|
在线视频一区二区三区
|
毛片网站大全
|
中文日韩欧美
|
www视频在线观看网站
|
激情综合久久
|
在线视频亚洲
|
午夜小视频在线观看
|
国产精品一区二区在线免费观看
|
久久免费网
|
欧美日韩中文字幕在线
|
欧美久久一区二区
|
2018天天操
|
中文在线字幕免费观看
|
久久不射网
|
国产日韩欧美一区二区
|
欧美色婷婷
|
精品免费在线
|
精品免费
|
91免费网站入口
|
久草视频网站
|
中文字幕在线观看不卡
|
野外(巨肉高h)
|
精品一区二区三区四区五区
|
五月天激情影院
|
在线日韩视频
|
伊人久久影院
|
av片在线观看
|
亚洲伊人影院
|
国产一区在线视频
|
午夜激情网站
|
人人插人人爱
|
天天综合天天做天天综合
|