;THIS IS ON-DIGI2 TX ASM
;                 v1.0    non log
;                 
;   bug fix 08/23   8/25 PORT B PALA OUT
	list	p=16f873
	include <p16F873.inc>

	__config _HS_OSC & _WDT_OFF & _PWRTE_OFF & _CP_OFF

;BANK1

	BSF	STATUS,RP0	;BANK 1
	BCF	STATUS,RP1

	MOVLW	B'11111111'	;PORT A INPUT
	MOVWF	TRISA	;WRITE
	MOVLW	B'00000000'	;PORT B OUTPUT(FOR CHECK)
	MOVWF	TRISB	;WRITE
	MOVLW	B'00000000'	;PORT C OUTPUT
	MOVWF	TRISC	;WRITE
	MOVLW   B'00001000'
	MOVWF	ADCON1  
	BSF	PIE1,	ADIE
;BANK0
	BCF	STATUS,RP0
	MOVLW	B'10000001'
	MOVWF	ADCON0	
	BCF	PIR1,	ADIF
	BSF	INTCON,PEIE

;A/D WAIT 190 STEPS　

	MOVLW	H'80'
	MOVWF	H'30'	
	MOVWF	H'31'	
;	MOVWF	H'32'
	MOVWF	H'33'	
	MOVWF	H'34'	
	MOVWF	H'35'	
	MOVWF	H'36'	
	MOVWF	H'37'
	MOVWF	H'40'	
	MOVWF	H'41'	
	MOVWF	H'42'
	MOVWF	H'43'
	MOVWF	H'44'	
	MOVWF	H'45'	
	MOVWF	H'46'	
	MOVWF	H'47'	

	MOVLW	H'80'
	MOVWF	H'32'	


MAIN
	BSF	ADCON0,GO	;A/D 0 START

;ADWAIT LOOP = 23*n+1
	MOVLW	H'08'	;LOOP START
	MOVWF	H'49'
ADWAIT
 	CALL WAIT20
	DECFSZ	H'49',1
	GOTO ADWAIT	;LOOP END
	CALL WAIT6

; DATA INPUT 625STEP
; DATA OUTPUT 125STEP

LOOP
	MOVF	ADRESH,0
	MOVWF	H'30'	;FF-80-00

	MOVF	H'32',0	
;	CSB H32,H30	
	MOVF H'30',0
	SUBWF	H'32',0
        BTFSC H'3',0

	GOTO GENSHOU ;MAINASU
;
	MOVF H'32',0	
	SUBWF H'30',0	
	MOVWF H'33'	
	MOVLW H'0F'
	MOVWF H'34'
	MOVLW H'20'
	SUBWF H'33',0
	BTFSS H'3',0
        DECF H'34',1                                         
	MOVLW H'19'
	SUBWF H'33',0
	BTFSS H'3',0
        DECF H'34',1                                         
	MOVLW H'13'
	SUBWF H'33',0
	BTFSS H'3',0
        DECF H'34',1                                         
	MOVLW H'0D'
	SUBWF H'33',0
	BTFSS H'3',0
        DECF H'34',1                                         
	MOVLW H'08'
	SUBWF H'33',0
	BTFSS H'3',0
        DECF H'34',1                                         
	MOVLW H'05'
	SUBWF H'33',0
	BTFSS H'3',0
        DECF H'34',1                                         
	MOVLW H'02'
	SUBWF H'33',0
	BTFSS H'3',0
        DECF H'34',1                                         
	GOTO DENSOU_OK


GENSHOU

	MOVF H'30',0	;A/DKEKKA > W
	SUBWF H'32',0	
	MOVWF H'33'	
	MOVLW H'00'	
	MOVWF H'34'
	MOVLW H'20'
	SUBWF H'33',0
	BTFSS H'3',0
        INCF H'34',1     
	MOVLW H'19'
	SUBWF H'33',0
	BTFSS H'3',0
        INCF H'34',1     
	MOVLW H'13'
	SUBWF H'33',0
	BTFSS H'3',0
        INCF H'34',1                                         
	MOVLW H'0D'
	SUBWF H'33',0
	BTFSS H'3',0
        INCF H'34',1                                         
	MOVLW H'08'
	SUBWF H'33',0
	BTFSS H'3',0
        INCF H'34',1                                         
	MOVLW H'05'
	SUBWF H'33',0
	BTFSS H'3',0
        INCF H'34',1                                         
	MOVLW H'02'
	SUBWF H'33',0
	BTFSS H'3',0
        INCF H'34',1                                         


DENSOU_OK

	MOVF H'34',0
	MOVWF H'40'	

MAKE_PARITY

	MOVLW H'00'
	MOVWF H'36'	
        BTFSC H'34',0 	
	INCF H'36',1
        BTFSC H'34',1 	
	INCF H'36',1
        BTFSC H'34',2 	
	INCF H'36',1
        BTFSC H'34',3 	
	INCF H'36',1
        BTFSC H'36',0	
	BSF H'40',4	



	BTFSS H'34',3	;1=NEXT SKIP
	GOTO GENSHOUD

	MOVLW H'0F'
	SUBWF H'34',0
	BTFSS H'3',2
	GOTO DAIE
        MOVLW H'24'
	MOVWF H'35'
DAIE
	MOVLW H'0E'
	SUBWF H'34',0
	BTFSS H'3',2
	GOTO DAID
        MOVLW H'1C'
	MOVWF H'35'
DAID
	MOVLW H'0D'
	SUBWF H'34',0
	BTFSS H'3',2
	GOTO DAIC
        MOVLW H'15'
	MOVWF H'35'
