S;; DONNIE JAMES   18, OCT 2004
;;
;; BATTERY POWERED MULTISTAGE CG
;; 1. RS232 TX & RX.
;; 2. PC INTERFACE IN PLACE OF LCD.
;; 3. 1 to 3 INJECTOR STAGES.
;;
;; PIC
;; PIN  I/O  FROM             TO
;;-----------------------------------------------
;; A0   O
;; A1   I    VREF
;; A2   I    V_DIVIDE
;; A3   I    
;; A4   I    BTN_CHARGE
;; A5  (I)   BTN_FIRE
;; A6   X    XTL	
;; A7   X    XTL
;;
;; B0   I    		LED
;; B1   I   
;; B2   O   
;; B3   O    
;; B4   O    		SIG1OUT	
;; B5   O    		SIG2OUT 	
;; B6   O    		B_SIG
;; B7   O    		B_EN	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
        LIST    P=16F628
        INCLUDE "P16F628A.INC"
        INCLUDE "SHORTS.INC"
        __CONFIG _LVP_OFF & _MCLRE_OFF & _XT_OSC & _BODEN_OFF & _CP_OFF & _WDT_OFF & _PWRTE_OFF
        ERRORLEVEL -302

#DEFINE B_EN PORTB, 7		;OUTPUT TO DRIVER1
#DEFINE B_SIG PORTB, 6		;OUTPUT TO DRIVER2
#DEFINE SIG1OUT PORTB, 4		;OUTPUT TO DRIVER3
#DEFINE SIG2OUT PORTB, 5		;OUTPUT TO DRIVER4
#DEFINE SIG3OUT PORTB, 3		;OUTPUT TO DRIVER5
#DEFINE LED PORTB, 0		;OUTPUT

