欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
stm32的4*4矩陣掃描按鍵驅動代碼
[打印本頁]
作者:
阿拉聽聽的天涯
時間:
2017-8-7 08:14
標題:
stm32的4*4矩陣掃描按鍵驅動代碼
矩陣掃描,沒有按鍵按下一直返回0 ,使用時最最好加判斷和延時,不然太快了看不到
全部資料下載地址:
KEY.zip
(2.61 KB, 下載次數: 68)
2017-8-7 08:12 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
stm32單片機源程序:
#include "stm32f10x.h"
#include "key.h"
#include "sys.h"
#include "delay.h"
//////////////////////////////////////////////////////////////////////////////////
//本程序只供學習使用,未經作者許可,不得用于其它任何用途
//ALIENTEK戰艦STM32開發板
//按鍵驅動代碼
//版本:V1.0
//版權所有,盜版必究。
//Copyright(C) 廣州市星翼電子科技有限公司 2009-2019
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////
/* 定義相關變量*/
#define GPIO_Pin_R (GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7)
#define GPIO_Pin_L (GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3)
ErrorStatus HSEStartUpStatus;
u16 const Key_Tab[4][4]=//鍵盤編碼表
{
{1,2,3,10},
{4,5,6,11},
{7,8,9,12},
{13,14,15,16}
};
/******************************************************************************************
name:void Delay_nus(u32 n)
function:延時Nus
parameter:n
return:無
*******************************************************************************************/
void Delay_nus(u32 n)
{
u32 j;
while(n--)
{ j=8;
while(j--);
}
}
/******************************************************************************************
name:void Delay_nms(u32 n)
function:延時Nms
parameter:n
return:無
*******************************************************************************************/
void Delay_nms(u32 n)
{
while(n--)
Delay_nus(1100);
}
//按鍵初始化函數
void KEY_Init(void) //IO初始化
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE,ENABLE);//使能PORTA,PORTE時鐘
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;//KEY0-KEY2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //設置成上拉輸入
GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIOE2,3,4
//初始化 WK_UP-->GPIOA.0 下拉輸入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0設置成輸入,默認下拉
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.0
}
//按鍵處理函數
//返回按鍵值
//mode:0,不支持連續按;1,支持連續按;
//0,沒有任何按鍵按下
//1,KEY0按下
//2,KEY1按下
//3,KEY2按下
//4,KEY3按下 WK_UP
//注意此函數有響應優先級,KEY0>KEY1>KEY2>KEY3!!
u8 KEY_Scan(u8 mode)
{
static u8 key_up=1;//按鍵按松開標志
if(mode)key_up=1; //支持連按
if(key_up&&(KEY0==0||KEY1==0||KEY2==0||WK_UP==1))
{
delay_ms(10);//去抖動
key_up=0;
if(KEY0==0)return KEY0_PRES;
else if(KEY1==0)return KEY1_PRES;
else if(KEY2==0)return KEY2_PRES;
else if(WK_UP==1)return WKUP_PRES;
}else if(KEY0==1&&KEY1==1&&KEY2==1&&WK_UP==0)key_up=1;
return 0;// 無按鍵按下
}
u16 Get_KeyValue(void)//使用PF0~PF7
{
GPIO_InitTypeDef GPIO_InitStructure;
u8 i=5,j=5;
u16 temp1,temp2;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
GPIO_DeInit(GPIOF);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_ResetBits(GPIOF,GPIO_Pin_L);//掃描列值
if((GPIO_ReadInputData(GPIOF)&0x00f0)==0x00f0)
return 0;
else
{
Delay_nms(70);//按鍵消抖
if((GPIO_ReadInputData(GPIOF)&0x00f0)==0x00f0)
return 0;
else
temp1=GPIO_ReadInputData(GPIOF)&0x00f0;
}
switch(temp1)
{
case 0x00e0:j=0;break;
case 0x00d0:j=1;break;
case 0x00b0:j=2;break;
case 0x0070:j=3;break;
default:break;
}
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_ResetBits(GPIOF,GPIO_Pin_R);//掃描行值
if((GPIO_ReadInputData(GPIOF)&0x000f)==0x000f)
return 0;
else
{//這里不再延時再掃描,因為已經確定了不是抖動才會進入本步操作
temp2=GPIO_ReadInputData(GPIOF)&0x000f;
}
switch(temp2)
{
case 0x000e:i=0;break;
case 0x000d:i=1;break;
case 0x000b:i=2;break;
case 0x0007:i=3;break;
default:break;
}
if((i==5)||(j==5))
return 0;
else
return (Key_Tab[i][j]);
}
復制代碼
作者:
seafly
時間:
2018-9-14 21:19
代碼很好,多謝!
歡迎光臨 (http://www.raoushi.com/bbs/)
Powered by Discuz! X3.1