;**** includes **** 2008/4/27
.include "m8def.inc"
; Output immediate   outi	port,var
.macro	outi
	ldi	r16,@1
	out	@0,r16
.endm
;	ldiw	Z(X,Y),var
.macro	ldiw
	ldi	@0L,low(@1)
	ldi	@0H,high(@1)
.endm
;*****************************
;* レジスタ変数　設定 *
;*****************************
.def	temp1	= r16
.def	temp2	= r17
.def	temp3	= r18
.def	temp4	= r19
.def	led_h	= r20
.def	led_l	= r21
.def	th_s	= r22	
.def	mode	= r23
.def	dsp_c	= r24
;use r0
;x  26:27
;y  28:29 
;z  30:31
;*****************************
;*　 ビット　設定　　*
;*****************************
;**  PORTB  ***
;LED列data 
;**  PORTC  ***
;port0 adc0
;**  PORTD  ***
;.equ		= 0　十位com
;.equ		= 1　一位com
;.equ  ctrl	= 2
;.equ		= 3
;.equ	 	= 4
;.equ  adj	= 5
;.equ  		= 6
;.equ  set	= 7  

;*********************
;* 割込みベクター *
;*********************
.cseg
	rjmp	start	 ;各種ﾘｾｯﾄ
	reti 	;RJMP	 EXT_INT0	 ;外部割り込み要求0
	reti 	;RJMP	 EXT_INT1	 ;外部割り込み要求1
	reti 	;RJMP	 TIM2_COMP	 ;ﾀｲﾏ/ｶｳﾝﾀ2 比較一致
	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_OVF	 ;ﾀｲﾏ/ｶｳﾝﾀ0 ｵｰﾊﾞｰﾌﾛｰ
	reti 	;RJMP	 SPI_STC	 ;SPI転送完了
	reti 	;RJMP	 USART_RXC	 ;USART受信完了
	reti 	;RJMP	 USART_DRE	 ;USART送信ﾊﾞｯﾌｧ空
	reti 	;RJMP	 USART_TXC	 ;USART送信完了
	reti 	;RJMP	 ADC	 ;A/D変換完了
	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	 		;ｽﾀｯｸ ﾎﾟｲﾝﾀ下位を初期化

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:
	outi	ADMUX,0b00000000 ;Ch0(pc0)選択
	outi	ADCSR,(1<<ADEN)|(1<<ADPS2)|(1<<ADPS0)	;ADC_ck 4M/32=125k
timer1_set:
	outi	TIMSK,1<<OCIE1A	;タイマ１比較A一致割込み許可
	outi	TCCR1B,(1<<CTC1)|(1<<CS11)	;タイマ１比較A一致後クリア、TCK=CK/8
	outi	OCR1AH,high(5000)	;割込み周期　10ms
	outi	OCR1AL, low(5000)	;10ms/(1/4M/8)=5000
	outi	TCNT1H,0
	outi	TCNT1L,0

	clr	mode	;0:normal 100:set
	ldi	th_s,30	;th set 30deg
	clr dsp_c
	clr led_l
	clr led_h
	sei

main:
	cpi	mode,100
	brcs	main
	cli
	cbi	PORTD,1
	cbi	PORTD,0
	cbi	PORTD,2
	andi led_l,0xfe
ad:
	ldi temp1,200
	rcall wait_ms
	sbis PIND,7
	rjmp ad
ad1:
	mov temp3,th_s
	rcall dnum
ad2:
	ldi temp4,25
ad3:
	cbi	PORTD,1
	mov	temp2,led_l
	out	PORTB,temp2
	sbi PORTD,0
	ldi temp1,10
	rcall wait_ms
	cbi PORTD,0
	mov	temp2,led_h
	out	PORTB,temp2
	sbi PORTD,1
	ldi temp1,10
	rcall wait_ms
	dec temp4
	brne ad3
	cbi	PORTD,1
	sbic PIND,5
	rjmp ad4
	ldi temp1,50
	rcall wait_ms
	sbic PIND,5
	rjmp ad5
	inc th_s
	cpi th_s,41
	brne ad5
	clr th_s
	rjmp ad5
ad4:
	sbic PIND,7
	rjmp	ad41
	ldi temp1,50
	rcall wait_ms
	sbic PIND,7
	rjmp	ad5
	clr	mode
	outi	TCNT1H,0
	outi	TCNT1L,0
	sei
	rjmp	main
ad41:
	ldi temp1,50
	rcall wait_ms
ad5:
	ldi temp1,250
	rcall wait_ms
	ldi temp1,100
	rcall wait_ms
	rjmp	ad1

TIM1_COMPA:
	inc	dsp_c
	sbrc dsp_c,0		
	rjmp dsp_high
dsp_low:
	cbi	PORTD,1
	mov	temp2,led_l
	out	PORTB,temp2
	sbi PORTD,0
	rjmp	dsp_e
dsp_high:
	cbi PORTD,0
	mov	temp2,led_h
	out	PORTB,temp2
	sbi PORTD,1
dsp_e:
	ldi	temp1,0x7f ;1280ms(128*10)毎計測
	and	temp1,dsp_c
	brne key_in	
temp_read:
	sbi ADCSR,ADIF	;Reset interrupt flag
 	sbi	ADCSR,ADSC	;Start A/D conversion
temp_read1:	
 	sbis ADCSR,ADIF
 	rjmp temp_read1
	in	yl,ADCL	
	in	yh,ADCH
	ldi temp1,1
	rcall wait_ms
	sbi ADCSR,ADIF	;Reset interrupt flag
 	sbi	ADCSR,ADSC	;Start A/D conversion
temp_read2:	
 	sbis ADCSR,ADIF
 	rjmp temp_read2
	in	zl,ADCL	
	in	zh,ADCH
	add	zl,yl
	adc zh,yh
	lsr zh
	ror zl	;ave 2times		
	ldi	yl,low(434)	;(434-V)/2
	ldi	yh,high(434)
	sub	yl,zl
	sbc yh,zh
	brcc	temp_1
	clr yl
	clr yh ;ﾏｲﾅｽは０度
temp_1:
	lsr yh
	ror yl
	mov temp3,yl
	rcall	dnum
temp_2:
    mov	temp1,th_s	
	sbis PIND,2	;out on
	rjmp temp_3	;out off
	subi temp1,2	;offset 2deg
	cp	temp1,yl
	brlt key_in ;    th_set-2<th
	cbi	PORTD,2	;off th_set-2=>th
	andi	led_l,0xfe
	rjmp key_in	
temp_3:
	cp	yl,temp1
	brlt key_in ;   th<th_set
	sbi	PORTD,2	;on th=>th_set
	ori led_l,0x01
key_in:
	sbic PIND,7
	rjmp key1
	inc mode
	rjmp keye
key1:
	clr mode	
keye:
	sec
	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_h,r0
	ldiw z,num*2
	clr temp1
	add	zl,temp3
	adc	zh,temp1
	lpm  ;r0=7seg
	andi led_l,0x01 
	or	led_l,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



