; Shift Point Indicator
;
;  CLK=10MHz
;
;  Ver2.1  2001.10.27   Dimmerのちらつき防止で 4段階に変更
;                       LEDの点灯を100rpm単位で変更可能に
;  Ver2.0  2001.10.20   BuzzerのRevLimitを個別変更化
;                       電源投入時のデモモード追加
;                       Dimmerをサポート(16段階)
;  Ver1.1  2001.06.27   回転信号入力中の設定変更で割り込み設定バグを修正
;  Ver1.0  2001.06.18   量産 Shift Point Indicator用
;
;
;  Pin I/O  Note(16F84A)
;----------------------------------------------------------
;  RA4 in   Rev Pulse Signal
;  RA3 -    No Connect
;  RA2 out  LED RevLimit + RevDif x 6
;  RA1 out  LED RevLimit + RevDif x 5
;  RA0 out  LED RevLimit + RevDif x 4
;
;  RB7 out  LED RevLimit + RevDif x 3
;  RB6 out  LED RevLimit + RevDif x 2
;  RB5 out  LED RevLimit + RevDif x 1
;  RB4 out  LED RevLimit
;
;  RB3 out  Buzzer RevLimit
;  RB2 in   UP
;  RB1 in   DOWN
;  RB0 in   MENU(Exit)
;
;    
;
;Pin Assign:
;          +----------+
;  LED6   1|RA2   RA1 |18    LED5
;  NC     2|RA3   RA0 |17    LED4
;  RevIN  3|RA4   OSC1|16    OSC1
;  +5V    4|MCLR  OSC2|15    OSC2
;  GND    5|Vss   Vdd |14    +5V
;  MENU   6|RB0   RB7 |13    LED3
;  DOWN   7|RB1   RB6 |12    LED2
;  UP     8|RB2   RB5 |11    LED1
;  Buzzer 9|RB3   RB4 |10    LED0
;          +----------+
;
;    RA2-0/RB7-4 -> 定電流ダイオード(15mA) -> LED -> GND
;    RB2/1/0 -> SW -> GND  (PIC内にてPullUp)
;    OSC1/OSC2は、10pFでGNDに接続
;    RB3 -> Buzzer
;
;RevCounter:
;    (+)  -> 7805in
;    (-)  -> 7805gnd
;    (TP) -> 10kΩ -> RA4
;
;
;
;----------------------------------------------------------
	include	P16f84.inc

__config	_HS_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF
	

;--- RAM ------------------------
pushw	equ	h'0C'		;Wレジスタ退避用
pushs	equ	h'0D'		;STATUS退避用
pcnt	equ	h'0E'		;Rev Pulse Counter
revl	equ	h'0F'		;Rev Lower
revu	equ	h'10'		;　　Upper
icntl	equ	h'11'		;Int Counter Lower
icntu	equ	h'12'		;            Upper
waitcnt	equ	h'13'		;Wait Counter
intcnt	equ	h'14'		;割り込み処理カウンタ
idct    equ	h'15'		;Indicator
lrev    equ     h'16'		;LED Rev Limit
lrdf	equ	h'17'		;LED点灯間隔
brev	equ	h'18'		;Buzzer Rev Limit
rtmp    equ     h'19'		;Rev Work
dim	equ	h'1A'		;Dimmer Ratio
ldm	equ	h'1B'		;Opening LED Disp Mode 
tmp	equ	h'1C'		;Temp
pa	equ	h'1D'		;PortA buffer
pb	equ	h'1E'		;PortB buffer
ul	equ	h'1F'		;Upper Limit Buffer
ll	equ	h'20'		;Lower Limit Buffer

divcnt	equ	h'21'		;除算loopカウンタ
div_e	equ	h'22'		;除算用Work/Result Lower
div_d	equ	h'23'		;　〃　Work/Result Upper
div_c	equ	h'24'		;　〃　Work
div_b	equ	h'25'		;  〃　Work



