;************************************************************************
;*									*
;*			dcf77 receiver, decoder				*
;*									*
;************************************************************************
.nolist
.include "tiny22.h"
.include "dcf77.h"
.include "macros.mac"
;.device AT90S8515			;support STS, LDS
;.list
;.listmac

	rjmp	reset

.org	ovf0addr
.include "timer0.inc

.list
.listmac
;------------------------------------------------------------------------

reset:
	ldi	stx_state, 5			;about 5 seconds delay
_rst1:
;	clr	stx_state
;	clr	t10ms1_reload
;	clr	zero				;  0
	mov	one, zero			; +1
	mov	ff, t10ms1_reload		; -1
	ldi	a, ramend			;initialize stack
	out	spl, a
	ldi	a, 1<<toie0			;enable timer interrupt
	out	timsk, a
	ldi	a, 1<<LED0^1<<led1^1<<stxd
	out	ddrb, a
	ldi	a, 1<<dcf77^1<<srxd^1<<stxd
	bst	zero, 5				;toggle LED fast
	bld	a, led0
	out	portb, a			;inputs with pull up

	inc	t10ms1_reload
	brne	_rst1
	djnz	zero, _rst1
	djnz	stx_state, _rst1
	sei					;enable interrupts

;------------------------------------------------------------------------

	rcall	sync_timer
	rcall	checkbit

	rcall	puts
	.db	_cr, "RC-Frequency: ", 0

	mov	a_op1_l, t10ms0_reload
	mov	a_op1_h, t10ms1_reload
	rcall	dec_out
	rcall	puts
	.db	"00Hz", _cr, 0

	ldi	second, 60
	ldi	zl, 0x60
_rst2:	st	z+, ff
	mifne	zl, ramend, _rst2

main:
	mifne	second, 58, main2
main1:
	mifne	t10ms_count, 110, main1		;count 59. second
	inc	second
	rjmp	main3
main2:
	rcall	checkbit			;return 0, 1 or 3 (error)
main3:
	rcall	gettime
	mifne	second, 0, main5
	ldi	xl, nme				;copy previous reading
	ldi	yl, cme				;to output buffer
main4:	ld	a, x+
	st	y+, a
	mifne	xl, nyz + 1, main4
main5:
	rcall	time_out
	rjmp	main

;------------------------------------------------------------------------
;.nolist
.include "sync.inc"
.include "udiv16.inc"
.include "uart_out.inc"
.include "dec_out.inc"
.include "checkbit.inc"
.include "gettime.inc"
.include "time_out.inc"
;------------------------------------------------------------------------
