;**** includes **** 2011/10/16 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
;*****************************
;* レジスタ変数　設定 *
;*****************************

.def	temp1	= r16
.def	temp2	= r17
.def	temp3	= r18
.def	lcd_data= r19

;*****************************
;*　 ビット　設定　　*
;*****************************
;**  PORTC  ***
;.equ			= 0; th
;.equ			= 1
;.equ			= 2
;.equ			= 3
;.equ			= 4
;.equ			= 5 
;.equ			= 7 
;**  PORTB  ***

;.equ		= 0
.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

;.eseg	;EEPROM add


.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捕獲発生
	 	 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,0b00000010
	out	DDRD,temp1	; pd0,2~7 in port
	ldi	temp1,0b11111100; pd2~7 pull up	
	out	PORTD,temp1
	

	ldi	temp1,0xff
	out	DDRB,temp1	; pb0~7 out port
	ldi	temp1,0x00	; pb0~7 clear	
	out	PORTB,temp1

	ldi	temp1,0xfc	
	out	PORTC,temp1	; pc2~7 pull up
	ldi	temp1,0
	out	DDRC,temp1	; pc2~7 in port,pc0,1 adc


; 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_data,0x20
	rcall	lcd_cmd
	ldi	temp1,100 		;wait for 100us+
	rcall	wait_us			; /
	ldi	lcd_data,0x28	; Data=4bit, 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
	ldi	lcd_data,0x84
 	rcall	lcd_cmd
	ldi	lcd_data,'O'
	rcall	lcd_dsp
	ldi	lcd_data,'K'
	rcall	lcd_dsp
USATR_int:
	stsi	UBRR0H,0		
	stsi	UBRR0L,25		;4MHz/[(25+1)x16]-->9600Hz
	stsi	UCSR0A,0		;受信完了フラグリセット
	stsi	UCSR0B,(1<<RXEN0)|(1<<TXEN0);RXEN0：受信許可、TXEN0：送信許可
	stsi	UCSR0C,(1<<UCSZ01)|(1<<UCSZ00);ビット長８bit、ストップビット１bit、非同期動作
ADC_set:
	stsi	ADCSRA,(1<<ADEN)|(1<<ADPS2)|(1<<ADPS0)	;ADC_ck 4M/32=125k

USART_1:
;	rcall Rx
;	cpi temp1,'o'
;	brne USART_1
;	mov r20,temp1
;	ldi	lcd_data,0xC0
; 	rcall	lcd_cmd
;	ldi	lcd_data,'O'
;	rcall	lcd_dsp
;	ldi	lcd_data,'n'
;	rcall	lcd_dsp
;	ldi	lcd_data,' '
;	rcall	lcd_dsp
;	mov	lcd_data,r20
;	rcall	lcd_dsp

timer1_set:
	stsi	TIMSK1,1<<OCIE1A	;タイマ１比較A一致割込み許可
	stsi	TCCR1B,(1<<WGM12)|(1<<CS11)|(1<<CS10)	;タイマ１比較A一致後クリア、TCK=CK/64
	stsi	OCR1AH,high(62500)	;割込み周期　1s
	stsi	OCR1AL, low(62500)	;1s/(1/4M/64)=62500
	stsi	TCNT1H,0
	stsi	TCNT1L,0
	sei
main:
	
	rjmp main


TIM1_COMPA:

read_t:
	stsi	ADMUX,0b11000000 ;基準電圧1.1V,Ch0(pc0)選択
	;Reset interrupt flag,Start A/D conversion
	ldi		r16,(1<<ADEN)|(1<<ADPS2)|(1<<ADPS0)|(1<<ADSC)|(1<<ADIF)
	sts		ADCSRA,r16
read_t1:	
	lds		r0,	ADCSRA
 	sbrs	r0,ADIF
 	rjmp read_t1
	lds  zl,ADCL
	lds zh,ADCH
	ldiw x,dsp_dat
	lsl zl		;256K=44*adc-17341  '(17367-26)   26:(1/10)=0.1切上
	rol zh		;yh:yl=256K →　yh=K
	lsl zl
	rol zh		;adc*4
	mov yl,zl
	mov yh,zh	;=abc*4
	lsl zl
	rol zh		;adc*8
	add yl,zl
	adc yh,zh	;=adc*12
	lsl zl
	rol zh
	lsl zl
	rol zh		;adc*32
	add yl,zl
	adc yh,zh	;=adc*44
	ldiw z,17341
	sub yl,zl
	sbc yh,zh
	brcc  read_t2
	com yl
	com yh	
	adiw yl,1
	sti x+,'-'
	rjmp read_t4
