;**** includes **** 2009/8/1 ck=4Mhz
.include "m88def.inc"

.macro	ldiw	;	ldiw	Z(X,Y),var
	ldi	@0L,low(@1)
	ldi	@0H,high(@1)
.endm

.MACRO cpi16	;cpi16	ra,rb,0xHHHH	;ra:rb-0xHHHH	set flags:N,C,Z
	push	@0
	push	@1
	subi	@1,low(@2)
	sbci	@0,high(@2)
	pop	@1
	pop	@0
.ENDMACRO
;*****************************
;* レジスタ変数　設定 *
;*****************************
.def	temp1	= r16
.def	temp2	= r17
.def	temp3	= r18
.def	led_2	= r20
.def	led_1	= r21
.def	led_0	= r22	
.def	mode	= r23
.def	led_n	= r24
.def	t1		= r25
.def	al_cal	= r19


;use r0
;xl  r26
;y  29:28 
;z  31:30
;*****************************
;*　 ビット　設定　　*
;*****************************
;**  PORTB  ***
;LED列data 
;**  PORTC  ***
;port0 adc0
;**  PORTD  ***
;.equ		= 0　一位com
;.equ		= 1　十位com
;.equ  		= 2　百位com
;.equ		= 3
;.equ	 	= 4
;.equ  		= 5
;.equ  		= 6
.equ  	sw	= 7  

;*********************
;* 割込みベクター *
;*********************
.cseg
	rjmp	start	 		;各種ﾘｾｯﾄ
	reti 	;RJMP	 EXT_INT0	;外部割り込み要求0
	reti 	;RJMP	 EXT_INT1	;外部割り込み要求1
	reti 	;RJMP	 PCINT0	 	;ﾋﾟﾝ変化割り込み要求0
	reti 	;RJMP	 PCINT1	 	;ﾋﾟﾝ変化割り込み要求1
	reti 	;RJMP	 PCINT2	 	;ﾋﾟﾝ変化割り込み要求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命令操作可
;*********************
;* メイン プログラム *
;*********************
start:
	LDI	 R16,HIGH(RAMEND)	;RAM最終ｱﾄﾞﾚｽ上位を取得
	OUT	 SPH,R16	 	;ｽﾀｯｸ ﾎﾟｲﾝﾀ上位を初期化
	LDI	 R16,LOW(RAMEND)	;RAM最終ｱﾄﾞﾚｽ下位を取得
	OUT	 SPL,R16	 	;ｽﾀｯｸ ﾎﾟｲﾝﾀ下位を初期化

	ldi	r16,0x80		;set 4MHz
	sts	CLKPR,r16
	ldi	r16,0x01
	sts	CLKPR,r16
i_o_reset:
	ldi	temp1,0	
	out	PORTB,temp1	; pb0~7 clear
	ldi	temp1,0xff
	out	DDRB,temp1	; pb0~7 out port
	ldi	temp1,0xfe	
	out	PORTC,temp1	; pc1~7 pull up
	ldi	temp1,0
	out	DDRC,temp1	; pc1~7 in port,pc0 adc
	ldi	temp1,0b11111000	
	out	PORTD,temp1	; pd0~2 clear,pd3~7 pull pu
	ldi	temp1,0b00000111
	out	DDRD,temp1	; pd0~2 out,3~7 input
ADC_set:
	ldi		r16,(1<<REFS0) ;基準電圧AVCC=5V,Ch0(pc0)選択
	sts		ADMUX,r16	;
	ldi		r16,(1<<ADEN)|(1<<ADPS2)|(1<<ADPS0)	;ADC_ck 4M/32=125k
	sts		ADCSRA,r16
timer1_set:
	ldi		r16,1<<OCIE1A	;タイマ１比較A一致割込み許可
	sts		TIMSK1,r16
	ldi		r16,(1<<WGM12)|(1<<CS11)	;タイマ１比較A一致後クリア、TCK=CK/8
	sts		TCCR1B,r16
	ldi		r16,high(3500)	;割込み周期　7ms
	sts		OCR1AH,r16
	ldi		r16,low(3500)	;7ms/(1/4M/8)=3500
	sts		OCR1AL,r16
	clr		temp1
	sts		TCNT1H,temp1
	sts		TCNT1L,temp1
	clr		mode	;0:cal 1:normal
	ldi	xl,84	;wait 30s
	ldi	led_2,0b10011100	;C
	ldi	led_1,0b11101110	;A
	ldi	led_0,0b00011100	;L
	clr	al_cal
	clr	led_n
	clr	t1
	sei

main:
	rjmp	main

TIM1_COMPA:
led_d:
	tst	led_n
	brne	led_d1
led_d0:
	cbi	PORTD,2
	out	PORTB,led_0
	sbi	PORTD,0
	rjmp	led_de