DAIC
	MOVLW H'0C'
	SUBWF H'34',0
	BTFSS H'3',2
	GOTO DAIB
        MOVLW H'0F'
	MOVWF H'35'
DAIB
	MOVLW H'0B'
	SUBWF H'34',0
	BTFSS H'3',2
	GOTO DAIA
        MOVLW H'0A'
	MOVWF H'35'
DAIA
	MOVLW H'0A'
	SUBWF H'34',0
	BTFSS H'3',2
	GOTO DAI9
        MOVLW H'06'
	MOVWF H'35'
DAI9
	MOVLW H'09'
	SUBWF H'34',0
	BTFSS H'3',2
	GOTO DAI8
        MOVLW H'03'
	MOVWF H'35'
DAI8
	MOVLW H'08'
	SUBWF H'34',0
	BTFSS H'3',2
	GOTO DAIX
        MOVLW H'01'
	MOVWF H'35'
DAIX

	MOVF H'35',0   ;DAIHOUCHI> W
	ADDWF H'32',1  ;NAIBUCHI+DAIHYOUCHI=NAIBUCHI
	BTFSS H'3',0	;IF CALLY=1 THEN NEXT SKIP
	GOTO DAIX2
	MOVLW H'FF'
	MOVWF H'32'
	GOTO DAIX3
DAIX2
	NOP
	NOP
	NOP

DAIX3
        NOP
	GOTO DAIZ

GENSHOUD

	MOVLW H'00'
	SUBWF H'34',0
	BTFSS H'3',2
	GOTO DAI1
        MOVLW H'24'
	MOVWF H'35'
DAI1
	MOVLW H'01'
	SUBWF H'34',0
	BTFSS H'3',2
	GOTO DAI2
        MOVLW H'1C'
	MOVWF H'35'
DAI2
	MOVLW H'02'
	SUBWF H'34',0
	BTFSS H'3',2
	GOTO DAI3
        MOVLW H'15'
	MOVWF H'35'
DAI3
	MOVLW H'03'
	SUBWF H'34',0
	BTFSS H'3',2
	GOTO DAI4
        MOVLW H'0F'
	MOVWF H'35'
DAI4
	MOVLW H'04'
	SUBWF H'34',0
	BTFSS H'3',2
	GOTO DAI5
        MOVLW H'0A'
	MOVWF H'35'
DAI5
	MOVLW H'05'
	SUBWF H'34',0
	BTFSS H'3',2
	GOTO DAI6
        MOVLW H'06'
	MOVWF H'35'
DAI6
	MOVLW H'06'
	SUBWF H'34',0
	BTFSS H'3',2
	GOTO DAI7
        MOVLW H'03'
	MOVWF H'35'
DAI7
	MOVLW H'07'
	SUBWF H'34',0
	BTFSS H'3',2
	GOTO DAIY
        MOVLW H'01'
	MOVWF H'35'

DAIY


	MOVF H'35',0   ;DAIHYOUCHI > W
	SUBWF H'32',1	;H32-W > H32
	BTFSC H'3',0	;IF CALLY=0 THEN NEXT SKIP
	GOTO DAIY2
	MOVLW H'00'
	MOVWF H'32'
	GOTO DAIY3
DAIY2
	NOP
	NOP
	NOP

DAIY3
	GOTO DAIZ



DAIZ


	BSF	ADCON0,GO	;A/D1 START
;kokomade 113 step
;	CALL WAIT8
	MOVF	H'40',01
	MOVWF	H'06'	;DATA OUT(RB0-7) FOR CHECK
	CALL WAIT6

;OUT-DATA1(LSB)  kokomade 121 step
	MOVLW	H'01'
	BTFSC	H'40',H'00'	;IF O THEN SKIP
	MOVLW	H'08'
	MOVWF	H'07'		
	CALL WAIT24
	CALL WAIT24
	CALL WAIT24
	CALL WAIT24
	CALL WAIT24
	NOP

;OUT-DATA2
	MOVLW	H'01'
	BTFSC	H'40',H'01'	;IF O THEN SKIP
	MOVLW	H'08'
	MOVWF	H'07'		
	CALL WAIT24
	CALL WAIT24
	CALL WAIT24
	CALL WAIT24
	CALL WAIT24
	NOP

;OUT-DATA3
	MOVLW	H'01'
	BTFSC	H'40',H'02'	;IF O THEN SKIP
	MOVLW	H'08'
	MOVWF	H'07'		
	CALL WAIT24
	CALL WAIT24
	CALL WAIT24
	CALL WAIT24
	CALL WAIT24
	NOP

;OUT-DATA4(MSB)
	MOVLW	H'01'
	BTFSC	H'40',H'03'	;IF O THEN SKIP
	MOVLW	H'08'
	MOVWF	H'07'		
	CALL WAIT24
	CALL WAIT24
	CALL WAIT24
	CALL WAIT24
	CALL WAIT24
	NOP

;OUT-DATA5(PARITY)
	MOVLW	H'01'
	BTFSC	H'40',H'04'	;IF O THEN SKIP
	MOVLW	H'08'
	MOVWF	H'07'		

	GOTO LOOP	







WAIT24	nop
	nop
WAIT22	nop
	nop
WAIT20	nop
	nop
WAIT18	nop
	nop
WAIT16	nop
	nop
WAIT14	nop
	nop
WAIT12	nop
	nop
WAIT10	nop
	nop
WAIT8	nop
	nop
WAIT6	nop
	nop
	return	;goto-2 return-2 and nop


	end