;--- EEP-ROM --------------------
elrev	equ	h'00'		;LED RevLimit
elrdf	equ	h'01'		;LED点灯間隔
ebrev	equ	h'02'		;Buzzer RevLimit
eldm	equ	h'03'		;LED Mode
edim	equ	h'04'		;Dimmer Ratio





;--- Reset -------------------------------------------------------------
        org     00H
	goto	init		;初期設定/Main



;--- Timer Interrupt ---------------------------------------------------------
	org	04H
	movwf	pushw		;Work Reg. push
	movf	STATUS,w	;Status ->Work
	movwf	pushs		;push Status

	bcf	INTCON,T0IF	;タイマ割り込みclear
	incf	intcnt,f	;カウンタinc

;--- Dimmer(LEDの点灯を間引) ----
	movlw	b'00000011'	;
	andwf	intcnt,w	;
	subwf	dim,w		;dim - (ledcnt & 00000011b)
	btfss	STATUS,C	;
	goto	dim_off		;
	movf	pa,w		;LED Enable
	movwf	PORTA		;
	movf	pb,w		;
	movwf	PORTB		;
	goto	dim_end
dim_off
	clrf	PORTA		;LED OFF
	clrf	PORTB		;
dim_end

	movf	pushs,w		;pop Status -> Work
	movwf	STATUS		;Work -> Status
	movf	pushw,w		;Work Reg. pop
	retfie



;--- 初期設定 ----------------------------------------------------------
init	bsf	STATUS,RP0	;Bank 1
	movlw	b'00000100'	;Pre-Scaler(1/64:12.8us),  PortB PullUp
	movwf	OPTION_REG	;Set OPTION Reg.
	movlw	b'00010000'	;1=in 0=out
	movwf	TRISA		;
	movlw	b'00000111'	;1=in 0=out
	movwf	TRISB		;
	bcf	STATUS,RP0	;Bank 0
	clrf	idct		;インジケータクリア
	clrf	pa		;PortA Bufferクリア
	clrf	pb		;PortB bufferクリア
	clrf	PORTA		;PortAクリア
	clrf	PORTB		;PortBクリア
	clrf	TMR0		;タイマーCLR
	bcf	INTCON,T0IF	;タイマ割り込みクリア
	bsf	INTCON,T0IE	;タイマー割り込みEnable
	bsf	INTCON,GIE	;全割り込みEnable

;--- LED RevLimit Read ----------
	movlw	elrev		;ADR Set
	call	eep_read	;EEP-ROM read
	movwf   lrev		;Set RevLimit
;--- LED点灯間隔 Read ----------
	movlw	elrdf		;ADR Set
	call	eep_read	;EEP-ROM read
	movwf   lrdf		;Set 点灯間隔
;--- Buzzer RevLimit Read -------
	movlw	ebrev		;ADR Set
	call	eep_read	;EEP-ROM read
	movwf   brev		;Set RevLimit
;--- Dimmer Ratio Read ----------
	movlw	edim		;ADR Set
	call	eep_read	;EEP-ROM read
	movwf   dim		;Set Dim Ratio
;--- LED Disp Mode Read ---------
	movlw	eldm		;ADR Set
	call	eep_read	;EEP-ROM read
	movwf   ldm		;Set Disp Mode

;--- Openning Demo -----------
	movwf	tmp		;
	decf	tmp,f		;
	btfsc	STATUS,Z	;
        goto	ldm01		;
	decf	tmp,f		;
	btfsc	STATUS,Z	;
        goto	ldm02		;
	goto	ldm03		;


;--- LED Demo -------------------
ldm01	bsf	pb,4		;
	call    wait60ms	;
	bsf	pb,5		;
	call    wait60ms	;
	bsf	pb,6		;
	call    wait60ms	;
	bsf	pb,7		;
	bcf	pb,4		;
	call    wait60ms	;
	bsf	pa,0		;
	bcf	pb,5		;
	call    wait60ms	;
	bsf	pa,1		;
	bcf	pb,6		;
	call    wait60ms	;
	bsf	pa,2		;
	bcf	pb,7		;
	call    wait60ms	;
	bcf	pa,0		;
	call    wait60ms	;
	bcf	pa,1		;
	call    wait60ms	;
	bcf	pa,2		;
	call    wait60ms	;
        goto	restart

ldm02	bsf	pb,4		;
	call    wait60ms	;
	bsf	pb,5		;
	call    wait60ms	;
	bsf	pb,6		;
	call    wait60ms	;
	bsf	pb,7		;
	bcf	pb,4		;
	call    wait60ms	;
	bsf	pa,0		;
	bcf	pb,5		;
	call    wait60ms	;
	bsf	pa,1		;
	bcf	pb,6		;
	call    wait60ms	;
	bsf	pa,2		;
	bcf	pb,7		;
	call    wait60ms	;
	bcf	pa,0		;
	call    wait60ms	;
	bcf	pa,1		;
	call    wait60ms	;

	bsf	pa,1		;
	call    wait60ms	;
	bsf	pa,0		;
	call    wait60ms	;
	bsf	pb,7		;
        bcf	pa,2		;
	call    wait60ms	;
	bsf	pb,6		;
	bcf	pa,1		;
	call    wait60ms	;
	bsf	pb,5		;
	bcf	pa,0		;
	call    wait60ms	;
	bsf	pb,4		;
	bcf	pb,7		;
	call    wait60ms	;
	bcf	pb,6		;
	call    wait60ms	;
	bcf	pb,5		;
	call    wait60ms	;
	bcf	pb,4		;
	call    wait60ms	;
	goto	restart

ldm03	bsf	pb,4		;
	call    wait60ms	;
	bcf	pb,4		;
	call	wait60ms	;
	bsf	pb,4		;
	bsf	pb,5		;
	bsf	pa,0		;
	call    wait60ms	;
	bcf	pb,4		;
	call    wait60ms	;
	bsf	pb,4		;
	bsf	pb,6		;
	bsf	pa,1		;
	call    wait60ms	;
	bcf	pb,4		;
	call    wait60ms	;
	bsf	pb,4		;
	bsf	pb,7		;
	bsf	pa,2		;
	call    wait60ms	;
	bcf	pb,4		;
	call    wait60ms	;

	bsf	pb,4		;
	bcf	pb,5		;
	bcf	pa,0		;
	call    wait60ms	;
	bcf	pb,4		;
	call    wait60ms	;
	bsf	pb,4		;
	bcf	pb,6		;
	bcf	pa,1		;
	call    wait60ms	;
	bcf	pb,4		;
	call    wait60ms	;
	bsf	pb,4		;
	bcf	pb,7		;
	bcf	pa,2		;
	call    wait60ms	;
	bcf	pb,4		;
        goto	restart



;--- Restart ---------------------------------------------------
restart
	movlw	h'10'		;Pulse信号の入力がない場合にはRB4を反転させる
	movwf	pb		;(Ideling(Stanby)表示)
	comf	idct		;
	btfss	STATUS,Z	;
	clrf	pb		;

;--- Main Loop -------------------------------------------------
;--- クランク初期位置待ち -------
main	clrf	TMR0		;タイマークリア
	clrf	intcnt		;カウンタクリア
h_wait	btfss	PORTB,0		;設定へ
	goto	setup		;
	btfss	PORTB,1		;Dimmer設定へ
	goto	dimsetup	;
	btfss	PORTB,2		;Dimmer設定へ
	goto	dimsetup	;
	
	movlw	d'60'		;約197ms
	subwf	intcnt,w	;
	btfsc	STATUS,Z	;
	goto	restart		;タイムアウト
	btfss	PORTA,4		;Sacn Signal
	goto	h_wait		;if Signal=H then h_wait

	clrf	TMR0		;タイマークリア
	clrf	intcnt		;カウンタクリア
