;**** includes **** 2014/3/15 ck=4.8Mhz
.include "tn13adef.inc"

.macro	ldiw	;	ldiw	Z(X,Y),var
	ldi	@0L,low(@1)
	ldi	@0H,high(@1)
.endm
.macro	stsi
	ldi	r16,@1
	sts	@0,r16
.endm
.macro	sti
	ldi	r16,@1
	st	@0,r16
.endm
.MACRO subi16	;subi16	ra,rb,0xHHHH
                 subi	@1,low(@2)
                 sbci	@0,high(@2)
.ENDMACRO 
.MACRO cpi16	;cpi16	ra,rb,0xHHHH	;ra:rb-0xHHHH	set flags:N,C
		push	@0
		push	@1
		subi	@1,low(@2)
		sbci	@0,high(@2)
cp162:	pop	@1
		pop	@0
.ENDMACRO
.MACRO subi24	;subi24	ra,rb,rc,0xHHHHHH
                 subi	@2,low(@3)
                 sbci	@1,high(@3)
                 sbci	@0,byte3(@3)
.ENDMACRO 
.MACRO cpi24	;cpi24	ra,rb,rc,0xHHHHHH	;ra:rb:rc-0xHHHHHH	set flags:N,C
		push	@0
		push	@1
		push	@2
		subi	@2,low(@3)
		sbci	@1,high(@3)
		sbci	@0,byte3(@3)
		pop	@2
		pop	@1
		pop	@0
.ENDMACRO
;*****************************
;* レジスタ変数　設定 *
;*****************************

.def	temp1	= r16
.def	temp2	= r17
.def	temp3	= r18
.def	temp4	= r19
.def	lcd_dat	= r20


;*****************************
;*　 ビット　設定　　*
;*****************************

;**  PORTB  ***
.equ	scl		= 0	;sb0802g
.equ	sda		= 1
.equ	t_dat	= 2	;DHT22
;.equ   		= 3
;.equ			= 4
 

.dseg
rtc_dat:	.byte 7

;.eseg	;EEPROM add
.cseg
;	rjmp	start	 ;各種ﾘｾｯﾄ
;	reti	;RJMP	 EXT_INT0	 ;外部割り込み要求0
;	reti	;RJMP	 PCINT0	 ;ﾋﾟﾝ変化割り込み要求0
;	reti	;RJMP	 TIM0_OVF	 ;ﾀｲﾏ/ｶｳﾝﾀ溢れ
;	reti	;RJMP	 EE_RDY	 ;EEPROM操作可
;	reti	;RJMP	 ANA_COMP	 ;ｱﾅﾛｸﾞ比較器出力遷移
;	reti	;RJMP	 TIM0_COMPA	 ;ﾀｲﾏ/ｶｳﾝﾀ比較A一致
;	reti	;RJMP	 TIM0_COMPB	 ;ﾀｲﾏ/ｶｳﾝﾀ比較B一致
;	reti	;RJMP	 WATCHDOG	 ;ｳｫｯﾁﾄﾞｯｸﾞ計時完了
;	reti	;RJMP	 ADC	 ;A/D変換完了
  
start:
	ldi	 R16,LOW(RAMEND)	;RAM最終ｱﾄﾞﾚｽ下位を取得
	out	 SPL,R16	;ｽﾀｯｸ ﾎﾟｲﾝﾀ(下位)を初期化

;	ldi	r16,0x80	;分周1、set 4.8MHz
;	sts	CLKPR,r16
;	ldi	r16,0x01
;	sts	CLKPR,r16
;	ldi	r16,120
;	sts OSCCAL,r16


i_o_reset:

	ldi	temp1,0b00011111	
	out	PORTB,temp1	;0-2 H,3-4 pull up
	ldi	temp1,0b00000111
	out	DDRB,temp1	;0-2 out,3-4 in port


