;**** includes ****  
.include "2313def.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	sreg_bak= r15
.def	temp1	= r16 
.def	temp2	= r17 
.def	temp3	= r18
.def	temp4	= r19
.def	OCR1AL_B= r20
.def	OCR1AH_B= r21
.def	pat_no	= r22 	;0-7


	
;***************************** 
;*　 ビット　設定　　* 
;***************************** 
;**  PORTB  ***
; 0:X,1:X~,2:Y,3:Y~
; 6(isp:1)=sf

;**  PORTB  ***
.equ	pl = 4	;
.equ	mi = 5	;
.equ	ccw= 6	;cw:1,ccw:0

.cseg 
;********************* 
;* 初期化　　　　　　* 
;********************* 	

	rjmp	start ;各種ﾘｾｯﾄ
	reti	;rjmp	 EXT_INT0	 ;外部割り込み要求0
	reti	;rjmp	 EXT_INT1	 ;外部割り込み要求1
	reti	;rjmp	 TIM_CAPT1	 ;ﾀｲﾏ/ｶｳﾝﾀ1 捕獲(ｷｬﾌﾟﾁｬ)発生
	rjmp	TIM_COMP1	 	;ﾀｲﾏ/ｶｳﾝﾀ1 比較一致
  
;********************* 
;* メイン プログラム * 
;********************* 
start: 
	ldi	temp1,low(RAMEND) 
	out	SPL,temp1	;init Stack Pointer 
	 
	ldi	temp1,0xff 
	out	DDRB,temp1	; PB0~7 out port 
	ldi	temp1,0x00
	out	PORTB,temp1	; PB0~7 clear 
	ldi	temp1,0x00 
	out	DDRD,temp1	; PD0~7 in port 
	ldi	temp1,0xff	 
	out	PORTD,temp1	; PD0~7 pull up 
 
time1_int:
	ldi	temp1,0	
	out	TCCR1A,temp1	;OC1 no_use　（PB3)
	out	TCNT1H,temp1
	out	TCNT1L,temp1
	ldi	temp1,(1<<CTC1)|(1<<CS11)|(1<<CS10);CTC動作、CKT1=CK/64
	out	TCCR1B,temp1
	ldi	temp1,0x08
	ldi	temp2,0x00
	out	OCR1AH,temp1;周期初期値　4.096M/64/$0800=31.25Hz
	out	OCR1AL,temp2	;            32ms
	mov	OCR1AL_B,temp2
	mov	OCR1AH_B,temp1
	ldi	temp1,1<<OCIE1A 
	out	TIMSK,temp1	;比較一致割込使用
 	clr	pat_no
	ldi	temp1,0x40
	mov	r1,temp1
	clr	r2
	sei
loop:
	sbic	PIND,5
	rjmp	loop_2
	ldi	temp1,50
	rcall	wait_ms
	sbic	PIND,5
	rjmp	loop_2
	add	OCR1AL_B,r1
	adc	OCR1AH_B,r2
	brcc	loop
	ldi	OCR1AH_B,0xff
	ldi	OCR1AL_B,0xc0
	rjmp	loop
loop_2:
	sbic	PIND,4
	rjmp	loop
	ldi	temp1,50
	rcall	wait_ms
	sbic	PIND,4
	rjmp	loop
	subi	OCR1AL_B,0x40
	sbci	OCR1AH_B,0
	cpi16	OCR1AH_B,OCR1AL_B,0x0140
	brpl	loop
	ldi	OCR1AL_B,0x40
	ldi	OCR1AH_B,0x01	
loop_3:

	rjmp	loop
 

TIM_COMP1:
	in	sreg_bak,SREG
	sbi	PORTB,6

	sbis	PIND,3		;励磁方式 1:2-1　
	rjmp	mode2
	sbis	PIND,2		;励磁方式 0:1	 1:2	
	rjmp	mood0
	rjmp	mood1
mood0:
	ldiw	z,m0_pat*2
	rjmp	mode3
mood1:
	ldiw	z,m_pat*2
	rjmp	mode3
mode2:
	ldiw	z,m1_pat*2
mode3:
	clr	temp4
	add	zl,pat_no
	adc	zh,temp4
	lpm		;r0 <-- (Z)
	ldi	temp4,0x40
	or	temp4,r0
	out	PORTB,temp4
	sbis	PIND,6
	rjmp	COMP1_0	
	inc	pat_no
	cpi	pat_no,8
	brne	COMP1_1
	clr	pat_no
	rjmp	COMP1_1
COMP1_0:
	dec	pat_no
	brpl	COMP1_1
	ldi	pat_no,7
COMP1_1:
	out	OCR1AH,OCR1AH_B
	out	OCR1AL,OCR1AL_B
	cbi	PORTB,6
	out	SREG,sreg_bak
	reti

wait_ms:			;temp1　ms
	ldi	temp2,64	;64*64=4096clocks at 4.096MHz
wait_ms_01:
	ldi	temp3,15    	;1clocks  4*(15+1)=64
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
 

m_pat:	  .db	0x05,0x06,0x0a,0x09,0x05,0x06,0x0a,0x09
m1_pat:   .db	0x05,0x04,0x06,0x02,0x0a,0x08,0x09,0x01
m0_pat:   .db	0x01,0x04,0x02,0x08,0x01,0x04,0x02,0x08