l_wait	btfss	PORTB,0		;Menuへ
	goto	setup		;
	btfss	PORTB,1		;Dimmer設定へ
	goto	dimsetup	;
	btfss	PORTB,2		;Dimmer設定へ
	goto	dimsetup	;

	movlw	d'120'		;約393ms
	subwf	intcnt,w	;
	btfsc	STATUS,Z	;
	goto	restart		;タイムアウト
	btfsc	PORTA,4		;Scan Signal
	goto	l_wait		;if Signal=L then l_wait

;--- クランクパルス幅測定 -------
	clrf	TMR0		;タイマクリア
	clrf	intcnt		;
	movlw	d'2'		;2パルス(1回転)をセット
	movwf	pcnt		;
mloop	movlw	d'78'		;1ms Wait
	call	wait13us	;チャタリング防止
h_mloop	movlw	d'60'		;197msでTimeout
	subwf	intcnt,w	;
	btfsc	STATUS,Z	;
	goto	restart		;Timeout
	btfss	PORTA,4		;
	goto	h_mloop		;if Signal=h then h_mloop

	movlw	d'78'		;1ms Wait
	call	wait13us	;チャタリング防止
l_mloop	movlw	d'120'		;393msでTimeout
	subwf	intcnt,w	;
	btfsc	STATUS,Z	;
	goto	restart		;Timeout
	btfsc	PORTA,4		;
	goto	l_mloop		;if Signal=h then l_mloop

	decfsz	pcnt,f		;ループカウンタを-1
	goto	mloop		;規定数loopしてないならmloopへ

;--- Rev計算 --------------------
	bcf	INTCON,T0IE	;タイマー割り込みDisable
	movf	TMR0,w		;カウント数store
	movwf	icntl		;
	movf	intcnt,w	;
	movwf	icntu		;
	bsf	INTCON,T0IE	;タイマー割り込みEnable
	call	div		;除算処理

;--- 表示 -----------------------
disp	clrf	pa
	clrf	pb

	movf	revu,w		;rtmp = revu
	movwf	rtmp		;
	decf	rtmp,f		;
	movf	lrev,w		;
	subwf	rtmp,f		;rtmp = revu - lrev
	btfss	STATUS,C	;
        goto	buzzer		;
	bsf	pb,4		;LED0 ON(LSB)

	movf	lrdf,w		;
	subwf	rtmp,f		;rtmp = rtmp - lrdf
	btfss	STATUS,C	;
        goto	buzzer		;
        bsf	pb,5		;LED1 ON

	movf	lrdf,w		;
	subwf	rtmp,f		;rtmp = rtmp - lrdf
	btfss	STATUS,C	;
        goto	buzzer		;
        bsf	pb,6		;LED2 ON

	movf	lrdf,w		;
	subwf	rtmp,f		;rtmp = rtmp - lrdf
	btfss	STATUS,C	;
        goto	buzzer		;
        bsf	pb,7		;LED3 ON

	movf	lrdf,w		;
	subwf	rtmp,f		;rtmp = rtmp - lrdf
	btfss	STATUS,C	;
        goto	buzzer		;
        bsf	pa,0		;LED4 ON

	movf	lrdf,w		;
	subwf	rtmp,f		;rtmp = rtmp - lrdf
	btfss	STATUS,C	;
        goto	buzzer		;
        bsf	pa,1		;LED5 ON

	movf	lrdf,w		;
	subwf	rtmp,f		;rtmp = rtmp - lrdf
	btfss	STATUS,C	;
        goto	buzzer		;
        bsf	pa,2		;LED6 ON

;--- Buzzer ---------------------
buzzer
	movf	revu,w		;rtmp = revu
	movwf	rtmp		;
	decf	rtmp,f		;
	movf    brev,w		;brev以上ならBuzzer ON
	subwf	rtmp,w		;
	btfsc	STATUS,C	;
	bsf	pb,3		;Buzzer ON

	goto	main		;







;----- RevLimit/OpenningDemo設定 -----------------------------------------
setup
	call    wait13us	;チャタリング防止Wait
	btfss	PORTB,0		;MEMU/EXITキーのリリース確認
	goto	setup		;