; LCD - Initialize LCD controller
lcd_init:
	ldi	temp1,50	; 
	rcall	wait_ms
	ldi	lcd_dat,0b00111000	;function set 
	rcall	lcd_com			;Data=8bit, Line=2, Font=5x7
	ldi	lcd_dat,0b00111001	;function set2 	
	rcall	lcd_com			 ;Data=8bit, Line=2,Font=5x7,IS
	ldi	lcd_dat,0b00010100	;Internal OSC frequency adjustment  
	rcall	lcd_com
	ldi	lcd_dat,0b01111011	;Contrast set 
	rcall	lcd_com		
	ldi	lcd_dat,0b01010101	;Power/ICON/Contrast control
	rcall	lcd_com
	ldi	lcd_dat,0b01101010	;Follower control
	rcall	lcd_com			
;	ldi	temp1,220		; Wait for 200ms+
	rcall	wait_250ms

	ldi	lcd_dat,0b00111000	;function set 
	rcall	lcd_com			;Data=8bit, Line=2, Font=5x7
	ldi	lcd_dat,0b00001100	;Display ON/OFF
	rcall	lcd_com		
	ldi	lcd_dat,0b00000001	;Clear Display 
	rcall	lcd_com			
	ldi	temp1,3			; Wait for 2ms+
	rcall	wait_ms
	ldi	lcd_dat,0b00000110	;Entry Mode Set 
	rcall	lcd_com	
			
;	ldi	lcd_dat,0x82
;	rcall	lcd_com
;	ldi	lcd_dat,'O'
;	rcall	lcd_dsp
;	ldi	lcd_dat,'K'
;	rcall	lcd_dsp


	;rtc_Initialize 
;	rcall wait_1s
;	rcall	rtc_w
;	ldi r21,17
;	ldiw z,rtc_int*2
rtc_0:
;	lpm	temp3,z+		
;	rcall	send_dat
;	dec r21
;	brne rtc_0
;	rcall	stop_c
;	rcall time_start

main:
	rcall	wait_500ms
	rcall	rtc_r
	ldi	lcd_dat,0x80
	rcall	lcd_com

	rcall rtc_set1
	andi	temp1,0x0f
	rcall rtc_set2
	ldi	lcd_dat,'/'
	rcall	lcd_dsp

	rcall rtc_set1
	andi	temp1,0x01
	rcall rtc_set2
	ldi	lcd_dat,'/'
	rcall	lcd_dsp
	ld	r15,-z

	rcall rtc_set1
	andi	temp1,0x03
	rcall rtc_set2
	ldi lcd_dat,' '
	rcall	lcd_dsp

	rcall rtc_set1
	andi	temp1,0x03
	rcall rtc_set2
	ldi	lcd_dat,':'
	rcall	lcd_dsp

	rcall rtc_set1
	andi	temp1,0x07
	rcall rtc_set2

	rcall week_dsp

	rcall	wait_1s
	rcall	read_dht
	clr	temp1
	clr	r29
	add	temp1,r13
	brpl	m_1
	ser	r29
m_1:
	add	temp1,r12
	add	temp1,r15
	add	temp1,r14
	cp	temp1,r11
	brne	m_er
	ldi	temp2,0x7f
	and	temp2,r13
	mov	temp1,r12
	rcall	bindec30

	tst	r29
	brmi	m_2
	ldi	lcd_dat,' '
	rjmp	m_3
m_2:
	ldi	lcd_dat,'-'
m_3:
	rcall	lcd_dsp
	tst	r2
	brne	m_4
	ldi	lcd_dat,' '
	rjmp	m_5
m_4:		
	ldi	lcd_dat,0x30
	add	lcd_dat,r2
m_5:
	rcall	lcd_dsp	
	ldi	lcd_dat,0x30
	add	lcd_dat,r3
	rcall	lcd_dsp
	ldi	lcd_dat,'.'
	rcall	lcd_dsp
	ldi	lcd_dat,0x30
	add	lcd_dat,r4
	rcall	lcd_dsp
	ldi	lcd_dat,0xdf
	rcall	lcd_dsp
	ldi	lcd_dat,'C'
	rcall	lcd_dsp

	mov	temp2,r15
	mov	temp1,r14
	rcall	bindec40
	tst r1
	brne	m_51
	ldi	lcd_dat,' '
	rjmp	m_52
m_51:
	ldi	lcd_dat,'1'
m_52:
	rcall	lcd_dsp
	or	r1,r2
	brne	m_6
	ldi	lcd_dat,' '
	rjmp	m_7
