📄 rtc.s
字号:
.module rtc.c
.area data(ram, con, rel)
_mon::
.blkb 2
.area idata
.byte 31,28
.area data(ram, con, rel)
.blkb 2
.area idata
.byte 31,30
.area data(ram, con, rel)
.blkb 2
.area idata
.byte 31,30
.area data(ram, con, rel)
.blkb 2
.area idata
.byte 31,31
.area data(ram, con, rel)
.blkb 2
.area idata
.byte 30,31
.area data(ram, con, rel)
.blkb 2
.area idata
.byte 30,31
.area data(ram, con, rel)
.dbfile G:\AVR系列\RTC\rtc.c
.dbsym e mon _mon A[12:12]c
.area text(rom, con, rel)
.dbfile G:\AVR系列\RTC\rtc.c
.dbfunc e putchar _putchar fV
; c -> R16
.even
_putchar::
.dbline -1
.dbline 21
; /******************************************/
;
; /* 目标MCU:MEGA8 晶振:内部RC(INT) 8MHZ */
; /******************************************/
; //说明:ATMEGA8只有在使用内部RC振荡器,T2使用异步时钟的
; // 情况下,外接的32.768KHZ的晶振才起作用。
; #include<iom8v.h>
; #include<macros.h>
; #define fosc 8000000
; #define baud 19200
; unsigned char hour;
; unsigned char minute;
; unsigned char second;
; unsigned char day;
; unsigned char month;
; unsigned int year;
; unsigned char mon[12]={31,28,31,30,31,30,31,31,30,31,30,31};
;
; /* 调整时间 */
; void putchar(unsigned char c)
; {
L2:
.dbline 22
L3:
.dbline 22
; while (!(UCSRA&(1<<UDRE)));
sbis 0xb,5
rjmp L2
.dbline 23
; UDR=c;
out 0xc,R16
.dbline -2
L1:
.dbline 0 ; func end
ret
.dbsym r c 16 c
.dbend
.dbfunc e getchar _getchar fc
.even
_getchar::
.dbline -1
.dbline 27
; }
; /* 字符输入函数 */
; unsigned char getchar(void)
; {
L6:
.dbline 28
L7:
.dbline 28
; while(!(UCSRA& (1<<RXC)));
sbis 0xb,7
rjmp L6
.dbline 29
; return UDR;
in R16,0xc
.dbline -2
L5:
.dbline 0 ; func end
ret
.dbend
.dbfunc e puts _puts fV
; s -> R20,R21
.even
_puts::
xcall push_gset1
movw R20,R16
.dbline -1
.dbline 33
; }
; /* 字符串输出函数 */
; void puts(char *s)
; {
xjmp L11
L10:
.dbline 35
.dbline 36
movw R30,R20
ldd R16,z+0
xcall _putchar
.dbline 37
subi R20,255 ; offset = 1
sbci R21,255
.dbline 38
L11:
.dbline 34
; while (*s)
movw R30,R20
ldd R2,z+0
tst R2
brne L10
.dbline 39
; {
; putchar(*s);
; s++;
; }
; putchar(0x0a);//回车换行
ldi R16,10
xcall _putchar
.dbline 40
; putchar(0x0d);
ldi R16,13
xcall _putchar
.dbline -2
L9:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r s 20 pc
.dbend
.dbfunc e uart_init _uart_init fV
.even
_uart_init::
.dbline -1
.dbline 44
; }
; /* UART初始化 */
; void uart_init(void)
; {
.dbline 46
; // OSCCAL=read_flash(osc_address);
; UCSRB=(1<<RXEN)|(1<<TXEN)|(1<<RXCIE);//允许发送和接收
ldi R24,152
out 0xa,R24
.dbline 47
; UBRRL=(fosc/16/(baud+1))%256;
ldi R24,26
out 0x9,R24
.dbline 48
; UBRRH=(fosc/16/(baud+1))/256;
clr R2
out 0x20,R2
.dbline 49
; UCSRC=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);//8位数据+1位STOP位
ldi R24,134
out 0x20,R24
.dbline -2
L13:
.dbline 0 ; func end
ret
.dbend
.dbfunc e time _time fV
.even
_time::
.dbline -1
.dbline 53
; }
;
; void time(void)
; {
.dbline 54
; if (second<60)
lds R24,_second
cpi R24,60
brsh L15
.dbline 55
; return;
xjmp L14
L15:
.dbline 57
; else
; {
.dbline 58
; second-=60;
lds R24,_second
subi R24,60
sts _second,R24
.dbline 59
; if (minute<59)
lds R24,_minute
cpi R24,59
brsh L17
.dbline 60
; minute++;
subi R24,255 ; addi 1
sts _minute,R24
xjmp L18
L17:
.dbline 62
; else
; {
.dbline 63
; minute=0;
clr R2
sts _minute,R2
.dbline 64
; if (hour<23)
lds R24,_hour
cpi R24,23
brsh L19
.dbline 65
; hour++;
subi R24,255 ; addi 1
sts _hour,R24
xjmp L20
L19:
.dbline 67
; else
; { hour=0;
.dbline 67
clr R2
sts _hour,R2
.dbline 68
; if(day<=mon[month-1])
ldi R24,<_mon-1
ldi R25,>_mon-1
lds R30,_month
clr R31
add R30,R24
adc R31,R25
ldd R2,z+0
lds R3,_day
cp R2,R3
brlo L21
.dbline 69
; { day++;
.dbline 69
mov R24,R3
subi R24,255 ; addi 1
sts _day,R24
.dbline 70
; if(((((year%4)==0)&&((year%100)!=0))||((year%400)==0))&&(month==4))
lds R24,_year
lds R25,_year+1
andi R24,3
andi R25,0
cpi R24,0
cpc R24,R25
brne L27
X0:
ldi R18,100
ldi R19,0
lds R16,_year
lds R17,_year+1
xcall mod16u
cpi R16,0
cpc R16,R17
brne L26
X1:
L27:
ldi R18,400
ldi R19,1
lds R16,_year
lds R17,_year+1
xcall mod16u
cpi R16,0
cpc R16,R17
breq X6
xjmp L22
X6:
X2:
L26:
lds R24,_month
cpi R24,4
breq X7
xjmp L22
X7:
.dbline 71
; mon[1]=28; }
ldi R24,28
sts _mon+1,R24
.dbline 71
xjmp L22
L21:
.dbline 73
; else
; {
.dbline 74
; day=0;
clr R2
sts _day,R2
.dbline 75
; if(month<13)
lds R24,_month
cpi R24,13
brsh L29
.dbline 76
; month++;
subi R24,255 ; addi 1
sts _month,R24
xjmp L30
L29:
.dbline 78
; else
; year++;
lds R24,_year
lds R25,_year+1
adiw R24,1
sts _year+1,R25
sts _year,R24
L30:
.dbline 79
; if((((year%4)==0)&&((year%100)!=0))||((year%400)==0))
lds R24,_year
lds R25,_year+1
andi R24,3
andi R25,0
cpi R24,0
cpc R24,R25
brne L34
X3:
ldi R18,100
ldi R19,0
lds R16,_year
lds R17,_year+1
xcall mod16u
cpi R16,0
cpc R16,R17
brne L33
X4:
L34:
ldi R18,400
ldi R19,1
lds R16,_year
lds R17,_year+1
xcall mod16u
cpi R16,0
cpc R16,R17
brne L31
X5:
L33:
.dbline 80
; mon[1]=29;
ldi R24,29
sts _mon+1,R24
L31:
.dbline 81
; }
L22:
.dbline 82
; }
L20:
.dbline 83
; }
L18:
.dbline 84
; }
.dbline -2
L14:
.dbline 0 ; func end
ret
.dbend
.dbfunc e timer2_init _timer2_init fV
.even
_timer2_init::
.dbline -1
.dbline 89
; }
;
; //初始化
; void timer2_init(void)
; {
.dbline 90
; TCCR2 = 0x00;
clr R2
out 0x25,R2
.dbline 91
; ASSR = 1<<AS2; //异步时钟
ldi R24,8
out 0x22,R24
.dbline 92
; TCNT2 = 0xE0; //定时时间1秒
ldi R24,224
out 0x24,R24
.dbline 93
; TCCR2 = (1<<CS22)|(1<<CS21)|(1<<CS20); //分频比1024,0x07
ldi R24,7
out 0x25,R24
.dbline -2
L36:
.dbline 0 ; func end
ret
.dbend
.area vector(rom, abs)
.org 16
jmp _timer2_ovf_isr
.area text(rom, con, rel)
.dbfile G:\AVR系列\RTC\rtc.c
.dbfunc e timer2_ovf_isr _timer2_ovf_isr fV
.even
_timer2_ovf_isr::
st -y,R24
st -y,R25
in R24,0x3f
st -y,R24
.dbline -1
.dbline 97
; }
; #pragma interrupt_handler timer2_ovf_isr:iv_TIMER2_OVF
; void timer2_ovf_isr(void)
; {
.dbline 98
; TCNT2 = 0xE0;
ldi R24,224
out 0x24,R24
.dbline 99
; second++;
lds R24,_second
subi R24,255 ; addi 1
sts _second,R24
.dbline -2
L37:
ld R24,y+
out 0x3f,R24
ld R25,y+
ld R24,y+
.dbline 0 ; func end
reti
.dbend
.dbfunc e put_time _put_time fV
.even
_put_time::
.dbline -1
.dbline 102
; }
; void put_time(void)
; {
.dbline 103
; putchar(year/1000+0x30);
ldi R18,1000
ldi R19,3
lds R16,_year
lds R17,_year+1
xcall div16u
subi R16,208 ; offset = 48
sbci R17,255
xcall _putchar
.dbline 104
; putchar(year/100+0x30);
ldi R18,100
ldi R19,0
lds R16,_year
lds R17,_year+1
xcall div16u
subi R16,208 ; offset = 48
sbci R17,255
xcall _putchar
.dbline 105
; putchar(year/10+0x30);
ldi R18,10
ldi R19,0
lds R16,_year
lds R17,_year+1
xcall div16u
subi R16,208 ; offset = 48
sbci R17,255
xcall _putchar
.dbline 106
; putchar(year%10+0x30);
ldi R18,10
ldi R19,0
lds R16,_year
lds R17,_year+1
xcall mod16u
subi R16,208 ; offset = 48
sbci R17,255
xcall _putchar
.dbline 107
; putchar(':');
ldi R16,58
xcall _putchar
.dbline 108
; putchar(month/10+0x30);
ldi R17,10
lds R16,_month
xcall div8u
subi R16,208 ; addi 48
xcall _putchar
.dbline 109
; putchar(month%10+0x30);
ldi R17,10
lds R16,_month
xcall mod8u
subi R16,208 ; addi 48
xcall _putchar
.dbline 110
; putchar(':');
ldi R16,58
xcall _putchar
.dbline 111
; putchar(day/10+0x30);
ldi R17,10
lds R16,_day
xcall div8u
subi R16,208 ; addi 48
xcall _putchar
.dbline 112
; putchar(day%10+0x30);
ldi R17,10
lds R16,_day
xcall mod8u
subi R16,208 ; addi 48
xcall _putchar
.dbline 113
; putchar(':');
ldi R16,58
xcall _putchar
.dbline 114
; putchar(hour/10+0x30);
ldi R17,10
lds R16,_hour
xcall div8u
subi R16,208 ; addi 48
xcall _putchar
.dbline 115
; putchar(hour%10+0x30);
ldi R17,10
lds R16,_hour
xcall mod8u
subi R16,208 ; addi 48
xcall _putchar
.dbline 116
; putchar(':');
ldi R16,58
xcall _putchar
.dbline 117
; putchar(minute/10+0x30);
ldi R17,10
lds R16,_minute
xcall div8u
subi R16,208 ; addi 48
xcall _putchar
.dbline 118
; putchar(minute%10+0x30);
ldi R17,10
lds R16,_minute
xcall mod8u
subi R16,208 ; addi 48
xcall _putchar
.dbline 119
; putchar(':');
ldi R16,58
xcall _putchar
.dbline 120
; putchar(second/10+0x30);
ldi R17,10
lds R16,_second
xcall div8u
subi R16,208 ; addi 48
xcall _putchar
.dbline 121
; putchar(second%10+0x30);
ldi R17,10
lds R16,_second
xcall mod8u
subi R16,208 ; addi 48
xcall _putchar
.dbline -2
L38:
.dbline 0 ; func end
ret
.dbend
.area vector(rom, abs)
.org 44
jmp _uart0_rx_isr
.area text(rom, con, rel)
.dbfile G:\AVR系列\RTC\rtc.c
.dbfunc e uart0_rx_isr _uart0_rx_isr fV
; i -> R20,R21
.even
_uart0_rx_isr::
xcall push_lset
xcall push_gset1
.dbline -1
.dbline 126
.dbline 128
in R20,0xc
clr R21
.dbline 129
cpi R20,116
ldi R30,0
cpc R21,R30
breq X8
xjmp L40
X8:
.dbline 130
.dbline 131
xcall _getchar
mov R18,R16
clr R19
ldi R16,1000
ldi R17,3
xcall empy16s
movw R24,R16
subi R24,128
sbci R25,187
sts _year+1,R25
sts _year,R24
.dbline 132
xcall _getchar
ldi R24,100
mul R24,R16
movw R24,R0
subi R24,192
sbci R25,18
sts _year+1,R25
sts _year,R24
.dbline 133
xcall _getchar
ldi R24,10
mul R24,R16
movw R24,R0
subi R24,224
sbci R25,1
sts _year+1,R25
sts _year,R24
.dbline 134
xcall _getchar
mov R24,R16
clr R25
sbiw R24,48
lds R2,_year
lds R3,_year+1
add R2,R24
adc R3,R25
sts _year+1,R3
sts _year,R2
.dbline 135
xcall _getchar
ldi R24,10
mul R24,R16
mov R24,R0
subi R24,224
sts _month,R24
.dbline 136
xcall _getchar
mov R24,R16
subi R24,48
lds R2,_month
add R2,R24
sts _month,R2
.dbline 137
xcall _getchar
ldi R24,10
mul R24,R16
mov R24,R0
subi R24,224
sts _day,R24
.dbline 138
xcall _getchar
mov R24,R16
subi R24,48
lds R2,_day
add R2,R24
sts _day,R2
.dbline 139
xcall _getchar
ldi R24,10
mul R24,R16
mov R24,R0
subi R24,224
sts _hour,R24
.dbline 140
xcall _getchar
mov R24,R16
subi R24,48
lds R2,_hour
add R2,R24
sts _hour,R2
.dbline 141
xcall _getchar
ldi R24,10
mul R24,R16
mov R24,R0
subi R24,224
sts _minute,R24
.dbline 142
xcall _getchar
mov R24,R16
subi R24,48
lds R2,_minute
add R2,R24
sts _minute,R2
.dbline 143
xcall _getchar
ldi R24,10
mul R24,R16
mov R24,R0
subi R24,224
sts _second,R24
.dbline 144
xcall _getchar
mov R24,R16
subi R24,48
lds R2,_second
add R2,R24
sts _second,R2
.dbline 145
L40:
.dbline -2
L39:
xcall pop_gset1
xcall pop_lset
.dbline 0 ; func end
reti
.dbsym r i 20 i
.dbend
.dbfunc e main _main fV
; second_old -> R20
.even
_main::
.dbline -1
.dbline 149
; }
; //UART接收中断
; #pragma interrupt_handler uart0_rx_isr:iv_USART_RXC
; void uart0_rx_isr(void)
; {
; unsigned i;
; i=UDR;
; if (i=='t')
; {
; year=(getchar()-0x30)*1000;
; year=(getchar()-0x30)*100;
; year=(getchar()-0x30)*10;
; year=year+(getchar()-0x30);
; month=(getchar()-0x30)*10;
; month=month+(getchar()-0x30);
; day=(getchar()-0x30)*10;
; day=day+(getchar()-0x30);
; hour=(getchar()-0x30)*10;
; hour=hour+(getchar()-0x30);
; minute=(getchar()-0x30)*10;
; minute=minute+(getchar()-0x30);
; second=(getchar()-0x30)*10;
; second=second+(getchar()-0x30);
; }
; }
; //main程序
; void main(void)
; {
.dbline 151
; unsigned char second_old;
; uart_init();
xcall _uart_init
.dbline 152
; timer2_init();
xcall _timer2_init
.dbline 153
; TIMSK =1<<TOIE2;
ldi R24,64
out 0x39,R24
.dbline 155
;
; SEI();
sei
xjmp L44
L43:
.dbline 157
; while(1)
; {
.dbline 158
; if (second!=second_old)
lds R2,_second
cp R2,R20
breq L46
.dbline 159
; {
.dbline 160
; time();
xcall _time
.dbline 161
; second_old=second;
lds R20,_second
.dbline 162
; put_time();
xcall _put_time
.dbline 163
; }
L46:
.dbline 164
L44:
.dbline 156
xjmp L43
X9:
.dbline -2
L42:
.dbline 0 ; func end
ret
.dbsym r second_old 20 c
.dbend
.area bss(ram, con, rel)
.dbfile G:\AVR系列\RTC\rtc.c
_year::
.blkb 2
.dbsym e year _year i
_month::
.blkb 1
.dbsym e month _month c
_day::
.blkb 1
.dbsym e day _day c
_second::
.blkb 1
.dbsym e second _second c
_minute::
.blkb 1
.dbsym e minute _minute c
_hour::
.blkb 1
.dbsym e hour _hour c
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -