.module keypad.c
.area text(rom, con, rel)
.dbfile C:\work\mega128\mix_exp\keypad.c
.dbfunc e scan_key _scan_key fc
; shift -> R22,R23
; i -> R10
; temp -> R12
.even
_scan_key::
xcall push_gset4
.dbline -1
.dbline 6
; #include
; /*键盘扫描函数,没有键按下返回0x7f*/
; /*如果shift按下,返回值最高位为1*/
; extern void delay_ms(unsigned int time);
; unsigned char scan_key(void)
; {
.dbline 8
; unsigned char i,temp;
; unsigned shift=0;
clr R22
clr R23
.dbline 9
; DDRB=0x0f;
ldi R24,15
out 0x17,R24
.dbline 10
; PORTB=0xff;
ldi R24,255
out 0x18,R24
.dbline 11
; for (i=0;i clr R10
xjmp L5
L2:
.dbline 12
; {
.dbline 13
; PORTB=~(1 ldi R16,1
mov R17,R10
xcall lsl8
mov R2,R16
com R2
out 0x18,R2
.dbline 14
; delay_ms(5); //等待低电平稳定
ldi R16,5
ldi R17,0
xcall _delay_ms
.dbline 15
; temp=PINB&0xf0;//读取键盘扫描信号,屏蔽低四位
in R24,0x16
andi R24,240
mov R12,R24
.dbline 16
; if ((PIND&0x20)==0) temp|=01;//检查shift键
sbic 0x10,5
rjmp L6
.dbline 16
ori R24,1
mov R12,R24
L6:
.dbline 17
; if (temp!=0xf0)//如果有键按下,延时10ms消抖
mov R24,R12
cpi R24,240
brne X1
xjmp L8
X1:
.dbline 18
; { //本程序用display代替10ms延时,
.dbline 19
; delay_ms(5); //可消除显示抖动
ldi R16,5
ldi R17,0
xcall _delay_ms
.dbline 20
; temp=PINB&0xf0;//再读键盘
in R24,0x16
andi R24,240
mov R12,R24
.dbline 21
; if ((PIND&0x20)==0) //检查shift键
sbic 0x10,5
rjmp L10
.dbline 22
; {
.dbline 23
; temp|=01;
ori R24,1
mov R12,R24
.dbline 24
; shift=1;
ldi R22,1
ldi R23,0
.dbline 25
; }
L10:
.dbline 26
; if (temp!=0xf0)
mov R24,R12
cpi R24,240
brne X2
xjmp L12
X2:
.dbline 27
; {
.dbline 28
; temp&=0xf0;
andi R24,240
mov R12,R24
.dbline 29
; switch (temp) //计算键值
mov R20,R24
clr R21
cpi R20,176
ldi R30,0
cpc R21,R30
breq L18
ldi R24,176
ldi R25,0
cp R24,R20
cpc R25,R21
brlt L22
L21:
cpi R20,112
ldi R30,0
cpc R21,R30
breq L17
xjmp L14
L22:
cpi R20,208
ldi R30,0
cpc R21,R30
breq L19
cpi R20,208
ldi R30,0
cpc R21,R30
brlt L14
L23:
cpi R20,224
ldi R30,0
cpc R21,R30
breq L20
xjmp L14
X0:
.dbline 30
; {
L17:
.dbline 31
; case 0x70:temp=15-(3-i)*4;break;
ldi R17,3
sub R17,R10
ldi R16,4
muls R16,R17
ldi R24,15
mov R12,R24
sub R12,R0
.dbline 31
xjmp L15
L18:
.dbline 32
; case 0xb0:temp=14-(3-i)*4;break;
ldi R17,3
sub R17,R10
ldi R16,4
muls R16,R17
ldi R24,14
mov R12,R24
sub R12,R0
.dbline 32
xjmp L15
L19:
.dbline 33
; case 0xd0:temp=13-(3-i)*4;break;
ldi R17,3
sub R17,R10
ldi R16,4
muls R16,R17
ldi R24,13
mov R12,R24
sub R12,R0
.dbline 33
xjmp L15
L20:
.dbline 34
; case 0xe0:temp=12-(3-i)*4;break;
ldi R17,3
sub R17,R10
ldi R16,4
muls R16,R17
ldi R24,12
mov R12,R24
sub R12,R0
.dbline 34
xjmp L15
L14:
.dbline 35
; default:temp=0x7f;
ldi R24,127
mov R12,R24
.dbline 36
; }
L15:
.dbline 37
; if (shift==1) temp|=0x80;
cpi R22,1
ldi R30,0
cpc R23,R30
brne L24
.dbline 37
mov R24,R12
ori R24,128
mov R12,R24
L24:
.dbline 38
; return temp;
mov R16,R12
xjmp L1
L12:
.dbline 40
; }
; }
L8:
.dbline 41
ldi R24,255
out 0x18,R24
.dbline 42
L3:
.dbline 11
inc R10
L5:
.dbline 11
mov R24,R10
cpi R24,4
brsh X3
xjmp L2
X3:
.dbline 43
; PORTB=0xff;
; }
; return 0x7f;
ldi R16,127
.dbline -2
L1:
xcall pop_gset4
.dbline 0 ; func end
ret
.dbsym r shift 22 i
.dbsym r i 10 c
.dbsym r temp 12 c
.dbend
.dbfunc e keypad _keypad fc
; temp1 -> R20
; temp2 -> R22
.even
_keypad::
xcall push_gset2
.dbline -1
.dbline 46
; }
; unsigned char keypad(void)
; {
.dbline 48
; unsigned char temp1,temp2;
; temp1=scan_key();
xcall _scan_key
mov R20,R16
.dbline 49
; if (temp1==0x7f) return 0x7f;
cpi R16,127
brne L27
.dbline 49
ldi R16,127
xjmp L26
L27:
L29:
.dbline 51
; do
; temp2=scan_key();
xcall _scan_key
mov R22,R16
L30:
.dbline 52
; while (temp1==temp2);
cp R20,R22
breq L29
.dbline 53
; return temp1;
mov R16,R20
.dbline -2
L26:
xcall pop_gset2
.dbline 0 ; func end
ret
.dbsym r temp1 20 c
.dbsym r temp2 22 c
.dbend