
; Created: 2021/11/30 10:47:59
; Author : kunio
;

; Replace with your application code
;**** includes **** 2021/10/17 ck=4Mhz
.include "m88pdef.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 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 subi16	;subi16	ra,rb,0xHHHH
                 subi	@1,low(@2)
                 sbci	@0,high(@2)
.ENDMACRO 
;*****************************
;* レジスタ変数　設定 *
;*****************************

.def	temp1	= r16
.def	temp2	= r17
.def	temp3	= r18
.def	temp4	= r19
.def	lcd_dat = r20

;*****************************
;*　 ビット　設定　　*
;*****************************
;**  PORTC  ***
;.equ			= 0
;.equ			= 1
;.equ			= 2
;.equ			= 3
;.equ			= 4
;.equ			= 5 
;.equ			= 7 
;**  PORTB  ***
.equ	t_dat	= 0	;DHT22
.equ	lcd_rs	= 1
.equ	lcd_rw	= 2
.equ	lcd_enb	= 3
.equ	lcd_db4 = 4
.equ	lcd_db5 = 5
.equ	lcd_db6 = 6
.equ	lcd_db7 = 7
;**  PORTD  ***
;.equ			= 0; Rx
;.equ			= 1; Tx
;.equ			= 2
;.equ			= 3
;.equ			= 4
;.equ			= 5 
;.equ			= 7 

.dseg
dsp_dat:	.byte 32

.cseg
	 	 rjmp	RESET   ;各種ﾘｾｯﾄ
	 	 reti  ;rjmp	 EXT_INT0	 ;外部割り込み要求0
	 	 reti  ;rjmp	 EXT_INT1	 ;外部割り込み要求1
	 	 reti  ;rjmp	 PCINT0		 ;ﾋﾟﾝ変化割り込み要求0
	 	 reti  ;rjmp	 PCINT1	 	;ﾋﾟﾝ変化割り込み要求1
	 	 reti  ;rjmp	 PC_INT2	 ;ﾋﾟﾝ変化割り込み要求2
	 	 reti  ;rjmp	 WDT_OVF	 ;ｳｫｯﾁﾄﾞｯｸﾞ計時完了
	 	 reti  ;rjmp	 TIM2_COMPA	 ;ﾀｲﾏ/ｶｳﾝﾀ2比較A一致
	 	 reti  ;rjmp	 TIM2_COMPB	 ;ﾀｲﾏ/ｶｳﾝﾀ2比較B一致
	 	 reti  ;rjmp	 TIM2_OVF	 ;ﾀｲﾏ/ｶｳﾝﾀ2溢れ
	 	 reti  ;rjmp	 TIM1_CAPT	 ;ﾀｲﾏ/ｶｳﾝﾀ1捕獲発生
	 	 reti  ;rjmp	 TIM1_COMPA	 ;ﾀｲﾏ/ｶｳﾝﾀ1比較A一致
	 	 reti  ;rjmp	 TIM1_COMPB	 ;ﾀｲﾏ/ｶｳﾝﾀ1比較B一致
	 	 reti  ;rjmp	 TIM1_OVF	 ;ﾀｲﾏ/ｶｳﾝﾀ1溢れ
	 	 reti  ;rjmp	 TIM0_COMPA	 ;ﾀｲﾏ/ｶｳﾝﾀ0比較A一致
	 	 reti  ;rjmp	 TIM0_COMPB	 ;ﾀｲﾏ/ｶｳﾝﾀ0比較B一致
	 	 reti  ;rjmp	 TIM0_OVF	 ;ﾀｲﾏ/ｶｳﾝﾀ0溢れ
	 	 reti  ;rjmp	 SPI_STC	 ;SPI転送完了
	 	 reti  ;rjmp	 USART_RXC	 ;USART 受信完了
	 	 reti  ;rjmp	 USART_UDRE	 ;USART 送信ﾊﾞｯﾌｧ空
	 	 reti  ;rjmp	 USART_TXC	 ;USART 送信完了
	 	 reti  ;rjmp	 ADC_	 	;ADC変換完了
		 reti  ;rjmp	 EE_RDY	 	;EEPROM操作可
	 	 reti  ;rjmp	 ANA_COMP	 ;ｱﾅﾛｸﾞ比較器出力遷移
	 	 reti  ;rjmp	 TWI	 	;2線直列ｲﾝﾀｰﾌｪｰｽ状態変化
	 	 reti  ;rjmp	 SPM_RDY	 ;SPM命令操作可
