;; DONNIE JAMES   14, JULY 2004  -  22, JULY 2004
;;
;; RS232 COMM COILGUN STAGE TIMER - UPTO 8 STAGES
;; OBJECTIVE: CONITUALLY WAIT FOR FIRE TRIGGER AND
;;            CAPCHARGE VERIFY INPUTS.  WHEN CONDITION IS
;;            MET, SEND OUTPUT SIGNALS TO BUFFER/DRIVERS.
;;            DRIVERS THEN SEND OUTPUT TO SCR'S.
;; 
;; PIC
;; PIN  I/O  FROM             TO
;;-----------------------------------------------
;; A0   O 
;; A1   O 		STATUS LED
;; A2   O   
;; A3   O   
;; A4   O   
;; A5  (I)
;; A6   O
;; A7   O
;;
;; B0   I		FIRE SWITCH      
;; B1   I    		CAPCHARGE VERIFY
;; B2   O    
;; B3   O     		DRIVER 1
;; B4   O    		DRIVER 2
;; B5   O    		DRIVER 3
;; B6   O    		DRIVER 4
;; B7   O    		DRIVER 5
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
         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 FIRETRIG PORTB, 0		;INPUT S1(TRIGGER)
#DEFINE CAPCHARGE PORTB, 1		;INPUT 450V VERIFY

#DEFINE SIG1 PORTB, 3		;OUTPUT SIG1
#DEFINE SIG2 PORTB, 4		;OUTPUT SIG2
#DEFINE SIG3 PORTB, 5		;OUTPUT SIG3
#DEFINE SIG4 PORTB, 6		;OUTPUT SIG4
#DEFINE SIG5 PORTB, 7		;OUTPUT SIG5
#DEFINE SIG6 PORTA, 0		;OUTPUT SIG6
#DEFINE SIG7 PORTA, 1		;OUTPUT SIG7
#DEFINE SIG8 PORTA, 2		;OUTPUT SIG8
#DEFINE BOOTLED PORTA, 3		;OUTPUT REBOOT LED
	
	CBLOCK 0X20; TEMPORARY STORAGE-------------
	CNTJ, CNTK, CNTL, CNT1, CNT2, CNT3, CNT4, CNT5
	CNT6, CNT7, CNT8, CNT9, CNT10
	MS_TIME, MS_TIME_TEMP, TEMP, TEMP2, TEMP_HI, TEMP_LO
	LO_BYTE, HI_BYTE
	;-------------------------------
	TIME1, TIME2, TIME3, TIME4, TIME5, TIME6, TIME7, TIME8
	LOOPCOUNT	
	ENDC;--------END OF C BLOCK-----------------
	
	;------PORT I/O CONFIGURATION/SETUP-------------------------------
	BANK0
	MOVLW	0X07
	MOVWF	CMCON			;DISABLE CAPTURE/COMPARE MODULES.

	BCF	STATUS, RP1
         BANK1
         LOADF    TRISA, B'00100000'
         LOADF    TRISB, B'00000011'	
         BSF	OPTION_REG, NOT_RBPU	;BCF/BSF = ENABLE/DISABLE PULL-UP.
	;------POWER ON RESET CONFIGURATION/SETUP--------------------------
	BSF	PCON, OSCF	;BCF/BSF = 37KHZ/4MHZ INTRC OR ER OSCILLATOR SPEED.
	;------INTERRUPT CONFIGURATION/SETUP-------------------------------
	BANK1
	BCF	INTCON, GIE  	;BCF/BSF = DIS/ENABLE     GLOBAL INTERRUPT.
	BCF	INTCON, T0IE	;BCF/BSF = DIS/ENABLE     TIMER1 INTERRUPT.
	BCF	INTCON, INTE	;BCF/BSF = DIS/ENABLE     RB0    INTERRUPT.
	BCF	INTCON, RBIE	;BCF/BSF = DIS/ENABLE     RB7-4  INTERRUPT.
	BCF	PIE1, TMR1IE  	;BCF/BSF = DIS/ENABLE     TIMER1 INTERRUPT.
	;------------------------------------------------------------------
 	;-----TIMER0 CONFIGURATION/SETUP-----------------------------------
 	BANK1
	BCF     OPTION_REG, T0CS 	;BCF/BSF = INTERNAL/EXTERNAL CLOCK. 
	BSF     OPTION_REG, PSA  	;BCF/BSF = ENABLE/DISABLE PRESCALER RATIOS.
	;PRESCALER RATIO:	 000=1:2   001=1:4   010=1:8    011=1:16
	;		 100=1:32  101=1:64  110=1:128  111=1:256
         BSF      OPTION_REG, PS0
         BSF      OPTION_REG, PS1
         BSF      OPTION_REG, PS2         
	;-----USART CONFIGURATION/SETUP------------------------------------
	BANK1
	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 9.6 KBPS.
	BANK0
	BCF	RCSTA, SPEN	;BCF/BSF = DIS/ENABLE          SERIAL PORT.
	BCF	RCSTA, RX9	;BCF/BSF = 8-BIT/9-BIT        TRANSMISSION.
	BCF	RCSTA, SREN	;BCF/BSF = DIS/ENABLE       SINGLE RECEIVE.	
	BCF	RCSTA, CREN	;BCF/BSF = DIS/ENABLE    CONTINUOS RECEIVE.
	;--- -TIMER1 CONFIGURATION/SETUP-----------------------------------
         BANK0
	BCF	T1CON, TMR1ON 	;BCF/BSF = DISABLE/ENABLE       TIMER1 ON.
	BCF	T1CON, TMR1CS 	;BCF/BSF = INTERNAL/EXTERNAL(RA6)   CLOCK.
	;------------------------------------------------------------------