m_6:
	ldi	lcd_dat,0x30
	add	lcd_dat,r2
m_7:
	rcall	lcd_dsp	
	ldi	lcd_dat,0x30
	add	lcd_dat,r3
	rcall	lcd_dsp
	ldi	lcd_dat,'.'
	rcall	lcd_dsp
	ldi	lcd_dat,0x30
	add	lcd_dat,r4
	rcall	lcd_dsp
	ldi	lcd_dat,'%'
	rcall	lcd_dsp
	sbic	PINB,4
	rjmp main
	ldi temp1,50
	rcall	wait_ms
	sbis	PINB,4
	rcall time_adj
m_er:
	rjmp main

time_adj:
	rcall	wait_250ms
	ldiw x,rtc_dat+7
	rcall	time_stop
	ldi	lcd_dat,0b00001101
	rcall	lcd_com
t_a_y:
	ldi	lcd_dat,0x80
	rcall	lcd_com
	rcall time_set
t_a_m:
	ldi	lcd_dat,0x83
	rcall	lcd_com
	rcall time_set
t_a_w:
	ldi	lcd_dat,0xc0
	rcall	lcd_com
	rcall key_in
	st	-x,r22
	mov r15,r22
	rcall week_dsp1

t_a_d:
	ldi	lcd_dat,0x86
	rcall	lcd_com
	rcall time_set
t_a_h:
	ldi	lcd_dat,0x89
	rcall	lcd_com
	rcall time_set
t_a_mi:
	ldi	lcd_dat,0x8c
	rcall	lcd_com
	rcall time_set
t_a_s:
	clr r22
	st	-x,r22
	rcall	rtc_w
	ldi r21,7
	ldi	temp3,0b00000010	;seconds
	rcall	send_dat
t_a_e:
	ld	temp3,x+		
	rcall	send_dat
	dec r21
	brne t_a_e
	rcall	stop_c

	rcall time_start
	ldi	lcd_dat,0b00001100
	rcall	lcd_com
	ret

week_dsp:
	ldi	lcd_dat,0xc0
	rcall	lcd_com
week_dsp1:
	ldi temp1,0x07
	and	temp1,r15
	mov r15,temp1
	lsl	temp1
	add	temp1,r15
	clr	temp2
	ldiw	z,week*2
	add	zl,temp1
	adc	zh,temp2
	lpm lcd_dat,z+
	rcall	lcd_dsp
	lpm lcd_dat,z+
	rcall	lcd_dsp
	lpm lcd_dat,z+
	rcall	lcd_dsp
	ret

time_set:
	rcall	key_in
	cpi	r22,10
	brcs time_set1
	sbiw x,1
	ret
 time_set1:
	ldi	lcd_dat,0x30
	add	lcd_dat,r22
	rcall	lcd_dsp
	mov r23,r22
	rcall	key_in
	ldi	lcd_dat,0x30
	add	lcd_dat,r22
	rcall	lcd_dsp
	swap r23
	or r22,r23
	st	-x,r22
	ret

key_in:
	ldi	temp1,0b00000000
	out	DDRB,temp1	;0-4 in port
key_in00:
	sbis PINB,4
	rjmp key_in00
key_in0:
	sbic	PINB,4
	rjmp	key_in0
	ldi	temp1,10
	rcall wait_ms
	in	r22,PINB
	com	r22
	andi	r22,0x0f
	ldi	temp1,100
	rcall wait_ms
	sbis	PINB,4
	rjmp	key_ine
	rjmp	key_in0
key_ine:
	in	temp1,PINB
	andi	temp1,0x0f
	cpi		temp1,0x0f
	brne	key_ine
	ldi temp1,200
	rcall	wait_ms
	ldi	temp1,0b00000111
	out	DDRB,temp1	;0-2 out,3-4 in port
	rcall	stop_c
	ret


	
time_start:
	rcall	rtc_w
	ldi	temp3,0		;rtc add 0x00
	rcall	send_dat
	ldi	temp3,0b00000000	;control1
	rcall	send_dat
	rcall	stop_c
	ret

