|
Część 5 - Klawiatura matrycowa
Przykład 5.1.ASM
NAME P05_01_ASM_SRC
$INCLUDE (ATMEL/REG8252.INC)
P05_01_ASM SEGMENT CODE
RSEG P05_01_ASM
row1 equ p3.4
row2 equ p3.5
row3 equ p3.6
row4 equ p3.7
col1 equ p3.0
col2 equ p3.1
col3 equ p3.2
col4 equ p3.3
start:
LCALL GetKbd
CPL A
JZ start
MOV P0, A
SJMP START
GetKbd:
MOV P3,#0FFH
MOV A, #0FFH
CLR row1
JB col1, D1
MOV A,#1
D1:
JB col2, D2
MOV A,#2
D2:
JB col3, D3
MOV A,#3
D3:
JB col4, D4
MOV A,#4
D4:
SETB row1
CLR row2
JB col1, D11
MOV A,#5
D11:
JB col2, D21
MOV A,#6
D21:
JB col3, D31
MOV A,#7
D31:
JB col4, D41
MOV A,#8
D41:
DALEJ2:
SETB row2
CLR row3
JB col1, D12
MOV A,#9
D12:
JB col2, D22
MOV A,#10
D22:
JB col3, D32
MOV A,#11
D32:
JB col4, D42
MOV A,#12
D42:
SETB row3
CLR row4
JB col1, D13
MOV A,#13
D13:
JB col2, D23
MOV A,#14
D23:
JB col3, D33
MOV A,#15
D33:
JB col4, D43
MOV A,#16
D43:
SETB row4
RET
END
Przykład 5.1.C
#include <ATMEL/REG8252.h>
sbit row1 = P3^4;
sbit row2 = P3^5;
sbit row3 = P3^6;
sbit row4 = P3^7;
sbit col1 = P3^0;
sbit col2 = P3^1;
sbit col3 = P3^2;
sbit col4 = P3^3;
char GetKbd(void)
{
char key = 0xFF;
row1 = 0;
if (col1 == 0) key = 1;
if (col2 == 0) key = 2;
if (col3 == 0) key = 3;
if (col4 == 0) key = 4;
row1 = 1;
row2 = 0;
if (col1 == 0) key = 5;
if (col2 == 0) key = 6;
if (col3 == 0) key = 7;
if (col4 == 0) key = 8;
row2 = 1;
row3 = 0;
if (col1 == 0) key = 9;
if (col2 == 0) key = 10;
if (col3 == 0) key = 11;
if (col4 == 0) key = 12;
row3 = 1;
row4 = 0;
if (col1 == 0) key = 13;
if (col2 == 0) key = 14;
if (col3 == 0) key = 15;
if (col4 == 0) key = 16;
row4 = 1;
return key;
}
void main(void)
{
while(1)
{
char x = GetKbd();
x ^= 0xFF;
if (x != 0)
P0 = x;
}
}
Przykład 5.1.BAS
Dim Key As Byte
Dim A As Byte
Do
Call Getkbd
Key = Not Key
If Key <> 0 Then P0 = Key
Loop
End
Sub Getkbd
P3 = 255
Key = 255
Reset P3.4
If P3.0 = 0 Then Key = 1
If P3.1 = 0 Then Key = 2
If P3.2 = 0 Then Key = 3
If P3.3 = 0 Then Key = 4
Set P3.4 : Reset P3.5
If P3.0 = 0 Then Key = 5
If P3.1 = 0 Then Key = 6
If P3.2 = 0 Then Key = 7
If P3.3 = 0 Then Key = 8
Set P3.5 : Reset P3.6
If P3.0 = 0 Then Key = 9
If P3.1 = 0 Then Key = 10
If P3.2 = 0 Then Key = 11
If P3.3 = 0 Then Key = 12
Set P3.6 : Reset P3.7
If P3.0 = 0 Then Key = 13
If P3.1 = 0 Then Key = 14
If P3.2 = 0 Then Key = 15
If P3.3 = 0 Then Key = 16
End Sub
|