|
新建位圖圖像.jpg (2.14 MB, 下載次數(shù): 43)
下載附件
簡單的原理圖
2019-8-3 12:09 上傳
因為沒有那么多LED燈,我用的詩stm32f103小系統(tǒng)的 所以就用一個燈來測試代碼。整體代碼附加在下面。
- #ifndef __KEY_H
- #define __KEY_H
- #include "sys.h"
- //#define KEY0 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2)
- //#define KEY1 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)
- //#define KEY2 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)
- //#define KEY3 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_5)
- #define HPin GPIO_Pin_3|GPIO_Pin_2|GPIO_Pin_1|GPIO_Pin_0
- #define LPin GPIO_Pin_7|GPIO_Pin_6|GPIO_Pin_5|GPIO_Pin_4
- //#define HRead(); GPIO_ReadInputData(GPIOD);
- //#define LRead(); GPIO_ReadInputData(GPIOD);
- //void KEY_Init(void);
- //u8 KEY_Scan(void);
- void HL_Init_HScan(void);
- void HL_Init_LScan(void);
- u8 HL_Scan(void);
- #endif
復(fù)制代碼- #include "key.h"
- #include "delay.h"
- //void KEY_Init(void)
- //{
- // GPIO_InitTypeDef GPIO_InitStructure;
- // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
- // GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
- // GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
- // GPIO_Init(GPIOE,&GPIO_InitStructure);
- //}
- //u8 KEY_Scan(void)
- //{
- // if((KEY0==0)||(KEY1==0)||(KEY2==0)||(KEY3==0))
- // {
- // delay_ms(20);
- // if(KEY0==0){while(KEY0==0);return 1;}
- // else if(KEY1==0){while(KEY1==0);return 2;}
- // else if(KEY2==0){while(KEY2==0);return 3;}
- // else if(KEY3==0){while(KEY3==0);return 4;}
- // }
- // return 0;
- //}
- //行置為輸入 默認(rèn)為1 列置為輸出默認(rèn)為0 掃描行
- void HL_Init_HScan(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- GPIO_InitStructure.GPIO_Pin=HPin;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin=LPin;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- GPIO_ResetBits(GPIOA,LPin);
- }
- //行置為輸出 默認(rèn)為0 列置為輸入默認(rèn)為1 掃描列
- void HL_Init_LScan(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- GPIO_InitStructure.GPIO_Pin=LPin;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin=HPin;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- GPIO_ResetBits(GPIOA,HPin);
- }
- u8 HL_Scan(void)
- {
- u16 temp=0;
- u8 key=0;
- HL_Init_HScan();
- temp=GPIO_ReadInputData(GPIOA);
- temp&=0x000f; //得到低四位數(shù)據(jù) 檢測4個2進(jìn)制數(shù)中0的存在
- if(temp==0x0e)//第一行有按鍵被按下
- {
- key=1 ;
- }
- else if(temp==0x0d)
- {
- key=2;
- }
- else if(temp==0x0b)
- {
- key=3;
- }
- else if(temp==0x07)
- {
- key=4;
- }else key=0;
- HL_Init_LScan();
- temp=GPIO_ReadInputData(GPIOA);
- temp&=0x00f0;//得到高四位 即列數(shù)據(jù)
- if((temp==0xe0)&&(key!=0))//第一列被按下
- {
- key=(key-1)*4+1;
- }else
- if((temp==0xd0)&&(key!=0))
- {
- key=(key-1)*4+2;
- }else
- if((temp==0xb0)&&(key!=0))
- {
- key=(key-1)*4+3;
- }else
- if((temp==0x70)&&(key!=0))
- {
- key=(key-1)*4+4;
- }
- else key=0;
-
- return key;
- }
復(fù)制代碼 |
-
-
鍵盤矩陣.7z
2019-8-3 15:24 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
185.96 KB, 下載次數(shù): 26, 下載積分: 黑幣 -5
評分
-
查看全部評分
|