read_t2:
	subi yh,100
	brcs	read_t3
	sti x+,'1'
	rjmp read_t4
read_t3:
	sti x+,' '
	subi yh,-100
read_t4:
	clr temp3
read_t5:
	subi yh,10
	brcs 	read_t6
	inc temp3
	rjmp read_t5
read_t6:
	tst temp3
	brne read_t7
	sti x+,' '
	rjmp read_t8
read_t7:	
	subi temp3,-0x30
	st x+,temp3	
read_t8:
	subi yh,-10-0x30
	st x+,yh
	sti x+,'.'
	cpi yl,51	;<0.2 
	brcc	read_t10
	sti x+,'0'
	rjmp 	read_t20
read_t10:
	cpi yl,102	;<0.4
	brcc	read_t11
	sti x+,'2'
	rjmp 	read_t20
read_t11:
	cpi yl,154	;<0.6
	brcc	read_t12
	sti x+,'4'
	rjmp 	read_t20
read_t12:
	cpi yl,205	;<0.8
	brcc	read_t13
	sti x+,'6'
	rjmp 	read_t20
read_t13:
	sti x+,'8'
read_t20:
	sti x+,0xdf	;°
	sti x+,'C'
	sti x+,' '

	stsi	ADMUX,0b11000001 ;基準電圧1.1V,Ch1(pc1)選択
	;Reset interrupt flag,Start A/D conversion
	ldi	r16,(1<<ADEN)|(1<<ADPS2)|(1<<ADPS0)|(1<<ADSC)|(1<<ADIF)
	sts	ADCSRA,r16
read_n:	
	lds		r0,	ADCSRA
 	sbrs	r0,ADIF
 	rjmp read_n
	lds  zl,ADCL
	lds zh,ADCH
	mov r16,zl	;512w=625adc
	mov yl,zh
	clr yh		;yh:yl:r16=adc
	clr r19
	lsl zl
	rol zh		;2adc(256*2)
	add yl,zl
	adc yh,zh	;(512+1)adc
	lsl zl
	rol zh		;4adc
	lsl zl
	rol zh		;8adc	
	lsl zl
	rol zh		;16adc
	add r16,zl
	adc yl,zh
	adc yh,r19	;(512+16+1)adc
	lsl zl
	rol zh		;32adc
	add r16,zl
	adc yl,zh
	adc yh,r19	;(512+32+16+1)adc
	lsl zl
	rol zh		;64adc
	add r16,zl
	adc yl,zh
	adc yh,r19	;(512+64+32+16+1)adc ,512w=625adc
	adiw yl,2	;+1   切り上げ
	lsr yh
    ror yl		;256w=yh:yl:r16, w=yh:yl
	andi yl,0xfe	;偶数化

	ldiw z,1000	;r19=0
	sub yl,zl
	sbc yh,zh
	brcs read_n1	;w<1000
	sti x+,'1'		;w>=1000
	ldi r19,1
	rjmp read_n2
read_n1:
	sti x+,' '
	add yl,zl
	adc yh,zh  
read_n2:
	ldiw z,100	
	clr r16
read_n3:
	sub yl,zl
	sbc yh,zh
	brcs read_n4
	inc r16
	rjmp read_n3
read_n4:
	add yl,zl
	adc yh,zh 
	tst r16
	brne read_n6 ;r16<>0
	tst r19
	brne read_n6 ;10xx	
	sti x+,' '   ;__xx
	rjmp read_n7
read_n6:
	subi r16,-0x30
	st x+,r16
	ldi r19,1	
read_n7:
	clr r16
read_n8:
	subi yl,10
	brcs read_n9
	inc r16
	rjmp read_n8	