time_stop:
	rcall	rtc_w
	ldi	temp3,0		;rtc add 0x00
	rcall	send_dat
	ldi	temp3,0b00100000	;control1
	rcall	send_dat
	rcall	stop_c
	ret

rtc_set1:
	ld	temp1,-z
	ldi	r21,0x0f
	and r21,temp1
	swap	temp1
	ret
rtc_set2:
	ldi lcd_dat,0x30
	add	lcd_dat,temp1
	rcall	lcd_dsp
	ldi lcd_dat,0x30
	add	lcd_dat,r21
	rcall	lcd_dsp
	ret

read_dht:
	cbi	PORTB,t_dat
	ldi 	temp1,10
	rcall 	wait_ms
	cbi	DDRB,t_dat
	ldi	temp1,10
	rcall	wait_us
r_t0:
	sbis	PINB,t_dat
	rjmp	r_t0
r_t1:
	sbic	PINB,t_dat
	rjmp	r_t1
	ldi	temp1,40
	rcall	wait_us
r_t2:
	sbis	PINB,t_dat
	rjmp	r_t2
	ldi	temp1,40
	rcall	wait_us
r_t3:
	sbic	PINB,t_dat
	rjmp	r_t3
	ldi temp4,40
r_t4:
	ldi	temp1,10
	rcall	wait_us
	clr	temp3
r_t5:
	sbis	PINB,t_dat
	rjmp	r_t5
r_t6:		
	inc	temp3	;1ck	
	sbic	PINB,t_dat	;3ck
	rjmp	r_t6	;2ck ,6ck about 1.3us
	ldi	temp1,35	;0:26-28us=22ck,1:70us=56ck 
	cp	temp1,temp3	;35> carry set="1", <35 no set="0"
	rol	r11
	rol	r12
	rol	r13
	rol	r14
	rol	r15
	dec	temp4
	brne	r_t4
	sbi	PORTB,t_dat
	sbi	DDRB,t_dat
	ret

start_c:
	sbi	PORTB,scl
	ldi	temp1,2
	rcall	wait_us
	cbi	PORTB,sda
	ldi	temp1,3
	rcall	wait_us
	ret
stop_c:
	cbi	PORTB,scl
	ldi	temp1,2
	rcall	wait_us
	cbi	PORTB,sda
	ldi	temp1,2
	rcall	wait_us
	sbi	PORTB,scl
	ldi	temp1,5
	rcall	wait_us
	sbi	PORTB,sda
	ret

send_dat0:	;re_start
	ser r29
	rjmp s_d0
send_dat:
	clr r29
s_d0:
	ldi	temp4,8
s_d1:
	cbi	PORTB,scl
	ldi	temp1,1
	rcall	wait_us	
	lsl	temp3
	brcs	s_d2
	cbi	PORTB,sda
	rjmp	s_d3
s_d2:
	sbi	PORTB,sda
s_d3:
	ldi	temp1,1
	rcall	wait_us
	sbi	PORTB,scl
	ldi	temp1,4
	rcall	wait_us	
	dec	temp4
	brne	s_d1
s_d4:
	cbi	PORTB,scl
	nop
	cbi	DDRB,sda	;input
	ldi	temp1,3
	rcall	wait_us

	tst r29
	brne s_d5
	cbi	PORTB,sda
	rjmp s_d6
s_d5:
	sbi	PORTB,scl
	ldi	temp1,4
	rcall	wait_us
	cbi	PORTB,scl
	sbi	PORTB,sda
	sbi	DDRB,sda	;output
	ldi	temp1,3
	rcall	wait_us
	ret
s_d6:
	sbi	DDRB,sda	;output
	sbi	PORTB,scl
	ldi	temp1,3
	rcall	wait_us

	ret

read_dat:
	ldi	temp4,8
	clr temp3
r_d1:
	cbi	PORTB,scl
	ldi	temp1,4
	rcall	wait_us
	cbi	DDRB,sda	;input	
	sbi	PORTB,scl
	ldi	temp1,2
	rcall	wait_us
	clc
	sbic PINB,sda
	sec
	rol	temp3
	ldi	temp1,1
	rcall	wait_us
	dec	temp4
	brne	r_d1

	cbi	PORTB,scl
	ldi	temp1,1
	rcall	wait_us
	cbi	PORTB,sda
	sbi	DDRB,sda	;output

	tst r29
	breq r_d2
	sbi	PORTB,sda
