📄 keyboard.s
字号:
.module keyboard.c
.area lit(rom, con, rel)
_num_table::
.byte 63,6
.byte 91,'O
.byte 'f,'m
.byte 125,7
.byte 127,'o
.byte 64
.dbfile E:\AVRATM~1\key_board\keyboard.c
.dbsym e num_table _num_table A[11:11]kc
.area text(rom, con, rel)
.dbfile E:\AVRATM~1\key_board\keyboard.c
.dbfunc e delay _delay fV
; time -> R16,R17
.even
_delay::
.dbline -1
.dbline 32
; //ICC-AVR application builder : 2004-8-25 1:33
; // Target : M16
; // Crystal: 8.0000Mhz
; #include <iom16v.h>
; #include <macros.h>
; #include <eeprom.h>
;
; #define uchar unsigned char
; #define uint unsigned int
; #define ulong unsigned long
;
; const uchar num_table[] = { 0x3f,0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x40 };
; //自定义定时器
; uint delay_counter, scom_counter, key_counter;
; //定义通讯缓存
; #define SCOM_BUF_LEN 10
; uchar tbuf[SCOM_BUF_LEN];
; uchar rbuf[SCOM_BUF_LEN];
; //显示缓存
; uint l_des_speed, r_des_speed;
; //键盘缓存
; uint key_buf;
; //子函数声明
; void delay( uint time );
; void master( uchar command, uchar* buf, uchar startP, uchar dataLen );
; uint scan_key( void );
; void display( void );
; void error_handle( void );
;
; /******************公共的底层函数区********************/
; void delay( uint time )
; {
.dbline 33
clr R2
clr R3
sts _delay_counter+1,R3
sts _delay_counter,R2
xjmp L5
L2:
.dbline 33
.dbline 33
wdr
.dbline 33
L3:
.dbline 33
L5:
.dbline 33
; for( delay_counter = 0; delay_counter < time; ) { WDR(); }
lds R2,_delay_counter
lds R3,_delay_counter+1
cp R2,R16
cpc R3,R17
brlo L2
.dbline -2
L1:
.dbline 0 ; func end
ret
.dbsym r time 16 i
.dbend
.dbfunc e scan_key _scan_key fi
; key_status -> R20,R21
.even
_scan_key::
xcall push_gset1
.dbline -1
.dbline 37
; }
;
; uint scan_key( void )
; {
.dbline 38
; uint key_status = 0;
clr R20
clr R21
.dbline 39
; PORTC = 0xF7; //定义管脚输入输出状态
ldi R24,247
out 0x15,R24
.dbline 40
; delay(1);
ldi R16,1
ldi R17,0
xcall _delay
.dbline 41
; if (!(PINC & 0x80)) key_status |= BIT(0);
sbic 0x13,7
rjmp L7
.dbline 41
ori R20,1
L7:
.dbline 42
; if (!(PINC & 0x40)) key_status |= BIT(1);
sbic 0x13,6
rjmp L9
.dbline 42
ori R20,2
L9:
.dbline 43
; if (!(PINC & 0x20)) key_status |= BIT(2);
sbic 0x13,5
rjmp L11
.dbline 43
ori R20,4
L11:
.dbline 44
; if (!(PINC & 0x10)) key_status |= BIT(3);
sbic 0x13,4
rjmp L13
.dbline 44
ori R20,8
L13:
.dbline 45
; PORTC = 0xFB;
ldi R24,251
out 0x15,R24
.dbline 46
; delay(1);
ldi R16,1
ldi R17,0
xcall _delay
.dbline 47
; if (!(PINC & 0x80)) key_status |= BIT(4);
sbic 0x13,7
rjmp L15
.dbline 47
ori R20,16
L15:
.dbline 48
; if (!(PINC & 0x40)) key_status |= BIT(5);
sbic 0x13,6
rjmp L17
.dbline 48
ori R20,32
L17:
.dbline 49
; if (!(PINC & 0x20)) key_status |= BIT(6);
sbic 0x13,5
rjmp L19
.dbline 49
ori R20,64
L19:
.dbline 50
; if (!(PINC & 0x10)) key_status |= BIT(7);
sbic 0x13,4
rjmp L21
.dbline 50
ori R20,128
L21:
.dbline 51
; PORTC = 0xFD;
ldi R24,253
out 0x15,R24
.dbline 52
; delay(1);
ldi R16,1
ldi R17,0
xcall _delay
.dbline 53
; if (!(PINC & 0x80)) key_status |= BIT(8);
sbic 0x13,7
rjmp L23
.dbline 53
ori R21,1
L23:
.dbline 54
; if (!(PINC & 0x40)) key_status |= BIT(9);
sbic 0x13,6
rjmp L25
.dbline 54
ori R21,2
L25:
.dbline 55
; if (!(PINC & 0x20)) key_status |= BIT(10);
sbic 0x13,5
rjmp L27
.dbline 55
ori R21,4
L27:
.dbline 56
; if (!(PINC & 0x10)) key_status |= BIT(11);
sbic 0x13,4
rjmp L29
.dbline 56
ori R21,8
L29:
.dbline 57
; PORTC = 0xFE;
ldi R24,254
out 0x15,R24
.dbline 58
; delay(1);
ldi R16,1
ldi R17,0
xcall _delay
.dbline 59
; if (!(PINC & 0x80)) key_status |= BIT(12);
sbic 0x13,7
rjmp L31
.dbline 59
ori R21,16
L31:
.dbline 60
; if (!(PINC & 0x40)) key_status |= BIT(13);
sbic 0x13,6
rjmp L33
.dbline 60
ori R21,32
L33:
.dbline 61
; if (!(PINC & 0x20)) key_status |= BIT(14);
sbic 0x13,5
rjmp L35
.dbline 61
ori R21,64
L35:
.dbline 62
; if (!(PINC & 0x10)) key_status |= BIT(15);
sbic 0x13,4
rjmp L37
.dbline 62
ori R21,128
L37:
.dbline 63
; PORTC = 0xF0;
ldi R24,240
out 0x15,R24
.dbline 64
; DDRC = 0x0F;
ldi R24,15
out 0x14,R24
.dbline 65
; PORTD |= BIT(PD2);
sbi 0x12,2
.dbline 66
; DDRD &= ~BIT(PD2);
cbi 0x11,2
.dbline 67
; return key_status;
movw R16,R20
.dbline -2
L6:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r key_status 20 i
.dbend
.dbfunc e master _master fV
; i -> R20
; dataLen -> R22
; startP -> R10
; buf -> R18,R19
; command -> R16
.even
_master::
xcall push_gset3
ldd R10,y+6
ldd R22,y+8
.dbline -1
.dbline 71
; }
;
; void master( uchar command, uchar* buf, uchar startP, uchar dataLen )
; {
.dbline 73
; uchar i;
; CLI();
cli
.dbline 74
; if ( command == 0x01 ){
cpi R16,1
brne L56
.dbline 74
L42:
.dbline 75
L43:
.dbline 75
; while( !( UCSRA & BIT( UDRE ) ) );
sbis 0xb,5
rjmp L42
.dbline 76
; UDR = command;
out 0xc,R16
L45:
.dbline 77
L46:
.dbline 77
; while( !( UCSRA & BIT( UDRE ) ) );
sbis 0xb,5
rjmp L45
.dbline 78
; UDR = dataLen;
out 0xc,R22
.dbline 79
mov R20,R10
xjmp L51
X0:
.dbline 79
; for( i = startP; i < dataLen + startP; i ++ ){
L52:
.dbline 80
L53:
.dbline 80
sbis 0xb,5
rjmp L52
.dbline 81
ldi R24,<_tbuf
ldi R25,>_tbuf
mov R30,R20
clr R31
add R30,R24
adc R31,R25
ldd R2,z+0
out 0xc,R2
.dbline 82
L49:
.dbline 79
inc R20
L51:
.dbline 79
mov R2,R22
add R2,R10
cp R20,R2
brlo L53
.dbline 83
xjmp L41
X1:
.dbline 83
; while( !( UCSRA & BIT( UDRE ) ) );
; UDR = tbuf[i];
; }
; } else {
L55:
.dbline 84
L56:
.dbline 84
; while( !( UCSRA & BIT( UDRE ) ) );
sbis 0xb,5
rjmp L55
.dbline 85
; UDR = command;
out 0xc,R16
L58:
.dbline 86
L59:
.dbline 86
; while( !( UCSRA & BIT( UDRE ) ) );
sbis 0xb,5
rjmp L58
.dbline 87
; UDR = dataLen;
out 0xc,R22
.dbline 88
mov R20,R10
xjmp L64
X2:
.dbline 88
; for( i = startP; i < dataLen + startP; i ++ ){
L65:
.dbline 89
L66:
.dbline 89
sbis 0xb,7
rjmp L65
.dbline 90
ldi R24,<_rbuf
ldi R25,>_rbuf
mov R30,R20
clr R31
add R30,R24
adc R31,R25
in R2,0xc
std z+0,R2
.dbline 91
L62:
.dbline 88
inc R20
L64:
.dbline 88
mov R2,R22
add R2,R10
cp R20,R2
brlo L66
.dbline 92
; while( !( UCSRA & BIT( RXC ) ) );
; rbuf[i] = UDR;
; }
; }
L41:
.dbline 93
; SEI();
sei
.dbline -2
L39:
xcall pop_gset3
.dbline 0 ; func end
ret
.dbsym r i 20 c
.dbsym r dataLen 22 c
.dbsym r startP 10 c
.dbsym r buf 18 pc
.dbsym r command 16 c
.dbend
.dbfunc e port_init _port_init fV
.even
_port_init::
.dbline -1
.dbline 97
; }
;
; void port_init(void)
; {
.dbline 98
; PORTA = 0x00;
clr R2
out 0x1b,R2
.dbline 99
; DDRA = 0xFF;
ldi R24,255
out 0x1a,R24
.dbline 100
; PORTB = 0xFF;
out 0x18,R24
.dbline 101
; DDRB = 0xFF;
out 0x17,R24
.dbline 102
; PORTC = 0xF0; //m103 output only
ldi R24,240
out 0x15,R24
.dbline 103
; DDRC = 0x0F;
ldi R24,15
out 0x14,R24
.dbline 104
; PORTD = 0xFF;
ldi R24,255
out 0x12,R24
.dbline 105
; DDRD = 0x00;
out 0x11,R2
.dbline -2
L68:
.dbline 0 ; func end
ret
.dbend
.dbfunc e timer0_init _timer0_init fV
.even
_timer0_init::
.dbline -1
.dbline 113
; }
;
; //TIMER0 initialisation - prescale:64
; // WGM: Normal
; // desired value: 1mSec
; // actual value: 1.000mSec (0.0%)
; void timer0_init(void)
; {
.dbline 114
; TCCR0 = 0x00; //stop
clr R2
out 0x33,R2
.dbline 115
; TCNT0 = 0x83; //set count
ldi R24,131
out 0x32,R24
.dbline 116
; OCR0 = 0x7D; //set compare
ldi R24,125
out 0x3c,R24
.dbline 117
; TCCR0 = 0x03; //start timer
ldi R24,3
out 0x33,R24
.dbline -2
L69:
.dbline 0 ; func end
ret
.dbend
.area vector(rom, abs)
.org 36
jmp _timer0_ovf_isr
.area text(rom, con, rel)
.dbfile E:\AVRATM~1\key_board\keyboard.c
.dbfunc e timer0_ovf_isr _timer0_ovf_isr fV
.even
_timer0_ovf_isr::
st -y,R24
st -y,R25
in R24,0x3f
st -y,R24
.dbline -1
.dbline 122
; }
;
; #pragma interrupt_handler timer0_ovf_isr:10
; void timer0_ovf_isr(void)
; {
.dbline 123
; TCNT0 = 0x83; //reload counter value
ldi R24,131
out 0x32,R24
.dbline 124
; delay_counter ++;
lds R24,_delay_counter
lds R25,_delay_counter+1
adiw R24,1
sts _delay_counter+1,R25
sts _delay_counter,R24
.dbline 125
; key_counter ++;
lds R24,_key_counter
lds R25,_key_counter+1
adiw R24,1
sts _key_counter+1,R25
sts _key_counter,R24
.dbline 126
; scom_counter ++;
lds R24,_scom_counter
lds R25,_scom_counter+1
adiw R24,1
sts _scom_counter+1,R25
sts _scom_counter,R24
.dbline -2
L70:
ld R24,y+
out 0x3f,R24
ld R25,y+
ld R24,y+
.dbline 0 ; func end
reti
.dbend
.dbfunc e uart0_init _uart0_init fV
.even
_uart0_init::
.dbline -1
.dbline 135
; }
;
; //UART0 initialisation
; // desired baud rate: 9600
; // actual: baud rate:9615 (0.2%)
; // char size: 8 bit
; // parity: Disabled
; void uart0_init(void)
; {
.dbline 136
; UCSRB = 0x00; //disable while setting baud rate
clr R2
out 0xa,R2
.dbline 137
; UCSRA = 0x00;
out 0xb,R2
.dbline 138
; UCSRC = 0x86;
ldi R24,134
out 0x20,R24
.dbline 139
; UBRRL = 0x00; //set baud rate lo
out 0x9,R2
.dbline 140
; UBRRH = 0x00; //set baud rate hi
out 0x20,R2
.dbline 141
; UCSRB = 0x98;
ldi R24,152
out 0xa,R24
.dbline -2
L71:
.dbline 0 ; func end
ret
.dbend
.dbfunc e watchdog_init _watchdog_init fV
.even
_watchdog_init::
.dbline -1
.dbline 147
; }
;
; //Watchdog initialisation
; // prescale: 32K cycles
; void watchdog_init(void)
; {
.dbline 148
; WDR(); //this prevents a timout on enabling
wdr
.dbline 149
; WDTCR = 0x0A; //WATCHDOG ENABLED - dont forget to issue WDRs
ldi R24,10
out 0x21,R24
.dbline -2
L72:
.dbline 0 ; func end
ret
.dbend
.dbfunc e init_devices _init_devices fV
.even
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -