;AD Converter program

	INCLUDE"p16F88.inc"
	list p=16F88

	__CONFIG    _CONFIG1,  _HS_OSC & _WDT_OFF & _PWRTE_OFF & _CP_OFF
	__CONFIG    _CONFIG2,  _FCMEN_OFF & _IESO_OFF


	ORG	0
	GOTO	START			;Main Program starts from START

	ORG	4
	CALL    Timer0_interrupt
	RETFIE

START

;Setting of Port B
	BSF	STATUS,RP0
	BCF	STATUS,RP1		;Selection of Bank 1
	MOVLW	B'00000000'
	MOVWF	TRISB			;RB0-7 -> Output Port
	MOVLW	B'00000010'
	MOVWf	TRISA			;RA1 -> Input Port

;Setting of Timer0
	BCF	OPTION_REG, T0CS	;Internal instruction cycle clock (CLKO)
	BCF	OPTION_REG, PSA		;Internal instruction cycle clock (CLKO)
	BSF	OPTION_REG, PS2		;
	BSF	OPTION_REG, PS1		;
	BSF	OPTION_REG, PS0		;Timer0 Rate 1:256

;Setting of Interrupt
	BSF	INTCON, GIE		;Enables all unmasked interrupts
	BSF	INTCON, PEIE		;Enables all unmasked peripheral interrupts
	BSF	INTCON, TMR0IE		;Enables the TMR0 interrupt
	BCF	INTCON, TMR0IF		;TMR0 register did not overflow


;Setting of AD Converter
	MOVLW	B'00000010'
	MOVWF	ANSEL			;AN1:Analog I/O
	BCF	ADCON1, ADFM		;Left Justified
	BCF	ADCON1, ADCS2		;ADCS2 = 0, ADCS = 01 -> A/D Conversion Clock = FOSC/8
	BCF	ADCON1, VCFG1		;Voltage Reference = VSS 
	BCF	ADCON1, VCFG0		;Voltage Reference = VDD

	BCF	STATUS,RP0
	BCF	STATUS,RP1		;Selection of Bank 0

	BCF	ADCON0, ADCS1		;
	BSF	ADCON0, ADCS0		;ADCS2 = 0, ADCS = 01 -> A/D Conversion Clock = FOSC/8
	BCF	ADCON0, CHS2
	BCF	ADCON0, CHS1
	BSF	ADCON0, CHS0		;CHS = 001 -> AN1
	BSF	ADCON0, ADON		;A/D converter module is operating

; Main Program

STEP1	GOTO	STEP1

;End of Main Program

;Sub Routine1
Timer0_interrupt
	MOVLW	B'00000001'		;'00000001' -> (W)
	MOVWF	PORTB			;(W) -> (PORTB)


	BSF	ADCON0, GO		;AD Conversion Start
AD_Wait	
	BTFSC	ADCON0, 2		
	GOTO	AD_Wait			;Wait until AD conv is done

	MOVF	ADRESH, 0		;ADRESH -> (W)
	MOVWF	TMR0			;(W) -> TMR0

	MOVLW	B'00000000'
	MOVWF	PORTB

	BCF	INTCON, TMR0IF		;TMR0 register did not overflow

	RETURN


	END
