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