#DEFINE BTN_CHARGE PORTA, 4	;INPUT 
#DEFINE BTN_FIRE PORTA, 5		;INPUT 
	CBLOCK 0X20;----BEGIN C BLOCK RAM STORAGE----
	CNTJ, CNTK, CNTL, CNT1, DECOUNT
	MS_TIME, MS_TIME_TEMP, TEMP, TEMP_MUL, TEMP_HI, TEMP_LO
		;//--------------------------------//
	CAP_FULL, LOOPCOUNT, RATE_FIRE_HI, RATE_FIRE_LO
         STAGE1_ENABLED, STAGE1_ON, STAGE1_OFF
	STAGE2_ENABLED, STAGE2_ON, STAGE2_OFF
         ADDRESS, INJECTOR_VALID, MENU, MONITOR_BYTE
	ENDC;--------END OF C BLOCK-----------------
	;------PORT I/O CONFIGURATION/SETUP-------------------------------
         BANK1
         LOADF	TRISA, B'11111110'
         LOADF    TRISB, B'00000010'	;SET BIT 1, CLEAR BIT 2 FOR USART.
         BSF	OPTION_REG, NOT_RBPU	;BCF/BSF = ENABLE/DISABLE PULL-UP.
	;------COMPARATOR CONFIGURATION/SETUP---------------------------
	BANK0
	BSF	CMCON, CM2	;MODE=101  ONE INDEPENDENT COMPARATOR
	BCF	CMCON, CM1	;VIN(+)1 = RA3  VIN(-)1 = RA0
	BSF	CMCON, CM0	;VIN(+)2 = RA2  VIN(-)2 = RA1
	BCF	CMCON, C1INV	;BCF/BSF = NOT/INVERTED OUTPUT C1OUT
	BSF	CMCON, C2INV	;BCF/BSF = NOT/INVERTED OUTPUT C2OUT
	BCF	PIR1, CMIF	;CLEAR COMPARATOR FLAG.
	BANK1	;------POWER ON RESET CONFIGURATION/SETUP-------------------
	BSF	PCON, OSCF	;C/S = 37KHZ/4MHZ     OSCILLATOR SPEED.
	BANK1	;------INTERRUPT CONFIGURATION/SETUP------------------------
	BCF	INTCON, GIE  	;C/S = DIS/ENABLE     GLOBAL INTERRUPT.
	BCF	INTCON, T0IE	;C/S = DIS/ENABLE     TIMER1 INTERRUPT.
	BCF	INTCON, INTE	;C/S = DIS/ENABLE     RB0    INTERRUPT.
	BCF	INTCON, RBIE	;C/S = DIS/ENABLE     RB7-4  INTERRUPT.
	BCF	INTCON, PEIE	;C/S = DIS/ENABLE PERIPHERAL INTERRUPTS.
	BCF	PIE1, CMIE	;C/S = DIS/ENABLE	    COMPARATOR INTERRUPT.
	BCF	PIE1, TMR1IE  	;C/S = DIS/ENABLE     TIMER1 INTERRUPT.
	BCF	PIE1, EEIE  	;C/S = DIS/ENABLE     EEPROM INTERRUPT.
 	BANK1 	;-----TIMER0 CONFIGURATION/SETUP-----------------------------
	BCF   	OPTION_REG, T0CS 	;C/S = INTERNAL/EXTERNAL CLOCK. 
	BSF      OPTION_REG, PSA  	;C/S = ENABLE/DISABLE PRESCALER RATIOS.
         BSF      OPTION_REG, PS0	; PRESCALER RATIO:	
         BSF      OPTION_REG, PS1	; 000=1:2  001=1:4  010=1:8   011=1:16
         BSF      OPTION_REG, PS2   ; 100=1:32 101=1:64 110=1:128 111=1:256      
         BANK0	;----TIMER1 CONFIGURATION/SETUP------------------------------
	BSF	T1CON, TMR1ON 	;C/S = DISABLE/ENABLE       TIMER1 ON.
	BCF	T1CON, TMR1CS 	;C/S = IN/EXTERNAL(RA6)         CLOCK.
	BANK1	;-----USART CONFIGURATION/SETUP-------TRANSMIT----------------
	BCF	PIE1, TXIE	;BCF/BSF = DIS/ENABLE         TX INTERRUPT.
	BSF 	TXSTA, CSRC       ;BCF/BSF = EXT/INTERNAL          GENERATOR.
	BCF	TXSTA, TX9  	;BCF/BSF = 8-BIT/9-BIT        TRANSMISSION.
	BCF	TXSTA, TXEN	;BCF/BSF = DIS/ENABLE         TRANSMISSION.
	BCF	TXSTA, SYNC	;BCF/BSF = A/SYNCHRONOUS              MODE.
	BSF	TXSTA, BRGH	;BCF/BSF = LO/HI SPEED BAUD RATE SELECT.
	LOADF	SPBRG, .12 	;SET BAUD RATE TO 19200 KBPS.
	BANK1	;-----USART CONFIGURATION/SETUP-------RECEIVE-----------------
	BANK0
	BCF	RCSTA, SPEN	;BCF/BSF = DIS/ENABLE          SERIAL PORT.
	BCF	RCSTA, RX9	;BCF/BSF = 8-BIT/9-BIT           RECEPTION.
	BCF	RCSTA, SREN	;BCF/BSF = DIS/ENABLE       SINGLE RECEIVE.	
	BSF	RCSTA, CREN	;BCF/BSF = DIS/ENABLE    CONTINUOS RECEIVE.
	BANK1	;-----VOLTAGE REFERENCE MODULE--------------------------------
	BSF	VRCON, VREN	;BCF/BSF = DIS/ENABLE		VREF.	
	BSF	VRCON, VROE	;BCF/BSF = DO NOT/DO     SEND TO RA2 PIN.
	BCF	VRCON, VRR	;BSF/BCF = LOW/HIGH      RANGE SELECTION.
	BSF	VRCON, VR3	;VREF VALUE SELECTION:
	BSF	VRCON, VR2        ;
	BCF	VRCON, VR1	;VRR=1: VREF=(VR<3:0>/ 24)*VDD
	BSF	VRCON, VR0        ;VRR=0: VREF=(1/4*VDD) + (VR<3:0>/ 32)*VDD
	BANK0	;-------------------------------------------------------------
;*********MAIN**************************
	BCF	B_EN
	BCF 	B_SIG
	BCF	SIG1OUT
	BCF	SIG2OUT
	BSF	LED
	BCF	CAP_FULL, 0
	CALL	FLASH2X
	LOADF	STAGE1_ON, .4
	LOADF	STAGE1_OFF, .56
	LOADF	STAGE2_ON, .4
	LOADF	STAGE2_OFF, .56
MAIN:	
	CALL	MONITOR
	GOTO	MAIN
;**********END 0F MAIN******************
BOOSTER:	; DUTY CYCLE = 75% OF 4.0KHZ: 495-ON, 165-OFF
	; INCREASE FREQUENCY FOR LOW CURRENT SOURCE.
	BTFSC	CAP_FULL, 0
	RETURN	
	BCF	B_EN
	BSF	B_SIG
	CALL	USEC_100
	CALL	USEC_20
	CALL	USEC_20
	BSF	B_EN
	BCF	B_SIG
	CALL	USEC_20
	CALL	USEC_10
	CALL	USEC_10
	NOP
	NOP
	NOP
	BCF	B_EN
					;CHECK IF CAPAPACITOR IS FULL.
	BTFSS	CMCON, C2OUT		;IF CAP IS FULL,
	GOTO	B1			;ELSE- CONTINUE,
	BSF	CAP_FULL, 0		;THEN- DISABLE CHARGING.
	BCF	LED
B1	RETURN
;---------------------------------------
MONITOR:
	CALL	BOOSTER			;THEN- CHARGE CAPACITOR.
	BTFSC	BTN_FIRE			;ELSE- CONTINUE.
	CALL	SEND_SIGS	
	GOTO	MONITOR
	RETURN
;---------------------------------------
;ROUTINE TO PROCESS INJECTORS AND STAGES.
SEND_SIGS:
	LOADF	MS_TIME, .5		;WAIT FOR DEBOUNCE.
	CALL	MS_DELAY			
	BTFSS	BTN_FIRE			;IF BUTTON IS LOW
	RETURN				;THEN- CANCEL FIRE
					;ELSE- CONTINUE.
	BTFSC	BTN_FIRE			;WAIT TILL BUTTON IS RELEASED.
	GOTO	$ - 1
	
	BCF	CAP_FULL, 0
	BSF	LED

	;1 FIRE EACH STAGE IN SEQUENCE.  DISABLED STAGES ARE SKIPPED.
	;2 IF CONFIG TIME = 255, THAT STAGE IS DISABLED.
	;-------STAGE SELECTION-----------------------
SSIG1	BSF	SIG1OUT			;PULSE STAGE1.
	MOVF_F	STAGE1_ON, LOOPCOUNT		
	CALL	LOOP_50USEC
	BCF	SIG1OUT			
	MOVF_F	STAGE1_OFF, LOOPCOUNT	
	CALL	LOOP_50USEC

SSIG2	BSF	SIG2OUT			;PULSE STAGE2.
	MOVF_F	STAGE2_ON, LOOPCOUNT		
	CALL	LOOP_50USEC
	BCF	SIG2OUT			
	MOVF_F	STAGE2_OFF, LOOPCOUNT		
	CALL	LOOP_50USEC
	
	LOADF	MS_TIME, .250		;WAIT TILL SCR TURNS OFF.
	CALL	MS_DELAY
	CALL	MS_DELAY
	CALL	MS_DELAY
	CALL	MS_DELAY	
SSEND	RETURN	
;---------------------------------------
FLASH2X:	LOADF	MS_TIME, .100
	BCF	LED
	CALL	MS_DELAY
	BSF	LED
	CALL	MS_DELAY
	BCF	LED
	CALL	MS_DELAY
	BSF	LED
	CALL	MS_DELAY
	RETURN