led_d1:
	cpi	led_n,1
	brne	led_d2	
	cbi	PORTD,0
	out	PORTB,led_1
	sbi	PORTD,1
	rjmp	led_de
led_d2:
	cbi	PORTD,1
	out	PORTB,led_2
	sbi	PORTD,2
led_de:
	inc	led_n
	cpi	led_n,3
	brne	led_de1
	clr	led_n
led_de1:
	inc	t1
	cpi	t1,51 ;357ms(7x51)毎計測
	breq	led_de2	
	rjmp 	key_in
led_de2:
	clr	t1
al_read:
	;Reset interrupt flag,Start A/D conversion
	ldi		r16,(1<<ADEN)|(1<<ADPS2)|(1<<ADPS0)|(1<<ADSC)|(1<<ADIF)
	sts		ADCSRA,r16
al_read1:	
	lds		r0,	ADCSRA
 	sbrs	r0,ADIF
 	rjmp	al_read1
	lds	yl,ADCL	
	lds	yh,ADCH
	ldi	temp1,1
	rcall	wait_ms
	;Reset interrupt flag,Start A/D conversion
	ldi		r16,(1<<ADEN)|(1<<ADPS2)|(1<<ADPS0)|(1<<ADSC)|(1<<ADIF)
	sts		ADCSRA,r16
al_read2:
	lds		r0,	ADCSRA
 	sbrs	r0,ADIF
 	rjmp 	al_read2
	lds	zl,ADCL	
	lds	zh,ADCH
	add	yl,zl
	adc	yh,zh
	lsr	yh
	ror	yl		;ave 2times
	cpi	mode,0
	brne	al0
	subi	xl,1
	breq	al000
	rjmp	TIM1_COMPA_e
al000:
	cpi16	yh,yl,120
	brcc	al00
	rjmp	al0
al00:
	ldi		xl,28	;wait 10s
	rjmp	TIM1_COMPA_e
al0:	
	cpi16	yh,yl,321
	brcc	up_dsp
	tst	yh
	brne	al1

	clr	temp3
	ldiw	z,dat_0*2
al01:
	lpm	temp1,z+
	cp	temp1,yl
	brcc	al2
	inc	temp3
	rjmp	al01
al1:
	ldi	temp3,52
	ldiw	z,dat_1*2
al11:
	lpm	temp1,z+
	cp	temp1,yl
	brcc	al2
	inc	temp3
	rjmp	al11
al2:
	tst	mode
	brne	al3
	mov	al_cal,temp3
	subi al_cal,-3
	ldi	mode,1
	ldi	led_2,0b11111101	;0.
	ldi	led_1,0b11111100	;0
	ldi	led_0,0b11111100	;0
	rjmp	TIM1_COMPA_e
al3:
	cp		temp3,al_cal
	brcs	key_in
	mov		al_cal,temp3
	rcall	dnum
	ldi		led_2,0b11111101
	rjmp	key_in
up_dsp:
	ldi	led_2,0b01100000	;1
	ldi	led_1,0b01111100	;U
	ldi	led_0,0b11001110	;P
	ldi	al_cal,255
key_in:
	sbic	PIND,7
	rjmp	TIM1_COMPA_e
	ldi	temp1,3
	rcall	wait_ms
	sbic	PIND,7	
	rjmp	TIM1_COMPA_e
	ldi	mode,0
	ldi	xl,28
	ldi	led_2,0b10011100	;C
	ldi	led_1,0b11101110	;A
	ldi	led_0,0b00011100	;L
TIM1_COMPA_e:
	reti

dnum:
	clr r0
dnum0:
	subi	temp3,10
	brcs	dnum1
	inc	r0
	rjmp	dnum0
dnum1:
	subi	temp3,-10
;	tst	r0
;	breq	dnum2
	ldiw	z,num*2
	clr	temp1
	add	zl,r0
	adc	zh,temp1
	lpm  	;r0=7seg 
dnum2:	
	mov	led_1,r0
	ldiw	z,num*2
	clr	temp1
	add	zl,temp3
	adc	zh,temp1
	lpm  	;r0=7seg
	mov	led_0,r0
	ret

wait_ms:	;temp1　ms
	ldi	temp2,50
wait_ms_01:
	ldi	temp3,19
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

;led 7seg pata:
num:	.db	0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe4,0xfe,0xf6
;sensor_table
dat_0:	.db	5,47,65,78,89,98,106,113,120,126,132,137,143,147,152,157,161,165,169,172,176,180,183,186,189,193,196,198,201,204,207,209,212,215,217,219,222,224,226,229,231,233,235,237,239,241,243,245,247,249,251,253,255,255
dat_1:	.db	0,2,4,6,7,9,11,12,14,15,17,18,20,21,23,24,26,27,29,30,32,33,34,36,37,38,40,41,42,43,45,46,47,48,50,51,52,53,54,55,57,58,59,60,61,62,63,64,255,255