RESET:
	ldi	R16,HIGH(RAMEND)	;RAM最終ｱﾄﾞﾚｽ上位を取得
	out	SPH,R16	 ;ｽﾀｯｸ ﾎﾟｲﾝﾀ上位を初期化
	ldi	R16,LOW(RAMEND)	;RAM最終ｱﾄﾞﾚｽ下位を取得
	out	SPL,R16	 ;ｽﾀｯｸ ﾎﾟｲﾝﾀ下位を初期化
start:
	ldi	r16,0x80	;set cpu clk 4MHz
	sts	CLKPR,r16
	ldi	r16,0x01	;1/2		
	sts	CLKPR,r16

	ldi	temp1,0x00  ;pb1~7 clear		
	out	PORTB,temp1 ;(pb0)dht22bus in port,no pull up
	ldi	temp1,0xfe
	out	DDRB,temp1	; pb1~7 out port

	ldi	temp1,0b00000010
	out	DDRD,temp1	; pd0,2~7 in port
	ldi	temp1,0b11111100; pd2~7 pull up	
	out	PORTD,temp1

	ldi	temp1,0b00100000
	out	DDRC,temp1	; pc0~4,6,7 in port
	ldi	temp1,0b11011111	
	out	PORTC,temp1	; pc0~4,6,7  pull up	

; LCD - Initialize LCD controller
lcd_init:
	ldi	temp1,15	; wait for 15+ms
	rcall	wait_ms		; /
	rcall	lcd_int		; /
	ldi	temp1,5		; Wait for 4.1ms+
	rcall	wait_ms		; /
	rcall	lcd_int		; /
	ldi	temp1,100 	;wait for 100us+
	rcall	wait_us		; /
	rcall	lcd_int		; /
	ldi	lcd_dat,0x20
	rcall	lcd_cmd
	ldi	temp1,100 		;wait for 100us+
	rcall	wait_us			; /
	ldi	lcd_dat,0x28	; Data=4bit, Line=2, Font=5x7
	rcall	lcd_cmd			; /
	ldi	lcd_dat,0x0c	; Display=ON, Cursor=OFF, Blink=OFF
	rcall	lcd_cmd			; /
	ldi	lcd_dat,6		; Increment, No shift
	rcall	lcd_cmd			; /
	ldi	lcd_dat,1		; clear ,homepos
	rcall	lcd_cmd
	ldi	lcd_dat,0x81
 	rcall	lcd_cmd
	ldi	lcd_dat,'ｵ'
	rcall	lcd_dsp
	ldi	lcd_dat,'ﾝ'
	rcall	lcd_dsp
	ldi	lcd_dat,'ﾄ'
	rcall	lcd_dsp
	ldi	lcd_dat,'ﾞ'
	rcall	lcd_dsp
	ldi	lcd_dat,0x89
 	rcall	lcd_cmd
	ldi	lcd_dat,'ｼ'
	rcall	lcd_dsp
	ldi	lcd_dat,'ﾂ'
	rcall	lcd_dsp
	ldi	lcd_dat,'ﾄ'
	rcall	lcd_dsp
	ldi	lcd_dat,'ﾞ'
	rcall	lcd_dsp
USATR_int:
	stsi	UBRR0H,0		
	stsi	UBRR0L,12		;4MHz/[(12+1)x16]-->19200Hz
	stsi	UCSR0A,0		;受信完了フラグリセット
	stsi	UCSR0B,(1<<RXEN0)|(1<<TXEN0);RXEN0：受信許可、TXEN0：送信許可
	stsi	UCSR0C,(1<<UCSZ01)|(1<<UCSZ00);ビット長８bit、ストップビット１bit、非同期動作


main:
	rcall wait_1s
	rcall wait_1s
	rcall wait_1s
	tst r30
	brne main1
	inc r30
	ldi	lcd_dat,0x80
 	rcall	lcd_cmd
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	rjmp main2
main1:
	clr	r30
	ldi	lcd_dat,0x80
 	rcall	lcd_cmd
	ldi	lcd_dat,'*'
	rcall	lcd_dsp
main2:
;TIM1_COMPA:
	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
	breq	m_11
	rjmp	m_er
