;******************************************************************:
; This program is installed in the RFID transimitter.
; 37.9kHz modulated IrDa for PAV-MP1.
; This program is installed in the PIC which drives with 4MHz clock.
;******************************************************************
;
		LIST		P=PIC16F84A
		INCLUDE		"P16F84A.INC"
		__CONFIG		 _HS_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF 
;*******************************************
;  Define variables
;*******************************************
SDATA	EQU		0CH		;Send data 2Byte
CNT1		EQU		0EH		;Counter
CNT2		EQU		0FH		;Counter
CNT3		EQU		10H		;Counter
CNT4		EQU		15H		;Counter
CNT5		EQU		16H		;Counter
CNT6		EQU		17H		;Counter
BITCNT	EQU		11H		;Bit counter
WCNT		EQU		12H		;counter for 37.9kHz
W6CNT	EQU		13H		;counter for 600usec
WORK		EQU		14H		;work area

;*******************************************
;  Jump vector
;*******************************************
		ORG		0		;reset
		GOTO		MAIN
;		ORG		4		;interrupt(not use)
;		GOTO		START	

;*******************************************
;  MAIN routine
;*******************************************
MAIN
		BCF		INTCON,GIE	;Inhibit interrupt

		CALL		PORT_INI	;initialize
;******* main program **********
START
		BTFSC		PORTB,4		;ACC check (ACC 12V then RB4 0) 
		GOTO		ACC_0V		;ACC=0V so goto POWER OFF
		CALL		WAIT5		;ACC=12V wait 5 sec
		BTFSC		PORTB,4		;ACC check (ACC 12V then RB4 0) 
		GOTO		START		;ACC change while in work
		CALL		POWER_ON		;ACC=12V so goto POWER ON
		BTFSC		PORTB,4		;ACC check (ACC 12V then RB4 0) 
		GOTO		START		;ACC change while in work
		CALL		WAIT10
		BTFSC		PORTB,4		;ACC check (ACC 12V then RB4 0) 
		GOTO		START		;ACC change while in work
		CALL		LEED			;ENTER (resum)send
		CALL		ENTERSEND
		BTFSC		PORTB,4		;ACC check (ACC 12V then RB4 0) 
		GOTO		START		;ACC change while in work
		GOTO		SLEEP_R		;ACC=0V sleep and wait change
		
ACC_0V	
		CALL		LEED
		CALL		STOPSEND		;ACC is OFF so stop PAV-MP1
		CALL		WAIT5		;wait 5sec
		CALL		POWER_OFF		;POWER OFF
		BTFSC		PORTB,4		;ACC check (ACC 12V then RB4 0) 
		GOTO		SLEEP_R		;ACC=0V sleep and wait change
		GOTO		START		;ACC change while in work

;********** sleep ***************

SLEEP_R
		BCF			INTCON,0		;RBIF erase
;		BCF			INTCON,1		;INTF rtsdr
		BSF			INTCON,3		;Rb  INT-RBIE enable
;		BSF			INTCON,4		;INT INT-RBIE enable

		SLEEP					;power down mode
LOOP		BCF			INTCON,7		;GIE erase
		BTFSC		INTCON,7
		GOTO		LOOP
		BCF			INTCON,3		;Rb  INT RBIE disable
;		BCF			INTCON,4		;INT INT RBIE disable

		GOTO		START
			
					
;*******************************************
;************ Subroutines ****************
;*******************************************

;*******************************************
;**** send leed phase ****
;*******************************************
LEED
		MOVLW		00FH			;reset bit counter
		MOVWF		BITCNT
LDLP
		CALL		BIT1			;send 1 data
		DECFSZ		BITCNT,F		;test end
		GOTO		LDLP			;continue

		MOVLW		007H			;reset bit counter
		MOVWF		BITCNT
LDLP2
		CALL		BIT0			;send 1 data
		DECFSZ		BITCNT,F		;test end
		GOTO		LDLP2

		CALL		BIT1
		RETURN

;*******************************************
;****  PAV-MP1 stop code send ****
;*******************************************

STOPSEND
		MOVLW		040H			;SYSTEM CODE
		MOVWF		SDATA
		CALL		SEND8D
		
		MOVLW		0BFH			;DEVICE CODE
		MOVWF		SDATA
		CALL		SEND8D
		
		MOVLW		030H			;STOP CODE
		MOVWF		SDATA
		CALL		SEND8D
		
		MOVLW		0CFH			;STOP~ CODE
		MOVWF		SDATA
		CALL		SEND8D
		
		CALL		WAIT


		RETURN
;*******************************************
;****  PAV-MP1 enter code send ****
;*******************************************