r_d2:
	ldi	temp1,2
	rcall	wait_us
	sbi	PORTB,scl
	ldi	temp1,3
	rcall	wait_us
	ret

rtc_r:
	ldiw z,rtc_dat
	ldi r28,6
	clr	r29
	rcall rtc_w
;	rcall	start_c
;	ldi	temp3,0b10100010	;SLA_W
;	rcall	send_dat
	ldi	temp3,0b00000010	;seconds
	rcall	send_dat0	
	rcall	start_c	;re_start
	ldi	temp3,0b10100011	;SLA_R
	rcall	send_dat
rtc_r1:
	rcall	read_dat
	st	z+,temp3
	dec	r28
	brne rtc_r1
	ser r29
	rcall read_dat
	st	z+,temp3
	rcall	stop_c
	ret

rtc_w:
	rcall	start_c
	ldi	temp3,0b10100010	;SLA_W
	rcall	send_dat
	ret

lcd_dsp:
	ldi	r24,0x40
	rjmp lcd_com1
lcd_com:
	ldi	r24,0x00
lcd_com1:
	rcall	start_c
	ldi	temp3,0b01111100	;SLA_W
	rcall	send_dat

	mov	temp3,r24	
	rcall send_dat

	mov	temp3,lcd_dat
	rcall send_dat
	rcall	stop_c
	ldi	temp1,50	; Wait for 26.3us+
	rcall	wait_us	
	ret
wait_1s:
	ldi	temp1,250
	rcall	wait_ms
	ldi	temp1,250
	rcall	wait_ms
wait_500ms:
	ldi	temp1,250
	rcall	wait_ms
wait_250ms:
	ldi	temp1,250
	rcall	wait_ms
	ret

;*********************************************
wait_us:	;base 4.8MHz	
				;temp1　us (5clocks)
	nop			; 1clocks
	nop			; 1clocks
	dec	temp1		; 1clocks
	brne	wait_us		; 2clocks
	ret
;*********************************************	
wait_ms:	;temp1　ms at 4.8MHz
	ldi	temp2,60	;80*60=4800clocks
wait_ms_01:
	ldi	temp3,19    	;1clocks  4*(19+1)=80
wait_ms_02:
	nop			; 1clocks
	dec	temp3		; 1clocks
	brne	wait_ms_02	; 2clocks
	dec	temp2		; 1clocks
	brne	wait_ms_01	; 2clocks
	dec	temp1		; 1clocks	
	brne	wait_ms		; 2clocks
	ret


;***************************************************************************
;*
;* "bindec" - 16-bit Binary to dec 
;*  bin:=temp2:temp1  dec=r1,r2,r3,r4
;***************************************************************************
bindec40:	;>999
	clr	r1
bindec41:
	cpi16	temp2,temp1,1000
	brcs	bindec30
	subi16	temp2,temp1,1000
	inc	r1
	rjmp	bindec41
bindec30:	;>99
	clr	r2
bindec31:
	cpi16	temp2,temp1,100
	brcs	bindec20
	subi16	temp2,temp1,100
	inc	r2
	rjmp	bindec31
bindec20:	;>9
	clr	r3
bindec21:
	cpi	temp1,10
	brcs	bindec10
	subi	temp1,10
	inc	r3
	rjmp	bindec21
bindec10:	;<10
;	clr	r4
	mov	r4,temp1
	ret
		   		;rtc add;control1  ;control2  ;seconds	;minutes	;hours    ;days	     ;weeks		
rtc_int: .db	0x00,	0b00100000,0b00000000,0b00000000,0b00000000,0b0010100,0b00010011,0b00000001;
				;months   ;years	 ;min alarm ;hour alarm;day alarm ;week alarm;clokout;timer control;timer
rtc_int0: .db	0b00001000,0b00010010,0b10000000,0b10000000,0b10000000,0b10000000,0b00000000,0b00000000,0b00000000,0
week: .db 	'S','u','n','M','o','n','T','u','e','W','e','d','T','h','u','F','r','i','S','a','t',0
