久久ER99热精品一区二区-久久精品99国产精品日本-久久精品免费一区二区三区-久久综合九色综合欧美狠狠

新聞中心

EEPW首頁 > 嵌入式系統 > 設計應用 > 簡單的鍵盤讀寫程序

簡單的鍵盤讀寫程序

作者: 時間:2016-11-11 來源:網絡 收藏
仿真電路如下:

本文引用地址:http://cqxgywz.com/article/201611/316793.htm

電路說明:

鍵盤由P1口引出,123是列線,4567是行線。P2和P3口的LED是檢測按鍵的。

程序如下:

#include
#include

#define uchar unsigned char

uchar key;

delay (void)
{
unsigned int i=1000;
while(i--);
}

output (void)
{
switch(key)
{
case 0: P2=0xfe; break;
case 1: P2=0xfd; break;
case 2: P2=0xfb; break;
case 3: P2=0xf7; break;
case 4: P3=0xfe; break;
case 5: P3=0xfd; break;
case 6: P3=0xfb; break;
case 7: P3=0xf7; break;
case 8: P3=0xef; break;
case 9: P3=0xdf; break;
case 10: P3=0xbf; break;
case 11: P3=0x7f; break;
case 12: P3=0xff; break;
}
}

keyscan (void)
{
uchar temp=0;
P1=0xF0;
delay ();

if(P1!=0xF0)
{
P1=0xF0;
delay();
temp=P1;
temp=temp&0xF0;
temp=~((temp>>4)|0xF0);

if(temp==1)key=0;
else if(temp==2) key=1;
else if(temp==4) key=2;
else if(temp==8) key=3;
else key=12;

P1=0x0F;
delay();
temp=P1;
temp=temp&0x0F;
temp=~(temp|0xF0);

if(temp==2) key=key+0;
else if(temp==4) key=key+4;
else if(temp==8) key=key+8;
else key=12;

output ();
}
else return;
}

main (void)
{
keyscan ();
}

程序說明:

函數定義很清楚,吃飯了,不多說了。



關鍵詞: 鍵盤讀寫程

評論


技術專區

關閉