3 ; Copyright (C) 2010 Leo C.
5 ; This file is part of avrcpm.
7 ; avrcpm is free software: you can redistribute it and/or modify it
8 ; under the terms of the GNU General Public License as published by
9 ; the Free Software Foundation, either version 3 of the License, or
10 ; (at your option) any later version.
12 ; avrcpm is distributed in the hope that it will be useful,
13 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ; GNU General Public License for more details.
17 ; You should have received a copy of the GNU General Public License
18 ; along with avrcpm. If not, see <http://www.gnu.org/licenses/>.
27 .equ ot_timer1 = delay_timer1-timer_var
30 .equ ot_timer2 = delay_timer2-timer_var
34 .equ ot_ms = timer_ms-timer_var
37 .equ ot_sec = timer_sec-timer_var
40 ; don't change order here, clock put/get depends on it.
41 cntms_out: ; register for ms
43 utime_io: ; register for uptime.
46 .equ ot_1ms = cnt_1ms-timer_var
49 .equ ot_uptime = uptime-timer_var
52 .equ timer_size = timer_top - timer_base
54 .equ utofs = cnt_1ms-cntms_out
55 .equ timerofs = cnt_1ms-timer_ms
58 .equ oclk_out = clk_out-timer_var
61 .equ o_clock = clock-timer_var
62 .byte 7 ;Format (bin): s m h D M YY
63 .equ clkofs = clock-clk_out
67 ; ------------- system timer 1ms ---------------
70 ; Timer/Counter1 Compare Match B interrupt
91 #if DRAM_8BIT /* Implies software uart */
92 lds zl,srx_char_to ;try to decrement character timout
94 brcs syscl0 ;timer was 0 before (not running)
95 sts srx_char_to,zl ;timer is running, store new value
111 ldd zl,y+ot_1ms ;count milli seconds
116 cpi zl,low(1000) ;one second ?
117 ldi zl,high(1000) ;doesn't change flags
140 ldd zl,y+o_clock+0 ;sec
146 ldd zl,y+o_clock+1 ;min
152 ldd zl,y+o_clock+2 ;hour
162 ldiw z,dayspm_tab*2 - 1
163 ldd temp,y+o_clock+4 ;month
166 lpm zh,z ;days this month
168 brne syscl_clknl ;february, may be leap year
169 ldd zl,y+o_clock+5 ;year
174 ldd zl,y+o_clock+3 ;day
187 ldd zl,y+o_clock+5 ;low year
188 ldd zh,y+o_clock+6 ;high year
210 .db 31,28,31,30,31,30
211 .db 31,31,30,31,30,31
213 ; ----------------------------------------------
220 sts delay_timer1,temp
222 lds temp,delay_timer1
227 ; ----------------------------------------------
232 breq clkget_copy ;lowest byte requestet, latch clock
265 ret ;req. byte in temp
267 ; ----------------------------------------------
275 breq clkput_copy ;lowest byte stored, latch clock
304 rcall rtc_set ; set hardware clock
309 ; ----------------------------------------------
311 ; convert binary to bcd
312 ; (only range 0 - 99)
359 ; convert bcd to binary
363 mov temp2,temp ;temp2 = high digit
366 andi temp,0x0f ;temp = low digit
369 mul temp2,_tmp0 ;high digit * 10
370 add temp,_tmp0 ;high digit * 10 + low digit
390 ; ----------------------------------------------
391 ; Set software clock from hardware clock
394 push _0 ;Placeholder for month/weekday
398 ldi temp,0x10 ;register address
399 push temp ;save reg adr/placeholder for sec
402 ldi temp,0xA0 ;PCF8583 slave address
408 rcall i2c_read ;get year (stored in RTC-RAM addr. 10h)
410 brmi rtc_get_e ;i2c error
412 ldd temp3,z+1 ;save year
415 ldi temp2,2 ;register pointer. 2 = secs
423 ldd temp,z+4 ;get year
436 ldd temp,z+1 ;get seconds
439 ldd temp,z+2 ;get minutes
442 ldd temp,z+3 ;get hours
445 ldd temp,z+4 ;get day
449 ldd temp,z+5 ;get months
452 st x+,temp ;store month
453 st x+,temp3 ;store year
454 st x+,temp4 ;store year century
466 ;----------------------------------------------
467 ; Set hardware clock from software clock
489 push temp ;-1 save month
492 mov xl,yl ; least significant 2 bits of year
497 or temp,xl ; combine with day
498 push temp ;-2 save year/day
501 push temp ;-3 save hours
504 push temp ;-4 save min
507 push temp ;-5 save sec
509 push _0 ;-6 1/10s, 1/100s
510 ldi temp,0x84 ; stop count, alarm enable
512 push _0 ;-8 register address
515 ldi temp,0xA0 ; PCF8583 slave address
520 ldi temp,0x04 ;enable counting
524 std z+3,yh ;store year in RTC RAM
531 addiw z,8 ;remove buffer from stack
540 ; ----------------------------------------------
547 brcs utimget_end ;Rel. port number = 0 ? (controlport)
548 breq utimget_copy ;lowest byte requestet, latch clock
575 cpi temp,starttimercmd
576 breq timer_start ;timer_ms
577 cpi temp,quitTimerCmd
579 cpi temp,printTimerCmd
580 breq timer_print ;timer_ms
583 rjmp uptime_print ;cnt_1ms
593 breq utput__copy ;lowest byte requestet, latch clock
636 ; put ms on stack (16 bit)
671 printstring "Timer running. Elapsed: "
701 printstring "Uptime: "
715 ; vim:set ts=8 noet nowrap