read_n9:
	tst r16
	brne read_n10 ;r16<>0
	tst r19
	brne read_n10 ;xx0x	
	sti x+,' '    ;___x
	rjmp read_n11
read_n10:
	subi r16,-0x30
	st x+,r16
read_n11:
	subi yl,-10-0x30
	st x+,yl
	sti x+,'w' 


	ldi	lcd_data,0x80
 	rcall	lcd_cmd
	ldi yl,13
	ldiw x,dsp_dat
read_t21:
	ld lcd_data,x+
	rcall	lcd_dsp
	dec yl
	brne read_t21
xport_send:
	ldi yl,13
	ldiw x,dsp_dat
xport_send1:
	ld temp1,x+
	rcall	Tx
	dec yl
	brne xport_send1
	ldi temp1,0x0A ;LF
	rcall	Tx	
	;ldi temp1,0x0D ;CR
	reti


;-----------------------------------------------;
; 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_data,0x30	; data='0011',RW = "L", RS = "L",E = "L"
	out	PORTB,lcd_data	
	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_data
	swap	temp2
	andi	temp2,0xf0	;data_L save

	andi lcd_data,0xf0   ;data_H set
	or	lcd_data,temp3
	out	PORTB,lcd_data	; PORTB = data_H,RW
	sbi	PORTB,lcd_enb	; E = "H"
	mov	lcd_data,temp2	; 
	or	lcd_data,temp3	;data_L set
	nop						
	cbi	PORTB,lcd_enb	; E = "L"
	nop			; I/O delay				
	out	PORTB,lcd_data	; 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
;*********************************************
Tx:
	LDS  r0,UCSR0A
	SBRS r0,UDRE0	;送信ﾊﾞｯﾌｧ空きでｽｷｯﾌﾟ
	RJMP Tx 	;送信ﾊﾞｯﾌｧ空き待機
	STS  UDR0,R16	;ﾃﾞｰﾀ送信(送信開始)
	RET 		;呼び出し元へ復帰
Rx:
	LDS  r0,UCSR0A
	SBRS r0,RXC0	;受信完了でｽｷｯﾌﾟ
	RJMP Rx 	;受信完了待機
	LDS  R16,UDR0	;受信ﾃﾞｰﾀ取得
	RET		;呼び出し元へ復帰
;*********************************************
EEPROM_WR:
	rcall	EEPROM_RD
	cp	temp1,temp2	;同じ値ならスキップ
	brne	EEPROM_WR1
	ret	
EEPROM_WR1:	
	 SBIC	 EECR,EEPE	 ;EEPROM書き込み完了ならばｽｷｯﾌﾟ
	 RJMP	 EEPROM_WR1	 ;以前のEEPROM書き込み完了まで待機
	 OUT	 EEARH,ZH	 ;EEPROMｱﾄﾞﾚｽ上位ﾊﾞｲﾄ設定
	 OUT	 EEARL,ZL	 ;EEPROMｱﾄﾞﾚｽ下位ﾊﾞｲﾄ設定
	 OUT	 EEDR,temp1	 ;EEPROM書き込み値を設定
	 SBI	 EECR,EEMPE	 ;EEPROMﾏｽﾀ書き込み許可ﾋﾞｯﾄ設定
	 SBI	 EECR,EEPE	 ;EEPROM書き込み開始(書き込み許可ﾋﾞｯﾄ設定)
	 RET	 	 	 ;呼び出し元へ復帰
;*********************************************
EEPROM_RD: 
	 SBIC	 EECR,EEPE	 ;EEPROM書き込み完了ならばｽｷｯﾌﾟ
	 RJMP	 EEPROM_RD	 ;以前のEEPROM書き込み完了まで待機
	 OUT	 EEARH,ZH	 ;EEPROMｱﾄﾞﾚｽ上位ﾊﾞｲﾄ設定
	 OUT	 EEARL,ZL	 ;EEPROMｱﾄﾞﾚｽ下位ﾊﾞｲﾄ設定
	 SBI	 EECR,EERE	 ;EEPROM読み出し開始(読み込み許可ﾋﾞｯﾄ設定)
	 IN	 temp2,EEDR	 ;EEPROM読み出し値を取得
	 RET	  	 	 ;呼び出し元へ復帰
;*********************************************

 