m_11:
	ldi	temp2,0x7f
	and	temp2,r13
	mov	temp1,r12
	rcall	bindec30

	ldi	lcd_dat,0xc0
	rcall	lcd_cmd
	tst	r29
	brmi	m_2
	ldi	lcd_dat,' '
	rjmp	m_3
m_2:
	ldi	lcd_dat,'-'
m_3:
	rcall	Tx	;fugo
	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	Tx	;10
	rcall	lcd_dsp	
	ldi	lcd_dat,0x30
	add	lcd_dat,r3
	rcall	Tx	;1
	rcall	lcd_dsp
	ldi	lcd_dat,'.'
	rcall	Tx
	rcall	lcd_dsp
	ldi	lcd_dat,0x30
	add	lcd_dat,r4
	rcall	Tx	;0.1
	rcall	lcd_dsp
	ldi	lcd_dat,0xdf
	rcall	Tx
	rcall	lcd_dsp
	ldi	lcd_dat,'C'
	rcall	Tx
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	Tx
	rcall	lcd_dsp

	mov	temp2,r15
	mov	temp1,r14
	rcall	bindec30
	tst	r2
	brne	m_6
	ldi	lcd_dat,' '
	rjmp	m_7
m_6:
	ldi	lcd_dat,0x30
	add	lcd_dat,r2
m_7:
	rcall	Tx	;10
	rcall	lcd_dsp	
	ldi	lcd_dat,0x30
	add	lcd_dat,r3
	rcall	Tx	;1
	rcall	lcd_dsp
	ldi	lcd_dat,'.'
	rcall	Tx
	rcall	lcd_dsp
	ldi	lcd_dat,0x30
	add	lcd_dat,r4
	rcall	Tx	;0.1
	rcall	lcd_dsp
	ldi	lcd_dat,'%'
	rcall	Tx
	rcall	lcd_dsp
	ldi	lcd_dat,'R'
	rcall	lcd_dsp
	ldi	lcd_dat,'H'
	rcall	lcd_dsp

	ldi r20,0x0A ;LF
	rcall	Tx	
	rjmp main

m_er:
	ldi	lcd_dat,0xc0
	rcall	lcd_cmd
	ldi	lcd_dat,'C'
	rcall	lcd_dsp
	ldi	lcd_dat,'R'
	rcall	lcd_dsp
	ldi	lcd_dat,'C'
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	ldi	lcd_dat,'E'
	rcall	lcd_dsp
	ldi	lcd_dat,'r'
	rcall	lcd_dsp
	ldi	lcd_dat,'r'
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	rjmp main
m_timeout:
	ldi	lcd_dat,0xc0
	rcall	lcd_cmd
	ldi	lcd_dat,'T'
	rcall	lcd_dsp
	ldi	lcd_dat,'i'
	rcall	lcd_dsp
	ldi	lcd_dat,'m'
	rcall	lcd_dsp
	ldi	lcd_dat,'e'
	rcall	lcd_dsp
	ldi	lcd_dat,'o'
	rcall	lcd_dsp
	ldi	lcd_dat,'u'
	rcall	lcd_dsp
	ldi	lcd_dat,'t'
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	ldi	lcd_dat,' '
	rcall	lcd_dsp
	rjmp	r_te


read_dht:
	sbi	DDRB,t_dat
	ldi 	temp1,2
	rcall 	wait_ms
	cbi	DDRB,t_dat
	sbi PORTC,5	;test
	ldi	temp1,10
	rcall	wait_us
r_t0:
	sbis	PINB,t_dat
	rjmp	r_t0
	clr r28
r_t1:
	inc r28
	ldi	temp1,5
	rcall	wait_us
	sbis	PINB,t_dat	;bus is "H" loop
	rjmp r_t2			;"L" next
	cpi	r28,20
	brne r_t1	; loop
	rjmp m_timeout ;100ms time out
r_t2:
	clr r27
r_t2a:
	inc r27
	ldi	temp1,5
	rcall	wait_us
	sbic	PINB,t_dat	;bus is "L" loop
	rjmp r_t3			;"H" next
	cpi	r27,20
	brne r_t2a	; loop
	rjmp m_timeout ; 100ms time out
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.5us
	ldi	temp1,33	;0:26-28us=18ck,1:70us=47ck 
	cp	temp1,temp3	;33> carry set="1", <33 no set="0"
	rol	r11
	rol	r12
	rol	r13
	rol	r14
	rol	r15
	dec	temp4
	brne	r_t4
