|
問題:圖一是我仿真使用的矩陣鍵盤,仿真正常使用。在某一個寶買了一個圖二的矩陣鍵盤,行列原理圖是反著的請問我該如何修改我的單片機代碼
- #include "button.h"
- #include "stm32f10x.h"
- #include "delay.h"
- extern u8 pass_word_state; //輸密碼時判斷是輸入了第幾位
- u8 Key_Data=0; //按鍵按下的數據
- u8 Key_flag=0; //按鍵按下標志位,區分按鍵按下一次的
- void KEY_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE);//開啟時鐘
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE); //改變指定管腳的映射完全禁用(JTAG+SW-DP)
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//改變指定管腳的映射,JTAG-DP 禁用 + SW-DP 使能
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //LED
- GPIO_InitStructure.GPIO_Pin =GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- LED0 = 0;
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //蜂鳴器(beep) 低電平觸發
- GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- BEEP=1;
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //繼電器(relay) 高電平觸發
- GPIO_InitStructure.GPIO_Pin =GPIO_Pin_13;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- RELAY=0;
- }
- //按鍵賦值,這里是將八個按鍵拼接成一組51單片機里面的Io,目前測試可行
- void Key_assignment(unsigned char z)
- {
- Key_Data=0x00; //清零
- if(z==0)
- {
- key_LowOutPutConfig();
- key5_1; key6_1; key7_1; key8_1; //使用的方法是反轉法
-
- delay_us(500); //短延時一下
-
- if(key1==1) Key_Data=Key_Data|0x8F;
- else if(key2==1) Key_Data=Key_Data|0x4F;
- else if(key3==1) Key_Data=Key_Data|0x2F;
- else if(key4==1) Key_Data=Key_Data|0x1F;
- else Key_Data=Key_Data|0x0F;
- }
- else
- {
- key_HighOutPutConfig();
- key1_1; key2_1; key3_1; key4_1;//使用的方法是反轉法
- delay_us(500); //短延時一下
-
- if(key5==1) Key_Data=Key_Data|0xF8;
- else if(key6==1) Key_Data=Key_Data|0xF4;
- else if(key7==1) Key_Data=Key_Data|0xF2;
- else if(key8==1) Key_Data=Key_Data|0xF1;
- else Key_Data=Key_Data|0xF0;
- }
- }
- u8 Key_Scan() //按鍵掃描函數
- {
- u8 num=16; //這里一定要先賦個用不到的值
- u8 Key_x;
- Key_assignment(1); //狀態 0 ,此時按鍵沒有按下的話是 0x0f
- delay_us(100); //短延時
- if(Key_Data!=0xF0) //只要是按鍵按下,就會使得 Key_Data!=0x0f
- {
- Key_flag=1; //有按鍵按下,標志位置1
- Key_x=Key_Data;//將此時的鍵值賦值
- Key_assignment(0); //切換狀態 1
- delay_us(100); //短延時一下
- Key_Data=Key_Data&Key_x; //兩個數據按位與,就會產生獨一無二的數據
-
- //當前情況下的鍵值 ,十六進制數據
- // 88 48 28 18 → 7 8 9 12(add)
- // 84 44 24 14 → 4 5 6 13(sub)
- // 82 42 22 12 → 1 2 3 14(enter)
- // 81 41 21 11 → 10(xx) 0 11(yy) 15(back)
- switch(Key_Data)
- {
- case 0x11 : num=back; break;
- case 0x12 : num=enter; break;
- case 0x14 : num=sub; break;
- case 0x18 : num=add; break;
-
- case 0x21 : num=yy; break;
- case 0x22 : num=3; break;
- case 0x24 : num=6; break;
- case 0x28 : num=9; break;
-
- case 0x41 : num=0; break;
- case 0x42 : num=2; break;
- case 0x44 : num=5; break;
- case 0x48 : num=8; break;
-
- case 0x81 : num=xx; break;
- case 0x82 : num=1; break;
- case 0x84 : num=4; break;
- case 0x88 : num=7; break;
- }
- Key_assignment(0);
- while(Key_Data!=0x0f)//松手檢測
- {
- Key_assignment(0);
- }
- }
- return num;
- }
- void key_LowOutPutConfig(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //下拉輸入
- GPIO_InitStructure.GPIO_Pin =C1_pin;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(C1_port, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //下拉輸入
- GPIO_InitStructure.GPIO_Pin =C2_pin;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(C2_port, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //下拉輸入
- GPIO_InitStructure.GPIO_Pin =C3_pin;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(C3_port, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //下拉輸入
- GPIO_InitStructure.GPIO_Pin =C4_pin;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(C4_port, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_InitStructure.GPIO_Pin =R1_pin;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(R1_port, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_InitStructure.GPIO_Pin =R2_pin;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(R2_port, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_InitStructure.GPIO_Pin =R3_pin;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(R3_port, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_InitStructure.GPIO_Pin =R4_pin;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(R4_port, &GPIO_InitStructure);
- }
- void key_HighOutPutConfig(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_InitStructure.GPIO_Pin =C1_pin;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(C1_port, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_InitStructure.GPIO_Pin =C2_pin;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(C2_port, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_InitStructure.GPIO_Pin =C3_pin;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(C3_port, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_InitStructure.GPIO_Pin =C4_pin;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(C4_port, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //下拉輸入
- GPIO_InitStructure.GPIO_Pin =R1_pin;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(R1_port, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //下拉輸入
- GPIO_InitStructure.GPIO_Pin =R2_pin;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(R2_port, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //下拉輸入
- GPIO_InitStructure.GPIO_Pin =R3_pin;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(R3_port, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //下拉輸入
- GPIO_InitStructure.GPIO_Pin =R4_pin;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(R4_port, &GPIO_InitStructure);
- }
復制代碼 |
|