|
下面是按鍵掃描代碼:
- 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); //延時(shí)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
- ************************************/
復(fù)制代碼 |
|