|
矩陣掃描,沒有按鍵按下一直返回0 ,使用時(shí)最最好加判斷和延時(shí),不然太快了看不到
全部資料下載地址:
KEY.zip
(2.61 KB, 下載次數(shù): 68)
2017-8-7 08:12 上傳
點(diǎn)擊文件名下載附件
stm32單片機(jī)源程序:
- #include "stm32f10x.h"
- #include "key.h"
- #include "sys.h"
- #include "delay.h"
- //////////////////////////////////////////////////////////////////////////////////
- //本程序只供學(xué)習(xí)使用,未經(jīng)作者許可,不得用于其它任何用途
- //ALIENTEK戰(zhàn)艦STM32開發(fā)板
- //按鍵驅(qū)動(dòng)代碼
- //版本:V1.0
- //版權(quán)所有,盜版必究。
- //Copyright(C) 廣州市星翼電子科技有限公司 2009-2019
- //All rights reserved
- //////////////////////////////////////////////////////////////////////////////////
- /* 定義相關(guān)變量*/
- #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:延時(shí)Nus
- parameter:n
- return:無
- *******************************************************************************************/
- void Delay_nus(u32 n)
- {
- u32 j;
- while(n--)
- { j=8;
- while(j--);
- }
- }
- /******************************************************************************************
- name:void Delay_nms(u32 n)
- function:延時(shí)Nms
- parameter:n
- return:無
- *******************************************************************************************/
- void Delay_nms(u32 n)
- {
- while(n--)
- Delay_nus(1100);
- }
-
- //按鍵初始化函數(shù)
- void KEY_Init(void) //IO初始化
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE,ENABLE);//使能PORTA,PORTE時(shí)鐘
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;//KEY0-KEY2
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //設(shè)置成上拉輸入
- 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設(shè)置成輸入,默認(rèn)下拉
- GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.0
- }
- //按鍵處理函數(shù)
- //返回按鍵值
- //mode:0,不支持連續(xù)按;1,支持連續(xù)按;
- //0,沒有任何按鍵按下
- //1,KEY0按下
- //2,KEY1按下
- //3,KEY2按下
- //4,KEY3按下 WK_UP
- //注意此函數(shù)有響應(yīng)優(yōu)先級,KEY0>KEY1>KEY2>KEY3!!
- u8 KEY_Scan(u8 mode)
- {
- static u8 key_up=1;//按鍵按松開標(biāo)志
- if(mode)key_up=1; //支持連按
- if(key_up&&(KEY0==0||KEY1==0||KEY2==0||WK_UP==1))
- {
- delay_ms(10);//去抖動(dòng)
- 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
- {//這里不再延時(shí)再掃描,因?yàn)橐呀?jīng)確定了不是抖動(dòng)才會(huì)進(jìn)入本步操作
- 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]);
- }
復(fù)制代碼
|
|