;*********MAIN**************************
	CALL	FLASHBOOT
	BCF	SIG1
	BCF	SIG2
	BCF	SIG3
	BCF	SIG4
	BCF	SIG5
	BCF	SIG6
	BCF	SIG7
	BCF	SIG8
MAIN:
	LOADF	TIME1, .1
	LOADF	TIME2, .46	
	LOADF	TIME3, .24
	LOADF	TIME4, .23
	LOADF	TIME5, .15
	LOADF	TIME6, .10	
	LOADF	TIME7, .7	
	
	LOADF	TIME8, .8	
	
	CALL	CHECK_TRIGGER
	CALL	SEND_SIGS
	GOTO	MAIN

;**********END 0F MAIN******************
CHECK_TRIGGER:
	BTFSS	FIRETRIG			;IF FIRETRIG AND CAPCHARGE,
	GOTO	$ - 1			;OR SET 
	BTFSS	CAPCHARGE		;THEN- CONTINUE
	GOTO	$ - 3			;ELSE- LOOP TILL READY
	
	;FLASH STATUS LED 3 TIMES BEFORE FIRING
	BSF	BOOTLED			;FLASH 1
	LOADF	MS_TIME, .100
	CALL	MS_DELAY
	BCF	BOOTLED
	LOADF	MS_TIME, .100
	CALL	MS_DELAY
	BSF	BOOTLED			;FLASH 2
	LOADF	MS_TIME, .100
	CALL	MS_DELAY
	BCF	BOOTLED
	LOADF	MS_TIME, .100
	CALL	MS_DELAY
	BSF	BOOTLED			;FLASH 3
	LOADF	MS_TIME, .100
	CALL	MS_DELAY
	BCF	BOOTLED
	LOADF	MS_TIME, .100
	CALL	MS_DELAY
	RETURN
;---------------------------------------
SEND_SIGS:
	MOVF_F	TIME1, LOOPCOUNT		;SIGNAL STAGE1
	CALL	LOOP_50USEC
	BSF	SIG1
	MOVF_F	TIME2, LOOPCOUNT		;SIGNAL STAGE2
	CALL	LOOP_50USEC
	BSF	SIG2
	MOVF_F	TIME3, LOOPCOUNT		;SIGNAL STAGE3
	CALL	LOOP_50USEC
	BSF	SIG3
	MOVF_F	TIME4, LOOPCOUNT		;SIGNAL STAGE4
	CALL	LOOP_50USEC
	BSF	SIG4
	MOVF_F	TIME5, LOOPCOUNT		;SIGNAL STAGE5
	CALL	LOOP_50USEC
	BSF	SIG5
	MOVF_F	TIME6, LOOPCOUNT		;SIGNAL STAGE6
	CALL	LOOP_50USEC
	BSF	SIG6
	MOVF_F	TIME7, LOOPCOUNT		;SIGNAL STAGE7
	CALL	LOOP_50USEC
	BSF	SIG7


	LOADF	MS_TIME, .1		;CLEAR ALL STAGES
	CALL	MS_DELAY
	BCF	SIG1
	BCF	SIG2
	BCF	SIG3
	BCF	SIG4
	BCF	SIG5
	BCF	SIG6
	BCF	SIG7
	RETURN
;---------------------------------------
;TURN ON "PIC READY" LED ON POWER-UP
FLASHBOOT:
	BSF	BOOTLED			;LED ON FOR 1/2 SECOND
	LOADF	MS_TIME, .250
	CALL	MS_DELAY
	LOADF	MS_TIME, .250
	CALL	MS_DELAY

	BCF	BOOTLED			;LED OFF FOR 1/4 SECOND
	LOADF	MS_TIME, .250
	CALL	MS_DELAY
	RETURN
;---------------------------------------
LOOP_50USEC:
	CALL	USEC_20
	CALL	USEC_20
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	DECFSZ	LOOPCOUNT, F
	GOTO	LOOP_50USEC
	RETURN
;---------------------------------------
;USE THIS DELAY BEFORE SENDING DATA TO TXREG.
TXWAIT:
	BANK1
	BTFSS	TXSTA, TRMT	;IF TX IS COMPLETED,
	GOTO	$ - 1		;ELSE, WAIT
	BANK0			;THEN, CONTINUE.
	LOADF	MS_TIME, .1
	CALL	MS_DELAY
	RETURN
;--------------------------------------

;------------------------------------
;    GENERAL TIMERS AND DELAYS      ;
;------------------------------------
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
