全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1746
推到 Plurk!
推到 Facebook!

8051矩陣按鍵詢問

尚未結案
Onlyfire
一般會員


發表:1
回覆:0
積分:0
註冊:2019-04-14

發送簡訊給我
#1 引用回覆 回覆 發表時間:2019-05-05 00:09:00 IP:114.36.xxx.xxx 未訂閱
各位先進大家好,小弟我最近在學習8051單晶片,透過YouTube上的教學影片,但是當學到鍵盤這一章節時卡住了,
主要是卡在鍵盤鍵值表的使用上,因為我搞不太懂為何在影片上的鍵值表的設定,同時我寫出的程式也無法正確地作動,希望各位前輩能夠幫忙解析,謝謝!
程式主要是做一個簡易的加法計算器,我比較搞不懂在教學影片中鍵值表如下,
unsigned char code KeycodeMap[4][4] = { //鍵盤鍵值表
{0x31, 0x32, 0x33, 0x26}, //1, 2, 3,
{0x34, 0x34, 0x36, 0x25}, //4, 5, 6,
{0x37, 0x38, 0x39, 0x28}, //7, 8, 9,
{0x30, 0x1B, 0x0D, 0x27} //0, ESC, Enter,
};
但是我查了網路上對於鍵值表相關資訊,大概都會是0xEE, 0xEB...等表示方式,
教學影片上的矩陣鍵盤電路如附件,因此想請問各位前輩,這樣的設定有甚麼樣的意義嗎?為何會跟我查到的不太一樣,
另外我的程式上是否有錯誤,為何按鍵一直沒辦法成功驅動呢?

完整的程式如下:
#include

sbit Key_in_1 = P2^7;
sbit Key_in_2 = P2^6;
sbit Key_in_3 = P2^5;
sbit Key_in_4 = P2^4;

sbit Key_out_1 = P2^0;
sbit Key_out_2 = P2^1;
sbit Key_out_3 = P2^2;
sbit Key_out_4 = P2^3;

unsigned char code Segment [] = {
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, //0, 1, 2, 5, 4, 5, 6, 7
0x80, 0x98, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E //8, 9, A, B, C, D, E, F
};

unsigned char Keysta [4][4] ={
{1,1,1,1}, {1,1,1,1}, {1,1,1,1}, {1,1,1,1}
};
unsigned char LedBuffer[6] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
unsigned char code KeycodeMap[4][4] = { //鍵盤鍵值表
{0x31, 0x32, 0x33, 0x26},
{0x34, 0x34, 0x36, 0x25},
{0x37, 0x38, 0x39, 0x28},
{0x30, 0x1B, 0x0D, 0x27}
};

void KeyDriver();

void main()
{
EA = 1;
TMOD = 0x01;
TH0 = 0xFC;
TL0 = 0x67;
TR0 = 1;
ET0 = 1;

LedBuffer[0] = Segment[0];
while(1)
{
KeyDriver();
}
}

void shownumber(unsigned long num)
{
unsigned char buffer[6];
signed char i = 0; //define signed due to i may be negative
for(i=0; i<6; i )
{
buffer[i] = num % 10;
num = num / 10;
}
for(i=5; i>=1; i--) //Not show the high number
{
if(buffer[i] == 0)
{
LedBuffer[i] = 0xFF;
}
else
break;
}
for(; i>=0; i--)
{
LedBuffer[i] = Segment[buffer[i]];
}
}

void KeyAction(unsigned char keycode)
{
static unsigned long result = 0;
static unsigned long addend = 0;
if((keycode >= 0x30) && (keycode <= 0x39))
{
addend = (addend*10) (keycode - 0x30);
shownumber(addend);
}
else if(keycode == 0x26)
{
result = addend;
addend = 0;
shownumber(result);
}
else if(keycode == 0x0D)
{
result = addend;
addend = 0;
shownumber(result);
}
else if(keycode == 0x1B)
{
addend = 0;
result = 0;
shownumber(addend);
}
}

void KeyDriver()
{
unsigned char i, j;
static unsigned char backup [4][4] = {
{1,1,1,1}, {1,1,1,1}, {1,1,1,1}, {1,1,1,1}
};
for(i=0; i<4; i ) //i表in
{
for(j=0; j<4; j ) //j表out
{
if(backup[i][j] != Keysta[i][j])
{
if(backup[i][j] == 0x00)
{
KeyAction(KeycodeMap[i][j]); //可表示出1-16
}
backup[i][j] = Keysta[i][j]; //更新目前按鍵
}
}
}
}

void KeyScan()
{
static unsigned char keyout = 0;
unsigned char i = 0;
unsigned char Keybuf[4][4] = {
{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF}
};
Keybuf[keyout][0] = (Keybuf[keyout][0] << 1) | Key_in_1;
Keybuf[keyout][1] = (Keybuf[keyout][1] << 1) | Key_in_2;
Keybuf[keyout][2] = (Keybuf[keyout][2] << 1) | Key_in_3;
Keybuf[keyout][3] = (Keybuf[keyout][3] << 1) | Key_in_4;
for(i=0; i<4; i )
{
if((Keybuf[keyout][i] & 0x0F) == 0x00)
{
Keysta[keyout][i] = 0;
}
else if((Keybuf[keyout][i] & 0x0F) == 0x0F)
{
Keysta[keyout][i] = 1;
}
}
keyout ;
keyout = keyout & 0x03;
switch(keyout)
{
case 0: Key_out_4 = 1; Key_out_1 = 0; break;
case 1: Key_out_1 = 1; Key_out_2 = 0; break;
case 2: Key_out_2 = 1; Key_out_3 = 0; break;
case 3: Key_out_3 = 1; Key_out_4 = 0; break;
default: break;
}
}

void LedScan()
{
static unsigned char i = 0;
P0 = 0xFF;
switch (i)
{
case 0: P1 = 0x00; i ; P0 = LedBuffer[0]; break;
case 1: P1 = 0x00; i ; P0 = LedBuffer[1]; break;
case 2: P1 = 0x00; i ; P0 = LedBuffer[2]; break;
case 3: P1 = 0x00; i ; P0 = LedBuffer[3]; break;
case 4: P1 = 0x00; i ; P0 = LedBuffer[4]; break;
case 5: P1 = 0x00; i=0; P0 = LedBuffer[5]; break;
default:break;
}
}
void InterruptTimer0() interrupt 1
{
TH0 = 0xFC;
TL0 = 0x67;
LedScan();
KeyScan();
}
系統時間:2024-04-29 14:35:32
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!