;****************************************************************************************
;* PIC microprojekt														Date: 29.12.09	*
;*																						*
;* FILE: ADC-2PWM.asm				 									Navn: GC		*
;*  																					*
;* Device: PIC16F684																	*
;*																						*
;*																						*
;* 																						*
;* 																						*
;* ADC-value controles blink frequency of DS1 on 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'		; Used by: ADC
Dly		equ H'21'		; Used by: Delay
Tmp_1	equ H'23'		; Used by: PW_O
Tmp_2	equ H'24'		; Used by: PW_O


	;	Definitions:
#define	Out_1	PORTC,0	; LED DS1 on PICkit2

		list p=16F684

		org		H'05'	; Start of program  (05)


;**************************** Chip Setup ************************************************


Bank_1	bsf		STATUS,5	; Switch to Bank-1

PORT_A	movlw	B'001111'	; 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	PORTC		; PORTC Outputs are cleared

		goto	Start


;*************************    Subroutines   *********************************************

Delay	movwf	Dly			; Store W in Dly (23h)
lp_d	decfsz	Dly,f		; Delay: Used by Ux? 
		goto	lp_d		; Delay= 1+(1+3<Dly>+2)*4TC
		return

		;	Reads ADC and stores the value in Ux
Ux?		bsf		ADCON0,0	; Turn on A/D Converter
		movlw	D'4'		; W contains delay-value for Dly (23h)
		call	Delay		; Delay
		bsf		ADCON0,1	; GO, Start A/D conversion
loop1	btfsc	ADCON0,1	; Done ?	(Bit1: A/D Start and Bussy 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


		;	Controls brightness of DS1 using PWM
PW_O	movf	Ux,w		; Input Data (Ux)
		movwf	Tmp_1		; Save a copy
		movwf	Tmp_2		; Work file for Pulse time or Dwell time
		btfsc	STATUS,2	; Make sure Data>0
		goto	Dwell		; No pulse, DS1 remains off
		bsf		Out_1		; Turn on DS1
Pwl_h	decfsz	Tmp_2,f		; Pulse time running
		goto	Pwl_h		; Pulse time Countdown loop
Dwell	bcf		Out_1		; Turn off DS1
		comf	Tmp_1,w		; Calculate Dwell time: Dt= H'FF'- H'<Tmp_1>' 
		btfsc	STATUS,2	; Make sure Data>0	To avoid dwell time "over run" 
		return				; Do nothing
		movwf	Tmp_2		; Load Work file
Pwl_l	decfsz	Tmp_2,f		; Dwell time running
		goto	Pwl_l		; Dwell time Countdown loop
		return				; PW adjusted according to Input Data (Ux)


;**************************** Main program **********************************************

Start	
New		call	Ux?			; Read ADC!
		call	PW_O		; Controls brightness of DS1 on PICkit2
		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)
