;**** includes ****
.include "8535def.inc"
.MACRO	mul16	;mul16 ra,rb,rc,rd  ;r0:r1:r2:r3=ra:rb*rc:rd
		;work	r4

	push	@2
	push	@3
	push	@0
	push	@1
	clr	r0
	clr	r1
	clr	r2
	clr	r3
	clr	r4
A_BL:
	lsr	@3
	brcc	A_BL1
	add	r3,@1
	adc	r2,@0
	adc	r1,r4
A_BL1:
	tst	@3
	breq	A_BH
	lsl	@1
	rol	@0
	rol	r4
	rjmp	A_BL
	
A_BH:
	pop	@1
	pop	@0
	pop	@3
	push	@3
	push	@0
	push	@1		
	clr	r4	
A_BH1:
	lsr	@2
	brcc	A_BH2
	add	r2,@1
	adc	r1,@0
	adc	r0,r4
A_BH2:
	tst	@2
	breq	mul16e
	lsl	@1
	rol	@0
	rol	r4
	rjmp	A_BH1
mul16e:
	pop	@1
	pop	@0
	pop	@3
	pop	@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
;*****************************
;* レジスタ変数　設定 *
;*****************************
.def	C1	=r11
.def	BZ	=r12
.def	F	=r13
.def	D	=r14
.def	H 	=r15

.def	AH	= r16
.def	AL	= r17
.def	B1	= r18
.def	B2	= r19
.def	B3	= r20
.def	B4	= r21
.def	temp1	= r22
.def	temp2	= r23
.def	temp3	= r24
.def	temp4	= r25
.def	lcd_data =r26	

;*****************************
;*　 ビット　設定　　*
;*****************************

;**  PORTB  ***
.equ	lcd_db0 = 0
.equ	lcd_db1 = 1
.equ	lcd_db2 = 2
.equ	lcd_db3 = 3
.equ	lcd_db4 = 4
.equ	lcd_db5 = 5
.equ	lcd_db6 = 6
.equ	lcd_db7 = 7

;**  PORTD  **
.equ	lcd_rs	= 0
.equ	lcd_rw	= 1
.equ	lcd_enb	= 2
.equ	bzout	= 3
;*********************
;* 割込みベクター *
;*********************
.cseg

	rjmp	start		; Reset Handle
;.org 0x08
;	rjmp	TIM1_OVF
	
.org 0x20

;*********************
;* メイン プログラム *
;*********************

start:
	ldi	temp1,low(RAMEND)
	out	SPL,temp1
	ldi	temp1,high(RAMEND)
	out	SPH,temp1	;init Stack Pointer (remove for AT90Sxx0x)
	

	ldi	temp1,0xff
	out	DDRB,temp1	; pb0~7 out port
	ldi	temp1,0	
	out	PORTB,temp1	; pb0~7 clear
	ldi	temp1,15
	out	DDRD,temp1	; pd0~3 out port
	ldi	temp1,0xf0	; pd0~3 clear	
	out	PORTD,temp1	; pd4~7 pull up
	ldi	temp1,0x0f

	clr	BZ

; LCD - Initialize LCD controller

lcd_init:
	ldi	temp1,20	; wait for 15ms
	rcall	wait_ms		; /
	ldi	lcd_data,0x30	; Internal reset
	rcall	lcd_cmd		; /
	ldi	temp1,5		; Wait for 4ms+
	rcall	wait_ms		; /
	ldi	lcd_data,0x30	; Internal reset
	rcall	lcd_cmd		; /
	ldi	temp1,1		; Wait for 100us+
	rcall	wait_ms		; /
	ldi	lcd_data,0x30	; Internal reset
	rcall	lcd_cmd		; /

	ldi	lcd_data,0x38	; Data=8bit, Line=2, Font=5x7
	rcall	lcd_cmd		; /
	ldi	lcd_data,0x0c	; Display=ON, Cursor=OFF, Blink=OFF
	rcall	lcd_cmd		; /
	ldi	lcd_data,6	; Increment, No shift
	rcall	lcd_cmd		; /
	ldi	lcd_data,1	; clear ,homepos
	rcall	lcd_cmd

		