;--- LED RevLimit Setting -------
	movlw	d'99'		;UpperLimit 9900rpm
	movwf	ul		;
	movlw	d'8'		;LowerLimit  800rpm
	movwf	ll		;
	movlw	b'00000100'	;Indicate for LED RevLimit
	movwf	pa		;
	clrf	pb		;

	movf	lrev,w		;
	movwf	tmp		;
	call	setting		;LED RevLimit設定
	movf	tmp,w		;
	movwf	lrev		;

	movwf	EEDATA		;DATA Set
	movlw	elrev		;ADR Set
	movwf	EEADR		;
	call	eep_write	;

;--- LED 点灯間隔 Setting ----
	movlw	d'10'		;UpperLimit 1000rpm
	movwf	ul		;
	movlw	d'1'		;LowerLimit  100rpm
	movwf	ll		;
	movlw	b'00000010'	;Indicate for Buzzer RevLimit
	movwf	pa		;
	clrf	pb		;

	movf	lrdf,w		;
	movwf	tmp		;
	call	setting		;LED 点灯間隔設定
	movf	tmp,w		;
	movwf	lrdf		;

	movwf	EEDATA		;DATA Set
	movlw	elrdf		;ADR Set
	movwf	EEADR		;
	call	eep_write	;

;--- Buzzer RevLimit Setting ----
	movlw	d'99'		;UpperLimit 9900rpm
	movwf	ul		;
	movlw	d'8'		;LowerLimit  800rpm
	movwf	ll		;
	movlw	b'00000001'	;Indicate for Buzzer RevLimit
	movwf	pa		;
	clrf	pb		;

	movf	brev,w		;
	movwf	tmp		;
	call	setting		;Buzzer RevLimit設定
	movf	tmp,w		;
	movwf	brev		;

	movwf	EEDATA		;DATA Set
	movlw	ebrev		;ADR Set
	movwf	EEADR		;
	call	eep_write	;

;--- LED Disp Mode Setting ------
	movlw	d'3'		;Upper Limit
	movwf	ul		;
	movlw	d'1'		;Lower Limit
	movwf	ll		;
	clrf	pa		;
	movlw	d'10000000'	;Indicate for Disp Mode
	movwf	pb		;

	movf	ldm,w		;
	movwf	tmp		;
	call	setting		;Openning Mode設定
	movf	tmp,w		;
	movwf	ldm		;

	movwf	EEDATA		;DATA Set
	movlw	eldm		;ADR Set
	movwf	EEADR		;
	call	eep_write	;
        goto    restart		;



;--- キー入力処理 ---------------
setting
	movf	tmp,w		;UpperLimit Check
	subwf	ul,w		;
	btfsc	STATUS,C	;
	goto	ul_chke		;
	movf	ul,w		;
	movwf	tmp		;
ul_chke
	movf	ll,w		;LowerLimit Check
	subwf	tmp,w		;
	btfsc	STATUS,C	;
	goto	ll_chke		;
	movf	ll,w		;
	movwf	tmp		;
ll_chke

	btfsc	PORTB,0		;Sacn SW(MEMU/EXIT)
	goto	SW_EXE		;
	clrf	pa		;
	clrf	pb		;
	call    wait13us	;チャタリング防止
SW_EXR
	btfss	PORTB,0		;Sacn SW(MEMU/EXIT)
	goto	SW_EXR		;
	return			;
SW_EXE
	btfsc	PORTB,2		;Sacn SW(UP)
	goto	SW_UPE		;
	bsf	pb,4		;Indicate for SW-PUSH
	call	wait13us	;
	incf	tmp,f		;increase
	goto	SW_DWE		;
SW_UPE
 	btfsc	PORTB,1		;Sacn SW(DOWN)
	goto	setting_disp	;
	bsf	pb,4		;Indicate for SW-PUSH
	call	wait13us	;
	decf	tmp,f		;Decrease
