?? keyboard.c
字號(hào):
#pragma SRC
#include <stdio.h>
#include <reg51.h>
#define TRUE 1
#define FALSE 0
#define DELAY_VALUE 3
#define PinDrvKey1 P1_0
#define PinDrvKey2 P1_1
#define PinDrvKey3 P1_2
#define PinDrvKey4 P1_3 //鍵盤掃描的驅(qū)動(dòng)線
#define PinScanKey1 P1_4
#define PinScanKey2 P1_5
#define PinScanKey3 P1_6
#define PinScanKey4 P1_7 //鍵盤掃描的信號(hào)獲取線
#define PinLamp P0_0 //指示燈驅(qū)動(dòng)管腳
#define PinSpeaker P0_1 //蜂鳴器驅(qū)動(dòng)管腳
sbit P0_0=P0^0;
sbit P0_1=P0^1;
sbit P0_2=P0^2;
sbit P0_3=P0^3;
sbit P0_4=P0^4;
sbit P0_5=P0^5;
sbit P0_6=P0^6;
sbit P0_7=P0^7;
sbit P1_0=P1^0;
sbit P1_1=P1^1;
sbit P1_2=P1^2;
sbit P1_3=P1^3;
sbit P1_4=P1^4;
sbit P1_5=P1^5;
sbit P1_6=P1^6;
sbit P1_7=P1^7;
sbit P2_0=P2^0;
sbit P2_1=P2^1;
sbit P2_2=P2^2;
sbit P2_3=P2^3;
sbit P2_4=P2^4;
sbit P2_5=P2^5;
sbit P2_6=P2^6;
sbit P2_7=P2^7;
void initial(void); //初始化子程序
void delay(short i);
unsigned char keyscan(void);
void main(void)
{
unsigned char keyword;
initial(); //鍵盤驅(qū)動(dòng)信號(hào)都設(shè)為低
keyword=keyscan();
}
void initial(void)
{
P1_0=FALSE;
P1_1=FALSE;
P1_2=FALSE;
P1_3=FALSE; //鍵盤驅(qū)動(dòng)信號(hào)都設(shè)為低
P0_0=TRUE; //發(fā)光二極管設(shè)為不亮
P0_1=TRUE; //蜂鳴器不叫
}
unsigned char keyscan()
{
PinDrvKey1=TRUE;
if(PinScanKey1==TRUE)
PinLamp=FALSE;
PinSpeaker=FALSE;
delay(DELAY_VALUE); //如果判斷有信號(hào),則延時(shí)一次
PinLamp=TRUE;
PinSpeaker=TRUE;
if(PinScanKey1==TRUE)
{ delay(DELAY_VALUE); //假如判斷還有信號(hào),則再延時(shí)
if(PinScanKey1==TRUE)
return 1;} //確認(rèn)確實(shí)有按鍵信號(hào),返回鍵值
if(PinScanKey2==TRUE)
PinLamp=FALSE;
PinSpeaker=FALSE;
delay(DELAY_VALUE); //如果判斷有信號(hào),則延時(shí)一次
PinLamp=TRUE;
PinSpeaker=TRUE;
if(PinScanKey2==TRUE)
{ delay(DELAY_VALUE); //假如判斷還有信號(hào),則再延時(shí)
if(PinScanKey2==TRUE)
return 5;} //確認(rèn)確實(shí)有按鍵信號(hào),返回鍵值
if(PinScanKey3==TRUE)
PinLamp=FALSE;
PinSpeaker=FALSE;
delay(DELAY_VALUE); //如果判斷有信號(hào),則延時(shí)一次
PinLamp=TRUE;
PinSpeaker=TRUE;
if(PinScanKey3==TRUE)
{ delay(DELAY_VALUE); //假如判斷還有信號(hào),則再延時(shí)
if(PinScanKey3==TRUE)
return 9;} //確認(rèn)確實(shí)有按鍵信號(hào),返回鍵值
if(PinScanKey4==TRUE)
PinLamp=FALSE;
PinSpeaker=FALSE;
delay(DELAY_VALUE); //如果判斷有信號(hào),則延時(shí)一次
PinLamp=TRUE;
PinSpeaker=TRUE;
if(PinScanKey2==TRUE)
{ delay(DELAY_VALUE); //假如判斷還有信號(hào),則再延時(shí)
if(PinScanKey2==TRUE)
return 12;} //確認(rèn)確實(shí)有按鍵信號(hào),返回鍵值
PinDrvKey1=FALSE; //以上語(yǔ)句掃描第一列
PinDrvKey2=TRUE;
if(PinScanKey1==TRUE)
PinLamp=FALSE;
PinSpeaker=FALSE;
delay(DELAY_VALUE); //如果判斷有信號(hào),則延時(shí)一次
PinLamp=TRUE;
PinSpeaker=TRUE;
if(PinScanKey1==TRUE)
{ delay(DELAY_VALUE); //假如判斷還有信號(hào),則再延時(shí)
if(PinScanKey1==TRUE)
return 2;} //確認(rèn)確實(shí)有按鍵信號(hào),返回鍵值
if(PinScanKey2==TRUE)
PinLamp=FALSE;
PinSpeaker=FALSE;
delay(DELAY_VALUE); //如果判斷有信號(hào),則延時(shí)一次
PinLamp=TRUE;
PinSpeaker=TRUE;
if(PinScanKey2==TRUE)
{ delay(DELAY_VALUE); //假如判斷還有信號(hào),則再延時(shí)
if(PinScanKey2==TRUE)
return 6;} //確認(rèn)確實(shí)有按鍵信號(hào),返回鍵值
if(PinScanKey3==TRUE)
PinLamp=FALSE;
PinSpeaker=FALSE;
delay(DELAY_VALUE); //如果判斷有信號(hào),則延時(shí)一次
PinLamp=TRUE;
PinSpeaker=TRUE;
if(PinScanKey3==TRUE)
{ delay(DELAY_VALUE); //假如判斷還有信號(hào),則再延時(shí)
if(PinScanKey3==TRUE)
return 0;} //確認(rèn)確實(shí)有按鍵信號(hào),返回鍵值
if(PinScanKey4==TRUE)
PinLamp=FALSE;
PinSpeaker=FALSE;
delay(DELAY_VALUE); //如果判斷有信號(hào),則延時(shí)一次
PinLamp=TRUE;
PinSpeaker=TRUE;
if(PinScanKey4==TRUE)
{ delay(DELAY_VALUE); //假如判斷還有信號(hào),則再延時(shí)
if(PinScanKey4==TRUE)
return 13;} //確認(rèn)確實(shí)有按鍵信號(hào),返回鍵值
PinDrvKey2=FALSE; //以上語(yǔ)句掃描第二列
PinDrvKey3=TRUE;
if(PinScanKey1==TRUE)
PinLamp=FALSE;
PinSpeaker=FALSE;
delay(DELAY_VALUE); //如果判斷有信號(hào),則延時(shí)一次
PinLamp=TRUE;
PinSpeaker=TRUE;
if(PinScanKey1==TRUE)
{ delay(DELAY_VALUE); //假如判斷還有信號(hào),則再延時(shí)
if(PinScanKey1==TRUE)
return 3;} //確認(rèn)確實(shí)有按鍵信號(hào),返回鍵值
if(PinScanKey2==TRUE)
PinLamp=FALSE;
PinSpeaker=FALSE;
delay(DELAY_VALUE); //如果判斷有信號(hào),則延時(shí)一次
PinLamp=TRUE;
PinSpeaker=TRUE;
if(PinScanKey2==TRUE)
{ delay(DELAY_VALUE); //假如判斷還有信號(hào),則再延時(shí)
if(PinScanKey2==TRUE)
return 7;} //確認(rèn)確實(shí)有按鍵信號(hào),返回鍵值
if(PinScanKey3==TRUE)
PinLamp=FALSE;
PinSpeaker=FALSE;
delay(DELAY_VALUE); //如果判斷有信號(hào),則延時(shí)一次
PinLamp=TRUE;
PinSpeaker=TRUE;
if(PinScanKey3==TRUE)
{ delay(DELAY_VALUE); //假如判斷還有信號(hào),則再延時(shí)
if(PinScanKey3==TRUE)
return 10;} //確認(rèn)確實(shí)有按鍵信號(hào),返回鍵值
if(PinScanKey4==TRUE)
PinLamp=FALSE;
PinSpeaker=FALSE;
delay(DELAY_VALUE); //如果判斷有信號(hào),則延時(shí)一次
PinLamp=TRUE;
PinSpeaker=TRUE;
if(PinScanKey4==TRUE)
{ delay(DELAY_VALUE); //假如判斷還有信號(hào),則再延時(shí)
if(PinScanKey4==TRUE)
return 14;} //確認(rèn)確實(shí)有按鍵信號(hào),返回鍵值
PinDrvKey3=FALSE; //以上語(yǔ)句掃描第三列
PinDrvKey4=TRUE;
if(PinScanKey4==TRUE)
PinLamp=FALSE;
PinSpeaker=FALSE;
delay(DELAY_VALUE); //如果判斷有信號(hào),則延時(shí)一次
PinLamp=TRUE;
PinSpeaker=TRUE;
if(PinScanKey1==TRUE)
{ delay(DELAY_VALUE); //假如判斷還有信號(hào),則再延時(shí)
if(PinScanKey1==TRUE)
return 4;} //確認(rèn)確實(shí)有按鍵信號(hào),返回鍵值
if(PinScanKey2==TRUE)
PinLamp=FALSE;
PinSpeaker=FALSE;
delay(DELAY_VALUE); //如果判斷有信號(hào),則延時(shí)一次
PinLamp=TRUE;
PinSpeaker=TRUE;
if(PinScanKey2==TRUE)
{ delay(DELAY_VALUE); //假如判斷還有信號(hào),則再延時(shí)
if(PinScanKey2==TRUE)
return 8;} //確認(rèn)確實(shí)有按鍵信號(hào),返回鍵值
if(PinScanKey3==TRUE)
PinLamp=FALSE;
PinSpeaker=FALSE;
delay(DELAY_VALUE); //如果判斷有信號(hào),則延時(shí)一次
PinLamp=TRUE;
PinSpeaker=TRUE;
if(PinScanKey3==TRUE)
{ delay(DELAY_VALUE); //假如判斷還有信號(hào),則再延時(shí)
if(PinScanKey3==TRUE)
return 11;} //確認(rèn)確實(shí)有按鍵信號(hào),返回鍵值
if(PinScanKey4==TRUE)
PinLamp=FALSE;
PinSpeaker=FALSE;
delay(DELAY_VALUE); //如果判斷有信號(hào),則延時(shí)一次
PinLamp=TRUE;
PinSpeaker=TRUE;
if(PinScanKey4==TRUE)
{ delay(DELAY_VALUE); //假如判斷還有信號(hào),則再延時(shí)
if(PinScanKey4==TRUE)
return 15;} //假如判斷還有信號(hào),則再延時(shí)
PinDrvKey3=FALSE; //以上語(yǔ)句掃描第四列
return 16;
}
void delay(short i)
{
int j=0;
int k=0;
k=i*DELAY_VALUE;
while (j<k)j++;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -