; BCO1
; Program for simple block cutout unit
; Now at 4 MHz for 12F675 
; Uses the comparator to detect overloads
; Uses the A/D and pot. to set the delay time from overload to trip
; Has opto for indicating overload

	
	processor 12F675
	include "p12F675.inc"
	 

	__CONFIG	h'3FE4' ; CP off, WDT off, internal clock, BOR


;Definitions





	CBLOCK	0x0020
	delval		;delay value from A/D
	flag		;set if overload exceeded
	count
	
	
	ENDC

	org	0000	;setup 
	bsf		STATUS,RP0		;set oscillator
	call	0x3ff
	movwf	OSCCAL
	movlw	B'00001111'		;GP4 and GP5 are outputs
	movwf	TRISIO
	movlw	B'00010100'
	movwf	ANSEL
	movlw	B'10000000'		;prescaler to Timer0
	movwf	OPTION_REG
	bcf		STATUS,RP0
	movlw	B'00010010'
	movwf	CMCON			;set comparator
	movlw	B'00001001'		;set A/D
	movwf	ADCON0
	call	read_AD			;get current delay value
;	
	



	clrf	GPIO
	bsf		GPIO,GP5		;DCC on		
	
main	btfss	CMCON,COUT	;overload?
		goto	main
		movlw	6
		movwf	count
main0	decfsz	count,F
		goto	main0	
		btfss	CMCON,COUT	;still there
		goto	main		;no so short spike
		movlw	B'00000100'
		movwf	T1CON		;set prescale to 1
		clrf	TMR1L
		movf	delval,W
		movwf	TMR1H
		bsf		T1CON,0		;start T1
		bcf		PIR1,TMR1IF
main1	clrf	flag
		movlw	0x9B		;timer 0 for 200usec
		movwf	TMR0		;start
		bcf		INTCON,T0IF
main2	btfsc	CMCON,COUT
		bsf		flag,0		;flag an overload
		btfss	INTCON,T0IF	;time out?
		goto	main2
		btfsc	flag,0		;no overload
		goto	main3
		goto	main
main3	btfss	PIR1,TMR1IF	;delay out?
		goto	main1		;no
		
		

cutout	bcf	GPIO,GP5
		bsf	GPIO,GP4
		movlw	B'00110100'
		movwf	T1CON			;PS ck/8
		call	read_AD
		bcf		PIR1,TMR1IF
		clrf	TMR1H
		clrf	TMR1L		;full count
		bsf		T1CON,0		;start
		
loop	btfss	PIR1,TMR1IF		;time out?
		goto	loop
		bcf		GPIO,GP4
		bsf		GPIO,GP5		;turn on again
		goto	main
		
;*********************************************************
;		read A/D to get delay time
	 
read_AD	bsf		ADCON0,1		;start
adloop	btfsc	ADCON0,1		;finished?
		goto	adloop
		movf	ADRESH,W
		movwf	delval
		return	
		
		end