int_:	
	ldi	temp1,0x85	;ADC ON, clock 4M/32=125k
	out	ADCSR,temp1	

main0:
	ldi	lcd_data,0x86
 	rcall	lcd_cmd
 	ldi	lcd_data,0xdf	;°
	rcall	lcd_dsp	
	ldi	lcd_data,'C'
	rcall	lcd_dsp
	ldi	lcd_data,0x8E
 	rcall	lcd_cmd
  	 ldi	lcd_data,0xdf	;°
	rcall	lcd_dsp	
	ldi	lcd_data,'C'
	rcall	lcd_dsp

	ldi	lcd_data,0xC6
 	rcall	lcd_cmd
 	ldi	lcd_data,0xdf	;°
	rcall	lcd_dsp	
	ldi	lcd_data,'C'
	rcall	lcd_dsp
	ldi	lcd_data,0xCE
 	rcall	lcd_cmd
  	 ldi	lcd_data,'V'
	rcall	lcd_dsp	

main:	;chan_0
	clr	F
 	ldi	temp1,0
 	out	ADMUX,temp1
	sbi     ADCSR,ADIF	; Reset interrupt flag
 	sbi	ADCSR,ADSC      ; Start A/D conversion
ready:	
 	sbis	ADCSR,ADIF
 	rjmp	ready
	in	AL,ADCL		;chan_0  →AH,AL
	in	AH,ADCH
	rcall	linear
	ldi	lcd_data,0x81
 	rcall	lcd_cmd

	rcall	dsp


	;chan_1
	clr	F
	ldi	temp1,1
	out	ADMUX,temp1
        sbi     ADCSR,ADIF	; Reset interrupt flag
	sbi	ADCSR,ADSC
ready1:
 	sbis	ADCSR,ADIF	
	rjmp	ready1
 	in	AL,ADCL		;chan_1 →AH,AL
	in	AH,ADCH
	rcall	linear
	ldi	lcd_data,0x89
 	rcall	lcd_cmd
	inc	C1 	
	rcall	dsp
	clr	C1
	;chan_2
	clr	F
	ldi	temp1,2
	out	ADMUX,temp1
        sbi     ADCSR,ADIF	; Reset interrupt flag
	sbi	ADCSR,ADSC
ready2:
 	sbis	ADCSR,ADIF	
	rjmp	ready2
 	in	AL,ADCL		;chan_2 →AH,AL
	in	AH,ADCH
	rcall	linear
	ldi	lcd_data,0xC1
 	rcall	lcd_cmd
	rcall	dsp
	
	;chan_3
	clr	F
	ldi	temp1,3
	out	ADMUX,temp1
        sbi     ADCSR,ADIF	;Reset interrupt flag
	sbi	ADCSR,ADSC
ready3:
 	sbis	ADCSR,ADIF	
	rjmp	ready3
 	in	AL,ADCL		;chan_3 →AH,AL
	in	AH,ADCH
	ldi	temp1,0
	ldi	temp2,160
	mul16	AH,AL,temp1,temp2
	mov	B4,r3
	mov	B3,r2
	mov	B2,r1
	mov	B1,r0
	ldi	lcd_data,0xCA
 	rcall	lcd_cmd
	clr	H
	rcall	dsp_2

	ldi 	temp1,250
	rcall	wait_ms
	ldi 	temp1,250
	rcall	wait_ms
	ldi 	temp1,200
	rcall	wait_ms
	rjmp	main	
;****************************
linear:
	cpi16	AH,AL,418
	brmi	linear01
	rjmp	linear1
linear01:		
	ldi	temp1,3
	ldi	temp2,158
	mul16	AH,AL,temp1,temp2
	ldi	temp1,6
	ldi	temp2,168
	ldi	temp3,242
	clr	temp4
	sub	r3,temp3
	sbc	r2,temp2
	sbc	r1,temp1
	sbc	r0,temp4
linear02:
	brpl	linear0
	inc	F
	clr	temp3
	clr	temp2
	neg	r3
	sbc	temp4,r2
	sbc	temp3,r1
	sbc	temp2,r0
	mov	r2,temp4
	mov	r1,temp3
	mov	r0,temp2
linear0:
	rjmp	linear_e