r_te:
	sbis PINB,t_dat
	rjmp r_te
	cbi PORTC,5	;test
	ret

;-----------------------------------------------;
; LCD - Send a data/cmd into LCD
	;PORTB   7   6   5   4   3   2    1   0
	;write   <-data(H/L)->  enb r/w  r/s  -
	;read   BF   -   -   -   -   -    -   -  
 
lcd_int:	; Internal reset
	ldi	lcd_dat,0x30	; data='0011',RW = "L", RS = "L",E = "L"
	out	PORTB,lcd_dat	
	sbi	PORTB,lcd_enb	; E = "H"
	nop			; I/O delay
	nop			; I/O delay
	nop			; I/O delay
	cbi	PORTB,lcd_enb	; E = "L"
	ret

lcd_dsp:
	rcall	lcd_bfch
	ldi	temp3,0x02	; RW = "L",RS = "H",E = "L",data='xxxx'
	rjmp	lcd_cmd1
lcd_cmd:
	rcall	lcd_bfch
	ldi	temp3,0		; RW = "L",RS = "L",E = "L",data='xxxx'
lcd_cmd1:
	mov	temp2,lcd_dat
	swap	temp2
	andi	temp2,0xf0	;data_L save

	andi lcd_dat,0xf0   ;data_H set
	or	lcd_dat,temp3
	out	PORTB,lcd_dat	; PORTB = data_H,RW
	sbi	PORTB,lcd_enb	; E = "H"
	mov	lcd_dat,temp2	; 
	or	lcd_dat,temp3	;data_L set
	nop						
	cbi	PORTB,lcd_enb	; E = "L"
	nop			; I/O delay				
	out	PORTB,lcd_dat	; PORTB = data_L,RW
					
	sbi	PORTB,lcd_enb	; E = "H"
	nop			; I/O delay
	nop			; I/O delay
	nop			; I/O delay
	cbi	PORTB,lcd_enb	; E = "L"
	ret

lcd_bfch:
	cbi	DDRB,lcd_db7	; lcd_db7 = INPUT
	sbi	PORTB,lcd_db7	; pullup
	sbi	PORTB,lcd_rw	; RW = "H"
	cbi	PORTB,lcd_rs	; RS = "L"
lcd_bfch1:
	sbi	PORTB,lcd_enb	; E = "H"
	nop			; I/O delay
	nop			; I/O delay
	in	temp1,PINB
	cbi	PORTB,lcd_enb	; E = "L"
	nop
	nop
	sbi	PORTB,lcd_enb	; E = "H"
	nop			; I/O delay
	nop			; I/O delay
	in	temp2,PINB
	cbi	PORTB,lcd_enb	; E = "L"

	andi	temp1,0x80	; Wait until busy flag becomes zero.
	brne	lcd_bfch1
	sbi	DDRB,lcd_db7	; lcd_db7 = OUTPUT
	cbi	PORTB,lcd_db7	; clear
	ret

wait_us:	;base 4MHz	
				;temp1　us (4clocks)
	nop			; 1clocks
	dec	temp1		; 1clocks
	brne	wait_us		; 2clocks
	ret
	
wait_ms:	;temp1　ms at 4MHz
	ldi	temp2,50	;80*50=4000clocks
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
wait_1s:
	ldi	temp1,250
	rcall	wait_ms
	ldi	temp1,250
	rcall	wait_ms
wait_500ms:
	ldi	temp1,250
	rcall	wait_ms
	ldi	temp1,250
	rcall	wait_ms
	ret
;*********************************************
Tx:
	LDS  r0,UCSR0A
	SBRS r0,UDRE0	;送信ﾊﾞｯﾌｧ空きでｽｷｯﾌﾟ
	RJMP Tx 	;送信ﾊﾞｯﾌｧ空き待機
	STS  UDR0,r20	;ﾃﾞｰﾀ送信(送信開始)
	RET 		;呼び出し元へ復帰
Rx:
	LDS  r0,UCSR0A
	SBRS r0,RXC0	;受信完了でｽｷｯﾌﾟ
	RJMP Rx 	;受信完了待機
	LDS  R16,UDR0	;受信ﾃﾞｰﾀ取得
	RET		;呼び出し元へ復帰
;*********************************************
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
