;3 Hour timer for charging the B&D drill battery
;internal 4MHz Oscillator

    ERRORLEVEL -302
	ERRORLEVEL -306

	list P=12F675
	#include P12F675.inc	;see Program files/Microchip/MPASM Suite/p12f675 & PIC book page 91

;Program Configuration Register 
		__CONFIG    _CPD_OFF & _CP_OFF & _BODEN_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT

	cblock	0x20
		tmp
		count_a
		count_b
	endc

#define 	tmr1_on	T1CON, 0		;TMR1 on/off
#define 	tmr1_of	PIR1, TMR1IF	;TMR1 counter overflow flag
		
#define 	c_bit	STATUS, C
#define 	z_bit	STATUS, Z

#define 	LEDG	GPIO, 0		;Gr LED, GP0, pin 7
#define 	rla  	GPIO, 5 	;GP5 pin 2

;**********************************************************************
	ORG     0x000

	call	Init
	clrf	GPIO	;ensure LEDs out & relays released. - GPIO is @ 0x05

	bsf		rla		;operate relay a to maintain the 240V connection
	bsf		LEDG

	movlw	d'42'	;0x2A (42  * 240 = 10 800 sec = 3 Hr)
	movwf	count_b
dxa
	movlw	d'240'	;0xF0
	movwf	count_a

dxb	movlw	0X20
	xorwf	GPIO, f	;toggle Green LED

	call	delay_500m	;
	call	delay_500m	;

	decfsz	count_a, f	
	goto	dxb

	decfsz	count_b, f
	goto	dxa

	clrf	GPIO

	sleep
	nop				;3 Hr = 10800 sec

;-------------------------------------------------------------------
delay_500m
	bsf		tmr1_on
	movlw	d'243'		;500 ms 
	movwf	tmp
	comf	tmp, w	
	movwf	TMR1H
	clrf	TMR1L

	bcf		tmr1_of
	bsf		tmr1_on		;T1CON, 0

	btfss	tmr1_of
	goto	$-1

	bcf		tmr1_on	
	bcf		tmr1_of
	return

Init				; set inputs/outputs
	banksel	GPIO		; select memory bank 0
	clrf	GPIO		; outputs low
	movlw	0x07		; set GP<2:0> to
	movwf	CMCON		; digital
	banksel	TRISIO		; select memory bank 1

	clrf	WPU			; disable weak pull ups

	movlw	B'00001011'	; GPO output, bit 3 must always = 1
	movwf	TRISIO		; GPO1, 2 & 3 are inputs
	movlw	0x81		;pullups disabled
	movwf	OPTION_REG	;TMR0 prescale = 1:4

	bsf		INTCON, PEIE	;(bit 5) enable peripheral interrupts
	bsf		PIE1, TMR1IE	;enable TMR1 overflow flag ?????
 
	clrf	IOC			;no interrupts on change

; calibrate internal oscillator
	bsf		STATUS, RP0	;bank 1
	call	H'80'	;centre frequency, 0xFC max frequency, 0x00 min frequency
cal	movwf	OSCCAL
	bcf		STATUS, RP0	;bank 0
  
	clrf	ANSEL		;digital i/o
	banksel	GPIO		;select memory bank 0

	movlw	0x35		;
	movwf	T1CON		;prescale 1:8, internal oscillator
	return
	end