linear1:
	cpi16	AH,AL,642
	brpl	linear2
	ldi	temp1,3
	ldi	temp2,125
	mul16	AH,AL,temp1,temp2
	ldi	temp1,6
	ldi	temp2,123
	ldi	temp3,146
	clr	temp4
	sub	r3,temp3
	sbc	r2,temp2
	sbc	r1,temp1
	sbc	r0,temp4
	rjmp	linear02
linear2:
	cpi16	AH,AL,804
	brpl	linear3
	ldi	temp1,4
	ldi	temp2,212
	mul16	AH,AL,temp1,temp2
	ldi	temp1,9
	ldi	temp2,228
	ldi	temp3,128
	clr	temp4
	sub	r3,temp3
	sbc	r2,temp2
	sbc	r1,temp1
	sbc	r0,temp4
	rjmp	linear_e
linear3:
	cpi16	AH,AL,900
	brpl	linear4
	ldi	temp1,8
	ldi	temp2,44
	mul16	AH,AL,temp1,temp2
	ldi	temp1,20
	ldi	temp2,104
	ldi	temp3,6
	clr	temp4
	sub	r3,temp3
	sbc	r2,temp2
	sbc	r1,temp1
	sbc	r0,temp4
	rjmp	linear_e
linear4:
	cpi16	AH,AL,952
	brpl	linear5
	ldi	temp1,14
	ldi	temp2,211
	mul16	AH,AL,temp1,temp2
	ldi	temp1,43
	ldi	temp2,195
	ldi	temp3,52
	clr	temp4
	sub	r3,temp3
	sbc	r2,temp2
	sbc	r1,temp1
	sbc	r0,temp4
	rjmp	linear_e
linear5:
	ldi	temp1,30
	ldi	temp2,194
	mul16	AH,AL,temp1,temp2
	ldi	temp1,103
	ldi	temp2,39
	ldi	temp3,98
	clr	temp4
	sub	r3,temp3
	sbc	r2,temp2
	sbc	r1,temp1
	sbc	r0,temp4
linear_e:
	mov	B4,r3
	mov	B3,r2
	mov	B2,r1
	mov	B1,r0
	ret
;****************************
dsp:
	tst	C1
	breq	dsp00
	tst	BZ
	brne	dsp01
	tst	F
	brne	dsp02
	ldi	temp4,136
	ldi	temp3,19
	ldi	temp2,0
	cp	B4,temp4	; B:5000   0.5℃
	cpc	B3,temp3
	cpc	B2,temp2	
	brpl	dsp00
dsp02:
	sbi	PORTD,bzout
	ldi 	temp1,250
	rcall	wait_ms
	ldi 	temp1,250
	rcall	wait_ms
	ldi 	temp1,250
	rcall	wait_ms
	cbi	PORTD,bzout
	inc	BZ
	rjmp	dsp00
dsp01:
	tst	F
	brne	dsp00

	ldi	temp4,16
	ldi	temp3,39
	ldi	temp2,0
	cp	B4,temp4	; B:10000   1.0℃
	cpc	B3,temp3
	cpc	B2,temp2
	brmi	dsp00
	sbi	PORTD,bzout
	ldi 	temp1,200
	rcall	wait_ms
	cbi	PORTD,bzout
	ldi 	temp1,100
	rcall	wait_ms
	sbi	PORTD,bzout
	ldi 	temp1,200
	rcall	wait_ms
	cbi	PORTD,bzout
	clr	BZ	

dsp00:
	clr	H
	tst	F
	brne	dsp_1
	ldi	temp4,64
	ldi	temp3,66
	ldi	temp2,15
;	ldi	temp1,0
	cp	B4,temp4	; B:1000000
	cpc	B3,temp3
	cpc	B2,temp2
;	cpc	B1,temp1
	brmi	dsp0
	sub	B4,temp4	; B>1000000 >- B=B-1000000
	sbc	B3,temp3
	sbc	B2,temp2
;	sbc	B1,temp1
	ldi	lcd_data,'1'	
	rcall	lcd_dsp
	inc	H
	rjmp	dsp_2
dsp0:
	ldi	lcd_data,' '	
	rcall	lcd_dsp
	rjmp	dsp_2
dsp_1:
	ldi	lcd_data,'-'	
	rcall	lcd_dsp
