;CUBE_PROTECTION
;M.KOVAC
;---------

	LIST P=10F320
	#include <P10F320.inc>


	__CONFIG  _WDTE_OFF & _FOSC_INTOSC

	org	0x00000
	goto	Start

	org	0x00008
	goto	ISR	

;---------

	org	0x00020
Start
	clrf	PORTA; clears all outputs

	movlw	B'00000110'; RA0 as output
	movwf	TRISA
	
	clrf ANSELA;
	bsf ANSELA,ANSA2; RA2 as analog input

	call	InitializeAD

	bsf	ADCON,GO_NOT_DONE

Main	goto	Main

;--------

ISR	
	;reference : 644mV
	movf	ADRES,W
  	addlw 0xd5; <43 (554mV)      
  	skpc 
	goto turn_off 

	movf	ADRES,W
  	addlw 0xc7; >57 (734mV)
  	skpnc 
	goto turn_off 

	goto turn_on
turn_off
	bcf LATA,LATA0; turn off RA0
	goto end_if
turn_on
	bsf LATA,LATA0; turn on RA0
end_if

	bcf	PIR1,ADIF
	bsf	ADCON,GO_NOT_DONE 

	goto	EndISR 

EndISR
	retfie	

;--------

InitializeAD
	movlw	B'10101011'; FOSC/16 AN2(RA2) 
	movwf	ADCON

	bcf	PIR1,ADIF 
	bsf	PIE1,ADIE
	
	bsf	INTCON,PEIE
	bsf	INTCON,GIE

	return

	END