FLASH3X:
	BCF	LED
	LOADF	MS_TIME, .200
	CALL	MS_DELAY
	BSF	LED
	LOADF	MS_TIME, .200
	CALL	MS_DELAY
	BCF	LED
	LOADF	MS_TIME, .200
	CALL	MS_DELAY
	BSF	LED
	LOADF	MS_TIME, .200
	CALL	MS_DELAY
	BCF	LED
	LOADF	MS_TIME, .200
	CALL	MS_DELAY
	BSF	LED
	LOADF	MS_TIME, .200
	CALL	MS_DELAY
	RETURN
FLASH5X:
	BCF	LED
	LOADF	MS_TIME, .200
	CALL	MS_DELAY
	BSF	LED
	LOADF	MS_TIME, .200
	CALL	MS_DELAY
	BCF	LED
	LOADF	MS_TIME, .200
	CALL	MS_DELAY
	BSF	LED
	LOADF	MS_TIME, .200
	CALL	MS_DELAY
	BCF	LED
	LOADF	MS_TIME, .200
	CALL	MS_DELAY
	BSF	LED
	LOADF	MS_TIME, .200
	CALL	MS_DELAY
	BCF	LED
	LOADF	MS_TIME, .200
	CALL	MS_DELAY
	BSF	LED
	LOADF	MS_TIME, .200
	CALL	MS_DELAY
	BCF	LED
	LOADF	MS_TIME, .200
	CALL	MS_DELAY
	BSF	LED
	LOADF	MS_TIME, .200
	CALL	MS_DELAY
	RETURN
FLASH3XF:
	BSF	LED
	LOADF	MS_TIME, .50
	CALL	MS_DELAY
	BCF	LED
	LOADF	MS_TIME, .50
	CALL	MS_DELAY
	BSF	LED
	LOADF	MS_TIME, .50
	CALL	MS_DELAY
	BCF	LED
	LOADF	MS_TIME, .50
	CALL	MS_DELAY
	BSF	LED
	LOADF	MS_TIME, .50
	CALL	MS_DELAY
	BCF	LED
	LOADF	MS_TIME, .200
	CALL	MS_DELAY
	RETURN
;---------------------------------------
LOOP_50USEC:
	INCF	LOOPCOUNT, F
LP1	DECFSZ	LOOPCOUNT, F
	GOTO	LP2
	GOTO	LP3
LP2	CALL	USEC_20
	CALL	USEC_20
	NOP
	NOP
	NOP
	NOP
	GOTO	LP1
LP3	RETURN
;---------------------------------------
USEC_10:	GOTO	$ + 1
	GOTO	$ + 1
	GOTO	$ + 1
	RETURN
USEC_20:
	GOTO	$ + 1
	GOTO	$ + 1
	GOTO	$ + 1
	GOTO	$ + 1
	GOTO	$ + 1
	GOTO	$ + 1
	GOTO	$ + 1
	NOP
	NOP
	RETURN
USEC_100:
	CALL	USEC_20
	CALL	USEC_20
	CALL	USEC_20
	CALL	USEC_20
	GOTO	$ + 1
	GOTO	$ + 1
	GOTO	$ + 1
	GOTO	$ + 1
	GOTO	$ + 1
	GOTO	$ + 1
	GOTO	$ + 1
	NOP
	NOP
	RETURN
;---------------------------------------
;MILLI SECOND DELAY ROUTINE
;DELAY = X MILLI-SECOND (4MHZ CLOCK).
MS_DELAY:
	MOVF_F	MS_TIME, MS_TIME_TEMP
MS_LOOP	MOVLW	0XA4
	MOVWF	CNTJ
		DECFSZ	CNTJ, F
	GOTO	$ - 1
	MOVLW	0XA4
	MOVWF	CNTJ
	DECFSZ	CNTJ, F
	GOTO	$ - 1

	DECFSZ	MS_TIME_TEMP, F	;IF COUNTER ENDS,
	GOTO	MS_LOOP		;ELSE- REPEAT
	RETURN			;THEN- EXIT LOOP
;---------------------------------------
	END