dsp_2:	
	clr	D
	ldi	temp4,160
	ldi	temp3,134
	ldi	temp2,1
;	ldi	temp1,0
dsp_21:
	cp	B4,temp4	; B:100000
	cpc	B3,temp3
	cpc	B2,temp2
;	cpc	B1,temp1
	brmi	dsp_3
	sub	B4,temp4	; B>100000,; B=B-100000
	sbc	B3,temp3
	sbc	B2,temp2
;	sbc	B1,temp1
	inc	D
	rjmp	dsp_21
dsp_3:	
	tst	H
	brne	dsp_31
	tst	D
	brne	dsp_31
	ldi	lcd_data,' '	
	rcall	lcd_dsp
	rjmp	dsp_4
dsp_31:
	mov	lcd_data,D
	subi	lcd_data,-0x30
	rcall	lcd_dsp
dsp_4:
	clr	D
	ldi	temp4,16
	ldi	temp3,39
	ldi	temp2,0
;	ldi	temp1,0
dsp_41:
	cp	B4,temp4	; B:10000 
	cpc	B3,temp3
	cpc	B2,temp2
;	cpc	B1,temp1
	brmi	dsp_5
	sub	B4,temp4	; B>10000   B=B-10000
	sbc	B3,temp3
	sbc	B2,temp2
;	sbc	B1,temp1
	inc	D
	rjmp	dsp_41
dsp_5:
	mov	lcd_data,D
	subi	lcd_data,-0x30
	rcall	lcd_dsp
	clr	D
	ldi	lcd_data,'.'	
	rcall	lcd_dsp
dsp_6:
	ldi	temp4,232
	ldi	temp3,3
;	ldi	temp2,0
;	ldi	temp1,0
dsp_61:
	cp	B4,temp4	; B:1000
	cpc	B3,temp3
;	cpc	B2,temp2
;	cpc	B1,temp1
	brmi	dsp_7
	sub	B4,temp4	; B>1000   B=B-1000
	sbc	B3,temp3
;	sbc	B2,temp2
;	sbc	B1,temp1
	inc	D
	rjmp	dsp_61
dsp_7:
	mov	lcd_data,D
	subi	lcd_data,-0x30
	rcall	lcd_dsp	
	ret
;-----------------------------------------------;
; LCD - Send a data/cmd into LCD


lcd_dsp:
	rcall	lcd_wait
	sbi	PORTD,lcd_rs	; RS = "H"
	rjmp	PC+2
lcd_cmd:
	rcall	lcd_wait
	cbi	PORTD,lcd_rw	; RW = "L"
	ldi	temp1,-1
	out	DDRB,temp1	; PBDIR = OUTPUT
	sbi	PORTD,lcd_enb	; E = "H"
	nop			; I/O delay
	nop			; /
	cbi	PORTD,lcd_enb	; E = "L"
	ldi	temp1,0
	out	DDRB,temp1	; PBDIR = INPUT
	ret


lcd_wait:
	out	PORTB,lcd_data	; PORTB = data
	ldi	temp1,0
	out	DDRB,temp1	; PBDIR = INPUT
	sbi	PORTD,lcd_rw	; RW = "H"
	cbi	PORTD,lcd_rs	; RS = "L"
	sbi	PORTD,lcd_enb	; E = "H"
	nop			; I/O delay
	nop			; /
	sbic	PINB,lcd_db7	; Wait until busy flag becomes zero.
	rjmp	PC-1		; /
	cbi	PORTD,lcd_enb	; E = "L"
	ret

wait_us:	;base 4MHz	temp1　us

	nop			; 1clocks		
	dec	temp1		; 1clocks
	brne	wait_us		; 2clocks
	ret			; 

wait_ms:
				;temp1　ms
	ldi	temp2,125

wait_ms_01:
	ldi	temp3,7
wait_ms_02:
	nop			; 1clocks
	dec	temp3		; 1clocks
	brne	wait_ms_02	; 2clocks

	nop			; 1clocks
	dec	temp2		; 1clocks
	brne	wait_ms_01	; 2clocks

	dec	temp1		; 1clocks	
	brne	wait_ms		; 2clocks
	ret
;***************************************************************************
	