;************************************************************************************
;* PIC-Micro project												Date: 25.11.09	*
;*																					*
;* FILE: ADC-PWM.asm		AD-Converter controls:  				Name: GC		*
;*  						Pulse-With (Brightness of D0)							*
;* 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
ADCON0		equ	H'1F'	; Bank-0
ADCON1		equ H'1F'	; Bank-1

	; 	RAM-registers used by routines:
Ux			equ	H'20'		; Used by: ADC
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
#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
		movwf	ADCON0		; Bit0=1	AD-Converter switched ON

		clrf	PORTC		; PORTC Outputs are cleared (LED's off)

		goto	Start


;*************************    Subroutines   *********************************************

		;	Reads ADC and stores the value in Ux
Ux?		bsf		ADCON0,1	; Start AD-Conversion
loop1	btfsc	ADCON0,1	; Done ?
		goto	loop1		; No, check again
		movf	ADRESH,w	; Yes, 
		movwf	Ux			; store result in Ux
		return				; New value for Ux is ready

		;	Controls brightness of D0 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:			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)


;  	PW:	Measured with Oscilloscope on DS1:	PW:	3-780uS	and		TP: 830uS