ENTERSEND
		MOVLW		040H			;SYSTEM CODE
		MOVWF		SDATA
		CALL		SEND8D
		
		MOVLW		0BFH			;DEVICE CODE
		MOVWF		SDATA
		CALL		SEND8D
		
		MOVLW		0D0H			;ENTER CODE
		MOVWF		SDATA
		CALL		SEND8D
		
		MOVLW		02FH			;ENTER~ CODE
		MOVWF		SDATA
		CALL		SEND8D
		
		CALL		WAIT


		RETURN
;*******************************************
; Send 8data subroutine
;*******************************************
SEND8D
		MOVLW	08H
		MOVWF	CNT4
SS1		BTFSC	SDATA,7
		GOTO	SS2
		CALL	SEND0
		GOTO	SS3
SS2		CALL	SEND1
SS3		RLF		SDATA
		DECFSZ	CNT4
		GOTO	SS1
		RETURN

;*******************************************
; Send data 0 subroutine
;*******************************************
SEND0
		CALL	BIT0
		CALL	BIT1
		RETURN
;*******************************************
; Send data 1 subroutine
;*******************************************
SEND1
		CALL	BIT0
		CALL	BIT0
		CALL	BIT0
		CALL	BIT1
		RETURN


;*******************************************
; Send BIT 0 subroutine
;*******************************************
BIT0
		MOVLW		0CH			;output RA2,RA3 1
		MOVWF		PORTA
		MOVLW		0CH			;get counter for 600usec
		MOVWF		W6CNT
BT01LP
		MOVLW		0FH			;15 get for 37.9kHz
		MOVWF		WCNT
BT00LP
		DECFSZ		WCNT,F
		GOTO		BT00LP		;3*15-1+2=46		
;***
		NOP
		DECFSZ		W6CNT,F
		GOTO		BT01LP		;(46+4)*12-1+5=604USEC
		RETURN 

;*******************************************
; Send BIT 1 subroutine 
;  "1" :13usec  "0":13usec = 37.9kHz
;*******************************************
BIT1
		MOVLW		017H	 		;get counter for 600usec
		MOVWF		W6CNT
;**** low output for 37.9kHz ****
BTLOOP
		MOVLW		000H			;set on data
		MOVWF		PORTA		;on LED
		NOP
		NOP
		NOP
		NOP
		NOP						;during 13 step
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
;**** high output for 37.9kHz ****		
		MOVLW		0CH			;set port
		MOVWF		PORTA		;off LED
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP						;during 13 step
		NOP
		NOP
		MOVLW		07H			;set on data for next
;**** end test for 600usec ****
		DECFSZ		W6CNT,F
		GOTO		BTLOOP		
		RETURN					;26*23-1+4=601usec


;*******************************************
;  300msec timer subroutine
;*******************************************
WAIT
		MOVLW		0AH			;10
		MOVWF		CNT3
WTLP3
		MOVLW		063H			;99
		MOVWF		CNT2
WTLP2
		MOVLW		064H			;100
		MOVWF		CNT1
WTLP1
		DECFSZ		CNT1,F		
		GOTO		WTLP1		;3*100-1+2=301usec
;**
		DECFSZ		CNT2,F
		GOTO		WTLP2		;(301+3)*99-1+3=30098
;**
		DECFSZ		CNT3,F
		GOTO		WTLP3		;(30098+3)*10-1+3=301msec
		RETURN

;*******************************************
;  10sec timer subroutine
;*******************************************
WAIT5
		MOVLW		01EH			;33
		MOVWF		CNT5

WTLP5	CALL		WAIT			;300msec
		DECFSZ		CNT5,F
		GOTO		WTLP5
		RETURN

;*******************************************
;  60sec timer subroutine
;*******************************************
WAIT10
		MOVLW		006H			;6times
		MOVWF		CNT6

WTLP10	CALL		WAIT5		;10sec
		DECFSZ		CNT6,F
		GOTO		WTLP10
		RETURN
		
;*******************************************		
; POWER control subroutine
;*******************************************
POWER_ON

		MOVLW		0FCH
		ANDWF		PORTA,1		;POWER ON   (L = FET ON)

		RETURN

POWER_OFF

		MOVLW		003H
		IORWF		PORTA,1		;POWER OFF	(H = FET OFF)

		RETURN

;*******************************************
; PORT initial setup subroutine
;  PORTA: all output
;         RA0,RA1 main Power ON/OFF 
;         RA2,RA3 drive LED 
;  PORTB: all input
;         RB4 ACC input wakeup
;*******************************************
PORT_INI
		BSF			STATUS,RP0	;Set page 1
		MOVLW		0FFH			;all PORTB is input
		MOVWF		TRISB		;PortB set all input
		CLRF		TRISA		;PortA set all output
		BCF			OPTION_REG,7	;PORTB pull up enable
		BCF			STATUS,RP0	;Set Page 0
		MOVLW		00CH			;OUTPUT 0CH
		MOVWF		PORTA		;LED OFF,Main Power ON
		RETURN 	

;*******************************************		
; Program END
;*******************************************

		END         