SW_DWE
	call    waita60ms	;
	bcf	pb,4		;
	call    waita60ms	;
	call    waita60ms	;
	goto	setting		;


;--- 設定値表示 -----------------
setting_disp
	bcf	pb,4		;
        movf    tmp,w		;
        movwf   rtmp		;
	call    wait60ms	;
	call    wait60ms	;
	call    wait60ms	;
	call    wait60ms	;
	call    wait60ms	;
	call    wait60ms	;
	call    wait60ms	;
	call    wait60ms	;

;--- 10の位を長点滅で表示 -------
rdisp10
	btfss	PORTB,0		;キー入力があった場合には戻る
	goto	setting		;
 	btfss	PORTB,1		;
	goto	setting		;
 	btfss	PORTB,2		;
	goto	setting		;

        movlw   d'10'        	;
	subwf	rtmp,w		;10以上ならLED長点滅
	btfss	STATUS,C	;
	goto	rdisp10e	;
        movwf   rtmp		;
	bsf	pb,4		;
	call    wait60ms	;
	call    wait60ms	;
	bcf	pb,4		;
	call    wait60ms	;
	call    wait60ms	;
        goto    rdisp10		;
rdisp10e
	call    wait60ms	;
	call    wait60ms	;
	call    wait60ms	;
	call    wait60ms	;

;--- 1の位を短点滅で表示 --------
rdisp01
	btfss	PORTB,0		;キー入力があった場合には戻る
	goto	setting		;
 	btfss	PORTB,1		;
	goto	setting		;
 	btfss	PORTB,2		;
	goto	setting		;

	movlw   d'1'		;1以上ならLED短点滅
	subwf	rtmp,w		;
	btfss	STATUS,C	;
	goto	setting_disp	;
        movwf   rtmp		;
	bsf	pb,4		;
	call    wait60ms	;
        bcf	pb,4		;
	call    wait60ms	;
	call    wait60ms	;
        goto    rdisp01		;



;--- Dimmer Ratio Setting ---------------------------------------------
dimsetup
	movlw	d'3'		;UpperLimit
	movwf	ul		;
	movlw	b'11100000'	;GreenLEDのみON
	movwf	pb		;
	clrf	pa		;

	movf	dim,w		;
	movwf	tmp		;
	call	dimsetting	;Dimmer Ratio設定(100%/75%/50%/25%)
	movf	tmp,w		;
	movwf	dim		;

	movlw	edim		;ADR Set
	movwf	EEADR		;
	movf	dim,w		;DATA Set
	movwf	EEDATA		;
	call	eep_write	;
	goto	restart		;


dimsetting
	call	wait13us
	btfsc	PORTB,0		;Sacn SW(EXIT)
	goto	dimMENU		;
	clrf	pa		;
	clrf	pb		;
dimEXIT
	btfss	PORTB,0		;キーリリース確認
	goto	dimEXIT		;
	return			;

dimMENU
	btfsc	PORTB,2		;Sacn SW(UP)
	goto	dimDOWN		;
	call	wait13us	;
	incf	tmp,f		;
	goto	dimCHK		;

dimDOWN
 	btfsc	PORTB,1		;Sacn SW(DOWN)
	goto	dimsetting	;
	call	wait13us	;
	movf	tmp,w		;
	btfss	STATUS,Z	;
	decf	tmp,f		;

dimCHK
	bsf	pb,4		;Indicate for SW-PUSH
	call    waita60ms	;
	bcf	pb,4		;
	movf	tmp,w		;Set Dimmer Ratio
	movwf	dim		;
	call    waita60ms	;
	call    waita60ms	;
	call    waita60ms	;

	movf	tmp,w		;UpperLimit Check
	subwf	ul,w		;
	btfsc	STATUS,C	;
	goto	dimsetting	;
	movf	ul,w		;
	movwf	tmp		;
	goto	dimsetting	;




