📄 ps2.s
字号:
.module PS2.C
.area text(rom, con, rel)
.dbfile D:\电子设计\AVR程序\PS2\PS2.C
.area lit(rom, con, rel)
_UnShifted::
.byte 28,'a
.byte 50,'b
.byte 33,'c
.byte 35,'d
.byte 36,'e
.byte 43,'f
.byte 52,'g
.byte 51,'h
.byte 'C,'i
.byte 59,'j
.byte 'B,'k
.byte 'K,'l
.byte 58,'m
.byte 49,'n
.byte 'D,'o
.byte 'M,'p
.byte 21,'q
.byte 45,'r
.byte 27,'s
.byte 44,'t
.byte 60,'u
.byte 42,'v
.byte 29,'w
.byte 34,'x
.byte 53,'y
.byte 26,'z
.byte 'E,48
.byte 22,49
.byte 30,50
.byte 38,51
.byte 37,52
.byte 46,53
.byte 54,54
.byte 61,55
.byte 62,56
.byte 'F,57
.byte 14,96
.byte 'N,45
.byte 'U,61
.byte 93,92
.byte 41,32
.byte 'T,91
.byte 91,93
.byte 'L,59
.byte 'R,39
.byte 'A,44
.byte 'I,46
.byte 'J,47
.byte 'q,46
.byte 'p,48
.byte 'i,49
.byte 'r,50
.byte 'z,51
.byte 'k,52
.byte 's,53
.byte 't,54
.byte 'l,55
.byte 'u,56
.byte 125,57
.dbfile D:\电子设计\AVR程序\PS2\scancodes.h
.dbsym e UnShifted _UnShifted A[118:59:2]kc
_Shifted::
.byte 28,'A
.byte 50,'B
.byte 33,'C
.byte 35,'D
.byte 36,'E
.byte 43,'F
.byte 52,'G
.byte 51,'H
.byte 'C,'I
.byte 59,'J
.byte 'B,'K
.byte 'K,'L
.byte 58,'M
.byte 49,'N
.byte 'D,'O
.byte 'M,'P
.byte 21,'Q
.byte 45,'R
.byte 27,'S
.byte 44,'T
.byte 60,'U
.byte 42,'V
.byte 29,'W
.byte 34,'X
.byte 53,'Y
.byte 26,'Z
.byte 'E,48
.byte 22,49
.byte 30,50
.byte 38,51
.byte 37,52
.byte 46,53
.byte 54,54
.byte 61,55
.byte 62,56
.byte 'F,57
.byte 14,126
.byte 'N,95
.byte 'U,43
.byte 93,124
.byte 41,32
.byte 'T,123
.byte 91,125
.byte 'L,58
.byte 'R,34
.byte 'A,60
.byte 'I,62
.byte 'J,63
.byte 'q,46
.byte 'p,48
.byte 'i,49
.byte 'r,50
.byte 'z,51
.byte 'k,52
.byte 's,53
.byte 't,54
.byte 'l,55
.byte 'u,56
.byte 125,57
.dbsym e Shifted _Shifted A[118:59:2]kc
.area data(ram, con, rel)
.dbfile D:\电子设计\AVR程序\PS2\scancodes.h
_IntNum:
.blkb 1
.area idata
.byte 0
.area data(ram, con, rel)
.dbfile D:\电子设计\AVR程序\PS2\scancodes.h
.dbfile D:\电子设计\AVR程序\PS2\PS2.C
.dbsym s IntNum _IntNum c
_Key_UP:
.blkb 1
.area idata
.byte 0
.area data(ram, con, rel)
.dbfile D:\电子设计\AVR程序\PS2\PS2.C
.dbsym s Key_UP _Key_UP c
_Shift:
.blkb 1
.area idata
.byte 0
.area data(ram, con, rel)
.dbfile D:\电子设计\AVR程序\PS2\PS2.C
.dbsym s Shift _Shift c
_BF:
.blkb 1
.area idata
.byte 0
.area data(ram, con, rel)
.dbfile D:\电子设计\AVR程序\PS2\PS2.C
.dbsym s BF _BF c
.area text(rom, con, rel)
.dbfile D:\电子设计\AVR程序\PS2\PS2.C
.dbfunc e uart0_init _uart0_init fV
; tmp -> R10,R11
; baud -> R10,R11
.even
_uart0_init::
xcall push_xgsetF00C
movw R10,R16
.dbline -1
.dbline 21
; #include<iom16v.h>
; #include<macros.h>
; #include "scancodes.h"
;
; #define F_CPU 8000000
; #define Key_Data ((PIND&0x10)>>4)
; #define Key_CLK ((PIND&0x08)>>3)
; static unsigned char IntNum = 0; //中断次数计数
; static unsigned char KeyV; //键值
; static unsigned char Key_UP=0, Shift = 0;//Key_UP是键松开标识,Shift是Shift键按下标识
; static unsigned char BF = 0; //标识是否有字符被收到
;
;
;
; /******************************************************************************/
; /********************************串口的初始化**********************************/
; //UART0 initialize
; // desired baud rate: 9600
; // actual: baud rate:9615 (0.2%)
; void uart0_init(unsigned int baud)
; {
.dbline 24
; unsigned int tmp;
; /* 设置波特率*/
; tmp= F_CPU/baud/16-1;
movw R2,R10
clr R4
clr R5
ldi R20,0
ldi R21,18
ldi R22,122
ldi R23,0
st -y,R5
st -y,R4
st -y,R3
st -y,R2
movw R16,R20
movw R18,R22
xcall div32s
ldi R20,16
ldi R21,0
ldi R22,0
ldi R23,0
st -y,R23
st -y,R22
st -y,R21
st -y,R20
xcall div32s
movw R2,R16
movw R4,R18
ldi R20,1
ldi R21,0
ldi R22,0
ldi R23,0
sub R2,R20
sbc R3,R21
sbc R4,R22
sbc R5,R23
movw R10,R2
.dbline 25
; UBRRH = (unsigned char)(tmp>>8);
mov R2,R3
clr R3
out 0x20,R2
.dbline 26
; UBRRL = (unsigned char)tmp;
out 0x9,R10
.dbline 28
; /* 接收器与发送器使能*/
; UCSRB = (1<<RXEN)|(1<<TXEN);
ldi R24,24
out 0xa,R24
.dbline 30
; /* 设置帧格式: 8 个数据位, 2 个停止位*/
; UCSRC = (1<<URSEL)|(1<<USBS)|(1<<UCSZ0)|(1<<UCSZ1);
ldi R24,142
out 0x20,R24
.dbline -2
L1:
.dbline 0 ; func end
xjmp pop_xgsetF00C
.dbsym r tmp 10 i
.dbsym r baud 10 i
.dbend
.dbfunc e USART_Transmit _USART_Transmit fV
; data -> R16
.even
_USART_Transmit::
.dbline -1
.dbline 38
; }
;
;
;
;
; // 数据发送【发送5 到8 位数据位的帧】
; void USART_Transmit( unsigned char data )
; {
L3:
.dbline 41
; /* 等待发送缓冲器为空 */
; while ( !( UCSRA & (1<<UDRE)) )
; ;
L4:
.dbline 40
sbis 0xb,5
rjmp L3
X0:
.dbline 43
; /* 将数据放入缓冲器,发送数据 */
; UDR = data;
out 0xc,R16
.dbline -2
L2:
.dbline 0 ; func end
ret
.dbsym r data 16 c
.dbend
.area vector(rom, abs)
.org 8
jmp _int1_isr
.area text(rom, con, rel)
.dbfile D:\电子设计\AVR程序\PS2\PS2.C
.dbfunc e int1_isr _int1_isr fV
.even
_int1_isr::
st -y,R1
st -y,R2
st -y,R3
st -y,R24
st -y,R25
in R1,0x3f
st -y,R1
.dbline -1
.dbline 50
; }
;
;
;
; #pragma interrupt_handler int1_isr:iv_INT1
; void int1_isr(void)
; {
.dbline 51
; if ((IntNum > 0) && (IntNum < 9))
lds R2,_IntNum
ldi R24,0
cp R24,R2
brsh L7
X1:
mov R24,R2
cpi R24,9
brsh L7
X2:
.dbline 52
; {
.dbline 53
; KeyV = KeyV >> 1; //因键盘数据是低>>高,结合上一句所以右移一位
lds R2,_KeyV
lsr R2
sts _KeyV,R2
.dbline 54
; if (Key_Data) KeyV = KeyV | 0x80; //当键盘数据线为1时为1到最高位
in R24,0x10
andi R24,16
swap R24
andi R24,#0x0F
breq L9
X3:
.dbline 54
mov R24,R2
ori R24,128
sts _KeyV,R24
L9:
.dbline 55
; }
L7:
.dbline 56
; IntNum++;
lds R24,_IntNum
subi R24,255 ; addi 1
sts _IntNum,R24
L11:
.dbline 57
; while (!Key_CLK); //等待PS/2CLK拉高
L12:
.dbline 57
in R24,0x10
andi R24,8
lsr R24
lsr R24
lsr R24
breq L11
X4:
.dbline 59
;
; if (IntNum > 10)
ldi R24,10
lds R2,_IntNum
cp R24,R2
brsh L14
X5:
.dbline 60
; {
.dbline 61
; IntNum = 0; //当中断11次后表示一帧数据收完,清变量准备下一次接收
clr R2
sts _IntNum,R2
.dbline 62
; BF = 1; //标识有字符输入完了
ldi R24,1
sts _BF,R24
.dbline 63
; CLI(); //关中断等显示完后再开中断 (注:如这里不用BF和关中断直接调Decode()则所Decode中所调用的所有函数要声明为再入函数)
cli
.dbline 64
; }
L14:
.dbline -2
L6:
.dbline 0 ; func end
ld R1,y+
out 0x3f,R1
ld R25,y+
ld R24,y+
ld R3,y+
ld R2,y+
ld R1,y+
reti
.dbend
.dbfunc e Decode _Decode fV
; TempCyc -> R20
; ScanCode -> R10
.even
_Decode::
xcall push_xgset300C
mov R10,R16
.dbline -1
.dbline 68
; }
;
; void Decode(unsigned char ScanCode) //注意:如SHIFT+G为12H 34H F0H 34H F0H 12H,也就是说shift的通码+G的通码+shift的断码+G的断码
; {
.dbline 71
; unsigned char TempCyc;
;
; if (!Key_UP) //当键盘松开时
lds R2,_Key_UP
tst R2
breq X21
xjmp L17
X21:
X6:
.dbline 72
; {
.dbline 73
; switch (ScanCode)
mov R20,R10
clr R21
cpi R20,89
ldi R30,0
cpc R21,R30
breq L24
X7:
ldi R24,89
ldi R25,0
cp R24,R20
cpc R25,R21
brlt L44
X8:
L43:
cpi R20,18
ldi R30,0
cpc R21,R30
breq L23
X9:
xjmp L19
L44:
cpi R20,240
ldi R30,0
cpc R21,R30
breq L22
X10:
xjmp L19
L22:
.dbline 76
; {
; case 0xF0 : // 当收到0xF0,Key_UP置1表示断码开始
; Key_UP = 1;
ldi R24,1
sts _Key_UP,R24
.dbline 77
; break;
xjmp L18
L23:
.dbline 80
;
; case 0x12 : // 左 SHIFT
; Shift = 1;
ldi R24,1
sts _Shift,R24
.dbline 81
; break;
xjmp L18
L24:
.dbline 84
;
; case 0x59 : // 右 SHIFT
; Shift = 1;
ldi R24,1
sts _Shift,R24
.dbline 85
; break;
xjmp L18
L19:
.dbline 93
;
; default:
; //if (DisNum > 15)
; //{
; // DisplayListChar(0, 1, Cls);//清LCD第二行
; // DisNum = 0;
; //}
; if(!Shift) //如果SHIFT没按下
lds R2,_Shift
tst R2
brne L25
X11:
.dbline 94
; {
.dbline 95
; for (TempCyc = 0;(UnShifted[TempCyc][0]!=ScanCode)&&(TempCyc<59); TempCyc++); //查表显示
clr R20
xjmp L30
L27:
.dbline 95
L28:
.dbline 95
inc R20
L30:
.dbline 95
ldi R24,<_UnShifted
ldi R25,>_UnShifted
mov R30,R20
clr R31
lsl R30
rol R31
add R30,R24
adc R31,R25
lpm R2,Z
cp R2,R10
breq L31
X12:
cpi R20,59
brlo L27
X13:
L31:
.dbline 96
; if (UnShifted[TempCyc][0] == ScanCode)
ldi R24,<_UnShifted
ldi R25,>_UnShifted
mov R30,R20
clr R31
lsl R30
rol R31
add R30,R24
adc R31,R25
lpm R2,Z
cp R2,R10
breq X22
xjmp L18
X22:
X14:
.dbline 97
; USART_Transmit(UnShifted[TempCyc][1]);
ldi R24,<_UnShifted+1
ldi R25,>_UnShifted+1
mov R30,R20
clr R31
lsl R30
rol R31
add R30,R24
adc R31,R25
lpm R16,Z
xcall _USART_Transmit
.dbline 99
;
; }
xjmp L18
L25:
.dbline 101
; else //按下SHIFT
; {
.dbline 102
; for(TempCyc = 0; (Shifted[TempCyc][0]!=ScanCode)&&(TempCyc<59); TempCyc++); //查表显示
clr R20
xjmp L38
L35:
.dbline 102
L36:
.dbline 102
inc R20
L38:
.dbline 102
ldi R24,<_Shifted
ldi R25,>_Shifted
mov R30,R20
clr R31
lsl R30
rol R31
add R30,R24
adc R31,R25
lpm R2,Z
cp R2,R10
breq L39
X15:
cpi R20,59
brlo L35
X16:
L39:
.dbline 103
; if (Shifted[TempCyc][0] == ScanCode)
ldi R24,<_Shifted
ldi R25,>_Shifted
mov R30,R20
clr R31
lsl R30
rol R31
add R30,R24
adc R31,R25
lpm R2,Z
cp R2,R10
brne L18
X17:
.dbline 104
; USART_Transmit(Shifted[TempCyc][1]);
ldi R24,<_Shifted+1
ldi R25,>_Shifted+1
mov R30,R20
clr R31
lsl R30
rol R31
add R30,R24
adc R31,R25
lpm R16,Z
xcall _USART_Transmit
.dbline 106
;
; }
.dbline 108
;
; break;
.dbline 110
; }
; }
xjmp L18
L17:
.dbline 112
; else
; {
.dbline 113
; Key_UP = 0;
clr R2
sts _Key_UP,R2
.dbline 114
; switch (ScanCode) //当键松开时不处理判码,如G 34H F0H 34H 那么第二个34H不会被处理
mov R20,R10
clr R21
cpi R20,18
ldi R30,0
cpc R21,R30
breq L48
X18:
cpi R20,18
ldi R30,0
cpc R21,R30
brlt L45
X19:
L50:
cpi R20,89
ldi R30,0
cpc R21,R30
breq L49
X20:
xjmp L45
L48:
.dbline 117
; {
; case 0x12 : // 左 SHIFT
; Shift = 0;
clr R2
sts _Shift,R2
.dbline 118
; break;
xjmp L46
L49:
.dbline 121
;
; case 0x59 : // 右 SHIFT
; Shift = 0;
clr R2
sts _Shift,R2
.dbline 122
; break;
L45:
L46:
.dbline 124
; }
; }
L18:
.dbline 125
; BF = 0; //标识字符处理完了
clr R2
sts _BF,R2
.dbline -2
L16:
.dbline 0 ; func end
xjmp pop_xgset300C
.dbsym r TempCyc 20 c
.dbsym r ScanCode 10 c
.dbend
.dbfunc e main _main fV
.even
_main::
.dbline -1
.dbline 130
; }
;
;
; void main()
; {
.dbline 131
; PORTD = PORTD | 0x18; //设置为上拉模式
in R24,0x12
ori R24,24
out 0x12,R24
.dbline 132
; DDRD = DDRD & 0xE7; //设置为输入模式
in R24,0x11
andi R24,231
out 0x11,R24
.dbline 133
; uart0_init(9600);
ldi R16,9600
ldi R17,37
xcall _uart0_init
.dbline 134
; MCUCR = MCUCR & 0xf3; //外部中断1设置为低电平触发
in R24,0x35
andi R24,243
out 0x35,R24
.dbline 135
; GICR = (1<<INT1); //外部中断0,1的使能
ldi R24,128
out 0x3b,R24
.dbline 136
; SEI();
sei
L52:
.dbline 138
; do
; {
.dbline 139
; if (BF)
lds R2,_BF
tst R2
breq L55
X23:
.dbline 140
; Decode(KeyV);
lds R16,_KeyV
xcall _Decode
xjmp L56
L55:
.dbline 142
; else
; SEI();; //开中断
sei
L56:
.dbline 142
.dbline 143
; }
L53:
.dbline 144
; while(1)
xjmp L52
X24:
.dbline -2
L51:
.dbline 0 ; func end
ret
.dbend
.area bss(ram, con, rel)
.dbfile D:\电子设计\AVR程序\PS2\PS2.C
_KeyV:
.blkb 1
.dbsym s KeyV _KeyV c
; ;
;
; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -