;****************************************************************************************
;* PIC microprojekt														Date: 29.12.09	*
;*																						*
;* FILE: ADC-2Blink.asm				 									Navn: GC		*
;*  																					*
;* Device: PIC16F684																	*
;*																						*
;*																						*
;* 																						*
;* 																						*
;* ADC-value controles blink frequency on DS1 of PICkit 2								*
;*																						*
;****************************************************************************************

;		H'<HEX>'		D'<Decimal>'		B'<Binary>'			A'<ASCII>'

; 	Registers og konstants are defined here:


STATUS	equ	H'03'	; status-register 

PORTA	equ	H'05'	; Input/output-register A
TRISA	equ	H'05'	; Bank-1, input/output configuration A
PORTC	equ	H'07'	; Input/output-register C
TRISC	equ	H'07'	; Bank-1, input/output configuration C

CMCON0	equ H'19'	; Comparator control register
ANSEL	equ H'11'	; Bank-1, Analog select register (A/D conversion Clock)
ADRESH	equ H'1E'	; A/D result
ADRESL	equ H'1E'	; Bank-1, A/D result
ADCON0	equ H'1F'	; A/D Control register 
ADCON1	equ H'1F'	; Bank-1, A/D Control register

; 	RAM-registers used by routines:
Ux		equ	H'20'	; Ux? (A/D Converter)
Ref_1	equ	H'21'	; 
Ref_2	equ H'22'	; 
Dly_1	equ H'23'	; Delay
Dly_2	equ H'24'	; Delay2
Dly_3	equ H'25'	; Delay3
Tmp_1	equ H'26'	; 
Tmp_2	equ H'27'	; 
Tmp_3	equ H'28'	; 


		list p=16F684

		org		H'05'		; Start of program  (05)


;*************************************** Chip setup *************************************


Bank_1	bsf		STATUS,5	; Switch to Bank-1 for chip-setup

PORT_A	movlw	B'11001111'	; RA5 and RA4 becomes digital outputs
		movwf	TRISA		; RA3, RA2 and RA1 becomes digital inputs (Tristate)
							; RA0 is ADC-input
PORT_C	clrf	TRISC		; PORTC= Outputs (TRISC<5:0>= 000000)

		movlw	B'00000001'	; RA0: ADC-Input, All other pins = digital I/O
		movwf	ANSEL		; 
		movlw	B'00010000' ; Bit<6:4> A/D Clock=Fosc/8
		movwf	ADCON1

Bank_0	bcf		STATUS,5	; Switch back to Bank-0

ADC		movlw	H'07'		; CM<2:0>=111	=>			!!! IMPORTANT !!!
		movwf	CMCON0		; Disable comparator, 		General purpos I/O

		movlw	B'00000000'	; Bit7=0:	0-------, Makes A/D Result Left justified (8 Bits used)
							; Bit6=0:	-0------, Vref for A/D =VDD
		movwf	ADCON0		; Bit<4:2>	---000--, Channel 0 selected: RA0/AN0= ADC-input
							; Bit1:		------1-, A/D start and Bussy test bit
							; Bit0=1:	-------1, Turns on A/D Converter

		clrf	PORTA		; PORTA Outputs are cleared (LED's off)
		clrf	PORTC		; PORTC Outputs are cleared

		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: D0 
Blink	bsf		PORTC,0		; (RC0)	Test of Delay-routine	Blinking on LED: DS1
		movf	Ux,w		; Uses ADC as input to Delay routine
		call	Delay3
		bcf		PORTC,0
		movf	Ux,w		; 
		call	Delay3
		return

;************************************* Main program *************************************

start	
New		call	Ux?			; Read ADC!
		incf	Ux,f		; Make Ux>0
		call	Blink		; Blink on D0
		goto	New			; New measurement

		end					; Program ends here


;*********************************** Programming a Device *******************************

;	  Select:
;
;	1.Configure		Select Device...	16f684
;	2.Configure		Configuration Bits	Clock:								Internal-RC,-
;																			No Clock
;
;										WatchDogTimer:						Off
; 										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)
