;************************************************************************************
;* PIC-Micro project												Date: 25.11.09	*
;*																					*
;* FILE: ADC-Blink.asm		AD-Converter controls:  				Name: GC		*
;*  						Blink-Rate on DS1										*
;* Device: PIC16F690																*
;*																					*
;*																					*
;************************************************************************************

;			H'<HEX>'	D'<Decimal>'	B'<Binary>'		A'<ASCII>'

	; 	Registers and Constants are defined here:

INDF		equ H'00'	; Indirect Register (Uses content of FSR)
STATUS		equ	H'03'	; Status-register
FSR			equ H'04'	; Indirect Data Memory Address Pointer
PORTA		equ	H'05'	; Input/output-register A
TRISA		equ	H'05'	; Bank-1, input/output configuration A
PORTB		equ	H'06'	; Input/output-register B
TRISB		equ	H'06'	; Bank-1, input/output configuration B
PORTC		equ	H'07'	; Input/output-register C
TRISC		equ	H'07'	; Bank-1, input/output configuration C

INTCON		equ	H'0B'	; Interupt Control register (Enable-bits/Flags)
PIR1		equ H'0C'	; Interupt-Flags (Timer1 +)
TMR1L		equ	H'0E'	; Timer1 Low-Byte
TMR1H		equ	H'0F'	; Timer1 High-Byte
T1CON		equ	H'10'	; Timer1 Control
IOCA		equ	H'16'	; Bank-1 (96h):	Interupt On Change on Port A

CM1CON0		equ H'19'	; Bank-2, Comparator control register
CM2CON0		equ H'1A'	; Bank-2, Comparator control register
CM2CON1		equ	H'1B'	; Bank-2, Comparator control register
ANSEL		equ H'1E'	; Bank-2, Analog select register (A/D conversion Clock)
ADRESH		equ H'1E'	; Bank-0, ADC result format
ADCON0		equ	H'1F'	; Bank-0, ADC Conditions and control
ADCON1		equ H'1F'	; Bank-1, ADC Clock control


	; 	RAM-registers used by routines:
Ux			equ	H'20'		; Used by: ADC
Dly_1		equ H'21'		; Used by Delay-routines
Dly_2		equ H'22'		; 
Dly_3		equ H'23'		; 


	;	Definitions
#define	Out_1	PORTC,0	; LED DS1 on PICkit2
#define	Out_2	PORTC,1	; LED DS2 on PICkit2


;------------------------------------------------------------------------------------------
		list p=16F690

		org		H'00'		; Start of code
		
		goto	Init		; Reset Vector 	Located at 0x0000
		nop					;
		nop					;
		nop					;
		nop					; (ISR Vector	Located at 0x0004)

;**************************** Chip setup ************************************************

Init						; 

Bank_1	bsf		STATUS,5	; Switch to Bank-1
PORTs	movlw	B'11111100'	; RC0 and RC1 are digital outputs
		movwf	TRISC		; 
		movlw	B'00010000'	; Bit<6:4> = 001 => ADC-Clock= Fosc/8
		movwf	ADCON1		;
Bk_1_0	bcf		STATUS,5	; Switch back to Bank-0

Bank_2	bsf		STATUS,6	; Switch to Bank-2
		clrf	CM1CON0		; Disable comparator 1, 				!!! IMPORTANT !!!
		clrf	CM2CON0		; Disable comparator 2, 			PORTC: General purpos I/O
ADC_I	movlw	B'00000001' ; Makes AN0 analog input for ADC
		movwf	ANSEL		; 
Bk_2_0	bcf		STATUS,6	; Switch back to Bank-0

Bank_0						; ADC-conditions:
ADC_O	movlw	B'00000001'	; Bit7=0:		ADC Result Left justified
							; Bit6=0:		Vref for ADC= VDD
							; Bit<4:2>=000 	Channel 0 (RA0/AN0) selected
		movwf	ADCON0		; Bit0=1		AD-Converter switched ON

		clrf	PORTC		; PORTC Outputs are cleared (LED's off)

		goto	Start


;***********************************    Subroutines   ***********************************

		;	Short delays used by:	Delay2, Ux?, 
Delay1	movwf	Dly_1		; Store W in Dly_1 (23H)
lp_d1	decfsz	Dly_1,f		;   
		goto	lp_d1		; 
		return				; Delay= (1+(1+3<Dly_1>+2))*4TC

		; 	Long delays 
Delay2	movwf	Dly_2		; Store W in Dly_2 (24H)
lp_d2	movlw	H'00'		
		call	Delay1		; 
		decfsz	Dly_2,f
		goto	lp_d2
		return

		;	Generates very long delays
Delay3	movwf	Dly_3		; Store W in Dly_3 (25H)
lp_d3	movlw	H'04'		; 
		call	Delay2
		decfsz	Dly_3,f
		goto	lp_d3
		return



		;	Reads ADC-value
Ux?		bsf		ADCON0,0	; Turn on ADConverter
		movlw	D'4'		; W contains delay-value for Dly_1 (23h)
		call	Delay1		; Delay1
		bsf		ADCON0,1	; GO, Start A/D conversion
loop1	btfsc	ADCON0,1	; Done ?	(Bit1: ADC Start and Busy test-bit)
		goto	loop1		; No, try again
		movf	ADRESH,w	; Yes, 
		movwf	Ux			; store result in Ux
		bcf		ADCON0,0	; Turn off A/D Converter
		return				; Go back to Main program


		;	Blink on LED: DS1
Blink	bsf		Out_1		; (RC0)	Test of Delay-routine	Blinking on LED: DS1
		movf	Ux,w		; Uses ADC as input to Delay routine
		call	Delay3
		bcf		Out_1
		movf	Ux,w		; 
		call	Delay3
		return

;************************************* Main program *************************************

Start	
New		call	Ux?			; Read ADC!
		incf	Ux,f		; Make Ux>0
		call	Blink		; Blink on DS1
		goto	New			; New measurement

		end					; Program ends here


;**************************** Programming a Device **************************************

;		Select:

;	1.	Configure	Select Device:			16F690

;	2.	Configure	Configuration Bits: 	Clock:								Internal-RC, No Clock
; 											WatchDogTimer:						Off
;											Power Up Timer						On
; 											Master Clear Enable:				Internal
; 											Internal External Switch Over Mode:	Disabled
; 											Monitor Clock Fail-safe: 			Disabled

;	3.	Project		Quickbuild 	*.asm
;	4.	Programmer	Select Programmer:		PICkit 2
;	5.	Programmer	Program Device			(Or Yellow Icon)
