;******************************************************************************
;   This file is a basic template for assembly code for a PIC18F2685. Copy    *
;   this file into your project directory and modify or add to it as needed.  *
;                                                                             *
;   The PIC18FXXXX architecture allows two interrupt configurations. This     *
;   template code is written for priority interrupt levels and the IPEN bit   *
;   in the RCON register must be set to enable priority levels. If IPEN is    *
;   left in its default zero state, only the interrupt vector at 0x008 will   *
;   be used and the WREG_TEMP, BSR_TEMP and STATUS_TEMP variables will not    *
;   be needed.                                                                *
;                                                                             *
;   Refer to the MPASM User's Guide for additional information on the         *
;   features of the assembler.                                                *
;                                                                             *
;   Refer to the PIC18F2682/2685/4682/4685 Data Sheet for additional          *
;   information on the architecture and instruction set.                      *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Filename:                                                                *
;    Date:                                                                    *
;    File Version:                                                            *
;                                                                             *
;    Author:                                                                  *
;    Company:                                                                 *
;                                                                             * 
;******************************************************************************
;                                                                             *
;    Files required:         P18F2685.INC                                     *
;                                                                             *
;******************************************************************************

	LIST P=18F2685		;directive to define processor
	#include <P18F2685.INC>	;processor specific variable definitions

;******************************************************************************
;Configuration bits
;Microchip has changed the format for defining the configuration bits, please 
;see the .inc file for futher details on notation.  Below are a few examples.



;   Oscillator Selection:
    	CONFIG	OSC=IRCIO7            ;intern
		CONFIG  WDT=OFF               ;no Watchdog
		CONFIG  CP0=OFF,CP1=OFF,CP2=OFF,CP3=OFF,CP4=OFF,CP5=OFF,CPB=OFF,CPD=OFF     ;code protect off
		CONFIG  WRT0=OFF,WRT1=OFF,WRT2=OFF,WRT3=OFF,WRT4=OFF,WRT5=OFF,WRTB=OFF,WRTC=OFF,WRTD=OFF
		CONFIG  PBADEN=OFF             ;Port B as Digital I/O
		CONFIG	BOREN=OFF
		CONFIG  LVP=OFF               ;low voltage programming

;******************************************************************************
;Variable definitions
; These variables are only needed if low priority interrupts are used. 
; More variables may be needed to store other special function registers used
; in the interrupt routines.

;		UDATA
;		WREG_TEMP	;variable used for context saving 
;		STATUS_TEMP	;variable used for context saving
;		BSR_TEMP	;variable used for context saving
		
	UDATA_ACS
adwH	RES 1		; aktueller AD HighByte Wert
adwL	RES 1		; aktueller AD LowByte Wert
cms		RES 1		; Counter in Subroutine wms
cxms	RES 1		; Counter in Subroutine wxms
xms		RES 1		; Parameter fuer Subroutine wxms

;******************************************************************************
;EEPROM data
; Data to be programmed into the Data EEPROM is defined here

;		ORG	0xf00000

;		DE	"Test Data",0,1,2,3,4,5

;******************************************************************************
;Reset vector
; This code will start executing when a reset occurs.

		ORG	0x0000

		goto	Main		;go to start of main code

;******************************************************************************
;High priority interrupt vector
; This code will start executing when a high priority interrupt occurs or
; when any interrupt occurs if interrupt priorities are not enabled.

;		ORG	0x0008

;		bra	HighInt		;go to high priority interrupt routine

;******************************************************************************
;Low priority interrupt vector and routine
; This code will start executing when a low priority interrupt occurs.
; This code can be removed if low priority interrupts are not used.

;		ORG	0x0018

;		movff	STATUS,STATUS_TEMP	;save STATUS register
;		movff	WREG,WREG_TEMP		;save working register
;		movff	BSR,BSR_TEMP		;save BSR register

;	*** low priority interrupt code goes here ***


;		movff	BSR_TEMP,BSR		;restore BSR register
;		movff	WREG_TEMP,WREG		;restore working register
;		movff	STATUS_TEMP,STATUS	;restore STATUS register
;		retfie

;******************************************************************************
;High priority interrupt routine
; The high priority interrupt code is placed here to avoid conflicting with
; the low priority interrupt vector.

;HighInt:

;	*** high priority interrupt code goes here ***


;		retfie	FAST

;******************************************************************************
;Start of main program
; The main program code is placed here.

Init:
		movlw	0x70				; 8MHz als
		movwf	OSCCON				; interne Frequenz
		movlw	0x0D				; RA0,RA2,RA3
		movwf	TRISA				; als Input
		clrf	LATA				; Alle Ausgaenge von PortA auf LOW
		clrf	TRISB				; Port B als Output
		clrf	LATB				; Alle Ausgaenge von PortB auf LOW
		clrf	TRISC				; Port C als Output
		clrf	LATC				; Alle Ausgaenge von PortC auf LOW
		movlw	0x3B				; 0011 1011
		movwf	ADCON1				; Vref ein, RA0-RA3 Analog
		movlw	0x01				; 0000 0001
		movwf	ADCON0				; Channel Select auf AN0 und ADON=1
		movlw	0xA1				; 1010 0001
		movwf	ADCON2				; ACQT=8T_AD, ADCS=F_OSC/8
	return

Main:
		ORG		0x0100

		rcall Init

StartAD:
		bsf		ADCON0,0			; ADON - AD aktivieren
		bsf		LATC,5				; RA4 als Statusabfrage fuer AD setzen
		bsf		ADCON0,1			; AD Starten
LoopAD:
		btfsc	ADCON0,1			; AD fertig?
		goto	LoopAD				; warte bis AD fertig
		bcf		LATC,5				; RA4 als Statussignal loeschen
ReadAD:
		movff	ADRESH,adwH			; HighByte des ADC_Wertes sichern
		movff	ADRESL,adwL			; LowByte des ADC_Wertes sichern
ShowAD:
		movff	adwL,LATB			; LowByte des ADC_Wertes auf PortB legen
		rrncf	adwH				; Bit 9 und 10 des ADC stehen in den LSBs
		rrncf	adwH				; des ADRESH, Ausgabe soll in RC7 und RC6
		movff	adwH,LATC			; RC7 und RC6 uebernehmen fehlende 2 Bit
RepeatAD:
		movlw	0xFA				; 250 Millisekunden warten
		movwf	xms					; 
		rcall	wxms				; 
		movlw	0xFA				; nochmal 250 Millisekunden warten
		movwf	xms					; 
		rcall	wxms				; 
	goto 	StartAD

;Mehrfach genutzte Subroutinen

wxms:
		movff xms,cxms				; Parameter xms an Counter cxms uebergeben
wxms_loop:
		rcall wms
		decf cxms
		bnz wxms_loop
	return

wms:
		movlw 0xF9					; getesteter Wert, ergibt mit nops zusammen
		movwf cms					; genau eine Millisekunde
wms_loop:
		decf cms
		nop
		nop
		nop
		nop
		nop
		bnz wms_loop
	return

;******************************************************************************
;End of program
;Ende:
		END