;--- EEP-ROM Read ----------------------------------------------------
; in  w : EEP-ROM adr
; out w : data
eep_read
	movwf	EEADR		;		
	bsf	STATUS,RP0	;Bank 1
	bsf	EECON1,RD	;EEPROM Read Enable
	bcf	STATUS,RP0	;Bank 0
	movf	EEDATA,w	;EEPROM Read
	return	



;--- EEP-ROM Write ---------------------------------------------------
; in EEADR  : EEP-ROM adr
;    EEDATA : data
eep_write
	bsf	STATUS,RP0	;Bank 1
	bsf	EECON1,WREN	;Enable Write
	bcf	INTCON,GIE	;Disable INTs
	movlw	h'55'		;Required Sequence for EEPROM Write
	movwf	EECON2		;
	movlw	h'aa'		;
	movwf	EECON2		;
	bsf	EECON1,WR	;Set WR bit
eep_wait
	btfsc   EECON1,WR	;Wait Write Completion
	goto	eep_wait	;

	bcf	STATUS,RP0	;Bank 0
	bsf	INTCON,GIE	;Enable INTs
	bcf	EECON1,EEIF	;EEPROM Write INT Flag clear
	return









;--- Wait Routine -----------------------------------------------------------
wait13us
	addwf	TMR0,w		;Wait=w x 12.8us
	movwf	waitcnt		;
wait13usl
	movf	waitcnt,w	;
	subwf	TMR0,w		;
	btfss	STATUS,Z	;
	goto	wait13usl	;
	return


wait60ms
        clrf    intcnt          ;
	clrf    TMR0		;
	movlw	d'16'		;約61ms
wait60msl
	btfss	PORTB,0		;キー入力があった場合には戻る
	return			;
 	btfss	PORTB,1		;
	return			;
 	btfss	PORTB,2		;
	return			;
	subwf	intcnt,w	;
	btfss	STATUS,Z	;
	goto	wait60msl	;
        return

waita60ms
        clrf    intcnt          ;
	clrf    TMR0		;
	movlw	d'16'		;約61ms
waita60msl
	subwf	intcnt,w	;
	btfss	STATUS,Z	;
	goto	waita60msl	;
        return



;--- Divide Routine ------------------------------------------------------
div	bcf	STATUS,C	;X x (us) = 12000000
	movlw	d'0'		; 8MHz 1rpm      3750000( 57 x 65536 +  56 x 256 + 112)
	movwf	div_e		;10MHz 1rpm      4687500( 71 x 65536 + 134 x 256 + 140)
	movlw	d'27'		;10MHz 2.56rpm Upper側で100rpmStep 
	movwf	div_d		;               12000000(183 x 65536 +  27 x 256 +   0) 
	movlw	d'183'		;                        
	movwf	div_c		;
	movlw	d'0'		;
	movwf	div_b		;
	movlw 	d'17'		;Loop
	movwf	divcnt		;
div1	rlf	div_e,f		;
	rlf	div_d,f		;
	rlf	div_c,f		;
	rlf	div_b,f		;
	movf	icntl,w		;
	subwf	div_c,f		;div_c-icntl -> div_c
	btfss	STATUS,C	;Carry Check
	goto	div2		;
	goto	div22		;
div2	movlw	d'1'		;
	addwf	icntu,w		;
	subwf	div_b,f		;
	goto	div222		;
div22	movf	icntu,w		;
	subwf	div_b,f		;
div222	btfsc	STATUS,C	;Carry Check
	goto	div6		;
	movf	icntl,w		;
	addwf	div_c,f		;
	btfsc	STATUS,C	;
	goto	div3		;
	goto	div33		;
div3	movlw	d'1'		;
	addwf	icntu,w		;
	addwf	div_b,f		;
	goto	div333		;
div33	movf	icntu,w		;
	addwf	div_b,f		;
div333	bcf	STATUS,C	;
div6	nop			;
	decfsz	divcnt,f	;
	goto	div1		;
	movf	div_e,w		;
	movwf	revl		;
	movf	div_d,w		;
	movwf	revu		;
	return

	end

