;-------------DDS mit AD9951-------------------
		list 	p=16f877a

  
;-----------Portleitungen definieren
#define		datenausgabe	portc, 2	;datenoutput, zum ad9951
#define		clok		portc, 1	;clockoutput, zum ad9951
#define		strobe		portc, 3	;strobeoutput, zum ad9951
#define		reset		portc, 0	;resetoutput zum ad9951
#define		lcd_e		portb, 3	;Enable output zur LCD Anzeige
#define		lcd_rw		portb, 2	;rw Leitung zur LCD Anzeige
#define		lcd_rs		portb, 1	;rs Leitung zur LCD Anzeige
#define		taster		porte, 2	;Input Taster vom Drehimpulsgeber; porte, 0 und porte, 1 sind input vom Drehgebr up/down Pin
#define		trigger		portb, 0	;output zum Triggern, geht auf high zu Beginn des Wobbelns
;------------Registerzuordnungen-------
indf		equ	0x00
cnter		equ	0x01
pc		equ	0x02
status		equ	0x03
fsr		equ	0x04
porta		equ	0x05
portb		equ	0x06
portc		equ	0x07
portd		equ	0x08
porte		equ	0x09
pclath		equ	0x0a
trisa		equ	0x85
trisb		equ	0x86
trisc		equ	0x87
trisd		equ	0x88
trise		equ	0x89
intcon		equ	0x0b
opt		equ	0x81
;----AD Wandler Register
adresl		equ	0x9e
adresh		equ	0x1e
adcon0		equ	0x1f
adcon1		equ	0x9f
;-----Timer1 Register
pir1		equ	0x0c
pie1		equ	0x8c
tmr1l		equ	0x0e
tmr1h		equ	0x0f
t1con		equ	0x10
;-----USART register
txreg		equ	0x19
txsta		equ	0x98
rcsta		equ	0x18
spbrg		equ	0x99	
;------EEPROM Register------
eeadr		equ	0x10d
eecon1		equ	0x18c
eedata		equ	0x10c
eecon2		equ	0x18d




;------------Allgemeine Register-------
	cblock	0x20
		daten	
		ausgabezaehler
		timer1	
		timer2	
		lcd_char                  ; Character being sent to the LCD
		lcd_read                  ; Character read from the LCD
		rs_value	              ; the lcd rs line flag value
		f_b0		;niedrigstes byte der Frequenz (binär)
		f_b1
		f_b2
		f_b3		;höchstes byte der frequenz (binär)
		
		f_b4		;ergebnis der multiplikation der bytes für die frequenz (binär)
	        	f_b5		;des dds tuning wortes liegt in f_b6 (höchstes byte - f_b3 (niedrigstes byte) 
	        	f_b6
	        	f_b7		;rechendummy, ergebnis vollkomen wurscht
	
		
		sr0		
		sr1
		sr2
		sr3
		mem
		cf
		sum
		_100MHZ		;frequenz als bcd
		_10MHZ
		_1MHZ
		_100KHZ
		_10KHZ
		_1KHZ
		_100HZ
		_10HZ
		_1HZ
		
		_100MHZLCD	;frequenz als ASCII für LCD Ausgabe
		_10MHZLCD
		_1MHZLCD
		_100KHZLCD
		_10KHZLCD
		_1KHZLCD
		_100HZLCD
		_10HZLCD
		_1HZLCD
		MHZpunkt		;Punkte zur Trennung von MHz und KHZ bei der LCD Ausgabe		
		KHZpunkt
		
		
		ref_clk0		;niedrigstes byte des multiplikationsfaktors
		ref_clk1		;für die Ermittlung des DDS Tuning Worts
		ref_clk2		;aus den Frequenzbytes (binär) 
		ref_clk3		;ref_clk = 2^32 : f_ref(in Hz): Ganzzahliger Anteil wandeln in Hex  
				;Rest mulitplizieren mit 2^24 und wandeln nach Hex
				;Ergebnis in Hex: 1 Byte ganzzahliger Anteil, 3 Byte Rest
				;Bsp: ref_clk = 2^32 : 400000000 (400MHz Referenzoscillator) = 10,73741824
				;Ganzzahliger Anteil ist 10 ==> Hex 0a
				;
				; Rest ist 0,73741824 * 2^24 = 12371825
				; Wandlung nach Hex:bcc771
				;==> ref_clk3 = 0a, ref_clk2 = bc, ref_clk1 = c7, ref_clk0 = 71 	
				
		d_neu		;Drehgeber Register
		d_alt
		drehzaehler		
		rauf_runter
		
		schrittzaehler	;Register in dem abgelegt ist, welche Schrittweite beim Abstimmen verwendet wird
		adresse		;eeprom adresse maskieren		

		zaehler
		pausenzaehler
		
		temp_100MHZ		;Rettungsregister Originalfrequenz als bcd  (brauchts beim wobbeln)
		temp_10MHZ
		temp_1MHZ
		temp_100KHZ
		temp_10KHZ
		temp_1KHZ
		temp_100HZ
		temp_10HZ
		temp_1HZ
		
		up_100MHZ		;frequenz als bcd obere frequenz beim wobbeln
		up_10MHZ
		up_1MHZ
		up_100KHZ
		up_10KHZ
		up_1KHZ
		up_100HZ
		up_10HZ
		up_1HZ
		
		tempstart_100MHZ	;Rettungsregister Startfrequenz beim Wobbeln
		tempstart_10MHZ
		tempstart_1MHZ
		tempstart_100KHZ
		tempstart_10KHZ
		tempstart_1KHZ
		tempstart_100HZ
		tempstart_10HZ
		tempstart_1HZ
			
		haltezaehler
		haltezaehler1
		oftwobbeln
		nochmalzaehler
		lcdzaehler
		lcdupdate
		endc
;---------------BIT equates

c		equ	0
z		equ	2
rp0		equ	5
rp1		equ	6
eepgd		equ	7
rd		equ	0
wren		equ	2
wr		equ	1	
f		equ	1
d		equ	0
w		equ	0

;-------------eeprom programmieren beim brennen mit picstart plus
		org	0x2100

;---EEPROM Adressen	0x00  0x01  0x02  0x03
	de	0x0a, 0xbc, 0xc7, 0x71		;multiplikationsfaktor für das ad9951 tuningwort, in dieser Folge: ref_clk3 bis ref_clk0

;---EEPROM Adressen	0x04  0x05  0x06  0x07   0x08   0x09  0x0a   0x0b  0x0c
	de	d'0', d'1', d'0', d'5', d'0', d'0', d'0', d'0', d'0'	;untere Startfrequenz in BCD zum Wobbeln

;---EEPROM Adressen	0x0d  0x0e  0x0f  0x10  0x11  0x12  0x13  0x14  0x15
	de	d'0', d'1', d'0', d'9', d'0', d'0', d'0', d'0', d'0'	;obere Endfrequenz in BCD zum Wobbeln
;	de	b'00000000'	;_4094_4

;-------------------Makros
page0	macro
	bcf	pclath, 3
	bcf	pclath, 4
	endm	

page1	macro
	bsf	pclath, 3
	bcf	pclath, 4
	endm	

page2	macro
	bcf	pclath, 3
	bsf	pclath, 4
	endm	

page3	macro
	bsf	pclath, 3
	bsf	pclath, 4
	endm	

;-----------------------ENDE Makros



;---------------Start Programm: Anfangsadresse-----
		org	0x000
		goto	beginn

;---------------für den ganzen programmablauf gültige vereinbarung-----
beginn		clrwdt			;lösche watchdogtimer
		clrf	status		;lösche status register
		bcf	status, 2
		clrf	intcon		;verbiete interrupts
		clrf	porta
		clrf	portb
		clrf	portc
		clrf	portd
		clrf	porte
		bsf	status, rp0
		
		movlw	b'00000000'		;outputs
		movwf	trisb
		movlw	b'00000000'
		movwf	trisd
		movlw	b'00000000'
		movwf	trisc
		movlw	b'00000111'
		movwf	trise
		movlw	b'00000000'		;outputs
		movwf	trisa
		movlw	b'00000110'		;alle analogen Pins auf digital schalten
		movwf	adcon1
		bcf	status, rp0

		movf	porte, 0	;Initialisierung vom Drehgeber
		andlw	b'00000011'
		movwf	d_neu
		movwf	d_alt
		movlw	0x02	;Drehgeber erzeugt pro Rastung 2 Impulse, die werden jeweils ausgemerzt
		movwf	drehzaehler
		
		movlw	b'01000000'		;Schrittzähler setzen (Schrittweite)
		movwf	schrittzaehler
		
		movlw	b'00000001'		;Haltezeit beim Wobbeln setzen
		movwf	haltezaehler
	
		movlw	b'00000001'		;Wartezeit am Ende  beim Wobbeln setzen
		movwf	haltezaehler1
		
		movlw	b'00000000'		;Register, das geprüft wird, wenn man entscheidet ob einmal oder mehrfach gewobbelt wird
		movwf	oftwobbeln			
		
		movlw	b'00000001'		;Register in dem beim Ende des Wobbelns steht, ob nochmal gewobbelt werden soll oder nicht
		movwf	nochmalzaehler
		
		bcf	lcdupdate, 0		;Register wann an LCD ausgegeben wird
		movlw	d'255'
		movwf	lcdzaehler		;wert, wie oft hauptschleife durchlaufen wird, bis es zu ersten ausgabe an lcd kommt
		
				
		bsf	reset		;Resete den ad9951, so dass alle Register in einem definierten Zustand sind
		call	init		;Initialisierung des AD9951 z.B. RefClk Multiplier setzen etc
		call	init_lcd		;Initialisierung der LCD Anzeige (2 Zeilen mit je 16 Zeichen)

		page3
		call	refclk_eeprom_lies	;mulitpilkationsfaktor für DDS Tuning Wort aus eeprom auslese, steht auf Seite 1 
		page0


;Nur beim Einschalten testen, ob der Multiplikationsfaktor für das DDS Tuning Wort geändert werden muss
		btfsc	taster
		goto	erstefrequenz	;taster nicht gedrückt, überspringe cal routine
		call	wait_8ms		
		btfsc	taster
		goto	erstefrequenz
			
ref_clk_aendern	movlw	b'10000001'		;gib als erstes mal aus, dass wir kalibrieren		
		call	cmnd2LCD
		movlw	'K'
		call	data2LCD
		movlw	'a'
		call	data2LCD
		movlw	'l'
		call	data2LCD
		movlw	'i'
		call	data2LCD
		movlw	'b'
		call	data2LCD
		movlw	'r'
		call	data2LCD
		movlw	'i'
		call	data2LCD
		movlw	'e'
		call	data2LCD
		movlw	'r'
		call	data2LCD
		movlw	'u'
		call	data2LCD
		movlw	'n'
		call	data2LCD
		movlw	'g'
		call	data2LCD
		
		btfss	taster		;ref_clk's aendern: warte bis der Taster nicht mehr gedrückt ist
		goto	$-1
		
		movlw	b'11000001'		;in der zweiten Zeile 10 MHZ
		call	cmnd2LCD
		movlw	'1'
		call	data2LCD
		movlw	'0'
		call	data2LCD
		movlw	'.'
		call	data2LCD
		movlw	'0'
		call	data2LCD
		movlw	'0'
		call	data2LCD
		movlw	'0'
		call	data2LCD
		movlw	'.'
		call	data2LCD
		movlw	'0'
		call	data2LCD
		movlw	'0'
		call	data2LCD
		movlw	'0'
		call	data2LCD
		movlw	' '
		call	data2LCD
		movlw	'M'
		call	data2LCD
		movlw	'H'
		call	data2LCD
		movlw	'z'
		call	data2LCD
		movlw	d'0'	
		movwf	_100MHZ		;gib nur an den DDS 10MHZ aus, ohne LCD Anzeige
		movlw	d'1'	
		movwf	_10MHZ
		movlw	d'0'	
		movwf	_1MHZ
		movlw	d'0'	
		movwf	_100KHZ
		movlw	d'0'	
		movwf	_10KHZ
		movlw	d'0'	
		movwf	_1KHZ
		movlw	d'0'	
		movwf	_100HZ
		movlw	d'0'	
		movwf	_10HZ
		movlw	d'0'	
		movwf	_1HZ
		call	dds_update		;Berechnung und Ausgabe an den DDS
					
caltest		call	dreh
		btfss	rauf_runter, 1	;wurde gedreht?
		goto	nichtgedrehtcal	;es wurde nicht gedreht 
		movlw	b'00000011'		;es wurde gedreht
		subwf	rauf_runter, 0	;wurde nach oben gedreht?
		btfss	status, z		
		goto	nachuntencal		;nach unten
		goto	nachobencal		;nach oben

nachuntencal	movlw	0x0a
		subwf	ref_clk0, 1
		btfsc	status, 0
		goto	down_0	;kein überlauf
		movlw	0x01	;überlauf
		subwf	ref_clk1, 1
		btfsc	status, 0
		goto	down_0
		movlw	0x01
		subwf	ref_clk2, 1
		btfsc	status, 0
		goto	down_0
		decf	ref_clk3, 1
	
down_0		call	dds_update
		goto	nichtgedrehtcal	

nachobencal	movlw	0x0a
		addwf	ref_clk0, 1
		btfss	status, 0
		goto	up_0
		incfsz	ref_clk1, 1
		goto	up_0
		incfsz	ref_clk2, 1
		goto	up_0
		incf	ref_clk3, 1
		
up_0		call	dds_update
		goto	nichtgedrehtcal

nichtgedrehtcal	btfsc	taster
		goto	caltest	;nein, nicht gedrückt
		call	wait_8ms	
		btfsc	taster
		goto	caltest
		
		btfss	taster		;ja, warte bis der taster wieder losgelassen wird
		goto	$-1
		call	wait_8ms
		btfss	taster
			;ja, warte bis der taster wieder losgelassen wird
		goto	$-4
		page3			;schreibe neuen wert der ref_clk's in das eeprom, steht auf Seite 1
		call	refclk_eeprom_schreib		 
		page0
		
		movlw	b'00000001'		;lösche LCD Anzeige
		call	cmnd2LCD
		

;ENDE: Nur beim Einschalten up testen, ob der Multiplikationsfaktor für das DDS Tuning Wort geändert werden muss		
		
erstefrequenz	movlw	d'0'		;Startfrequenz
		movwf	_100MHZ		;frequenz als bcd
		movlw	d'1'	
		movwf	_10MHZ
		movlw	d'0'	
		movwf	_1MHZ
		movlw	d'0'	
		movwf	_100KHZ
		movlw	d'0'	
		movwf	_10KHZ
		movlw	d'0'	
		movwf	_1KHZ
		movlw	d'0'	
		movwf	_100HZ
		movlw	d'0'	
		movwf	_10HZ
		movlw	d'0'	
		movwf	_1HZ


		
		call	dds_update		;Berechnung und Ausgabe an den DDS
		call	LCD		;Führende Nullen unterdrücken, ASCII Wandlung, Einheit bestimmen, raus an LCD Anzeige
		;call	con
		;call	mpn
		
;=================Beginn Hauptprogramm======
start		;btfsc	taster
		;goto	drehtestung		;taster gedrückt, unterprogramm zur einstellung der schrittweite
		;call	wait_8ms		
		;btfsc	taster
		;goto	drehtestung
		;call	schritteeinstellen
		;--------------Hier wird engtschieden wann eine LCD Ausgabe erfolgen soll, nur alle 255 Durchlaufzyklen der Hauptschleife
		decfsz	lcdzaehler, 1	;bei jeder Schleife wird lcdzaheler um 1 verringert
		goto	$+4		; weitergemacht solange er nicht 0 ist
		movlw	d'255'		; ist er 0, wird er wieder auf 255 gesetzt 
		movwf	lcdzaehler
		bsf	lcdupdate, 0		;und in diesem Byte BIT 0 gesetzt
		
		
		
		;test wie lange der taster gedrückt wird
		movlw	d'100'
		movwf	zaehler	
		btfsc	taster
		goto	drehtestung
		call	wait_8ms
		btfsc	taster
		goto	drehtestung
		page3
		call	schritteeinstellenlcd
		page0
		
		
testtaster		btfss	taster
		goto	testzaehler
schrittweitetest	call	schritteeinstellen	;taster war kürzer als 1 Sekunde gedrückt
		goto	drehtestung

testzaehler		call	wait_8ms
		decfsz	zaehler, 1
		goto	testtaster
wobbelntest		page1			;taster war länger als 1 Sekunde gedrückt
		call	wobbeln
		page0
		goto	drehtestung

		;ENDE test wie lange der taster gedrückt wird
	
	
drehtestung		call	dreh		;taster nicht gedrückt, drehgeber routine aufrufen
		btfss	rauf_runter, 1	;wurde gedreht?
		goto	nichtgedreht		;es wurde nicht gedreht 
		movlw	b'00000011'		;es wurde gedreht
		subwf	rauf_runter, 0	;wurde nach oben gedreht?
		btfss	status, z		
		goto	nachunten		;nach unten
		goto	nachoben		;nach oben
nachunten		btfss	schrittzaehler, 0	;1 Hz Schritte? 
		goto	$+3		;nein
		call	qrgminus_1HZ	;ja
		goto	undraus		
		btfss	schrittzaehler, 1	;10 Hz Schritte?
		goto	$+3		;nein
		call	qrgminus_10HZ	;ja
		goto	undraus
		btfss	schrittzaehler, 2	;100 Hz Schritte?
		goto	$+3		;nein
		call	qrgminus_100HZ	;ja
		goto	undraus
		btfss	schrittzaehler, 3	;1 kHz Schritte?
		goto	$+3		;nein
		call	qrgminus_1KHZ	;ja
		goto	undraus
		btfss	schrittzaehler, 4	;10 kHz Schritte?
		goto	$+3		;nein
		call	qrgminus_10KHZ	;ja
		goto	undraus
		btfss	schrittzaehler, 5	;100 kHz Schritte?
		goto	$+3		;nein
		call	qrgminus_100KHZ	;ja
		goto	undraus
		btfss	schrittzaehler, 6	;1 MHz Schritte?
		goto	$+3		;nein
		call	qrgminus_1MHZ	;ja
		goto	undraus
		call	qrgminus_10MHZ	;dann könnens ja blos noch 10MHZ Schritte sein
		goto	undraus
		
nachoben		btfss	schrittzaehler, 0	;1 Hz Schritte? 
		goto	$+3		;nein
		call	qrgplus_1HZ	;ja
		goto	undraus		
		btfss	schrittzaehler, 1	;10 Hz Schritte?
		goto	$+3		;nein
		call	qrgplus_10HZ	;ja
		goto	undraus
		btfss	schrittzaehler, 2	;100 Hz Schritte?
		goto	$+3		;nein
		call	qrgplus_100HZ	;ja
		goto	undraus
		btfss	schrittzaehler, 3	;1 kHz Schritte?
		goto	$+3		;nein
		call	qrgplus_1KHZ	;ja
		goto	undraus
		btfss	schrittzaehler, 4	;10 kHz Schritte?
		goto	$+3		;nein
		call	qrgplus_10KHZ	;ja
		goto	undraus
		btfss	schrittzaehler, 5	;100 kHz Schritte?
		goto	$+3		;nein
		call	qrgplus_100KHZ	;ja
		goto	undraus
		btfss	schrittzaehler, 6	;1 MHz Schritte?
		goto	$+3		;nein
		call	qrgplus_1MHZ	;ja
		goto	undraus
		call	qrgplus_10MHZ	;dann könnens ja blos noch 10MHZ Schritte sein
		goto	undraus
				


undraus		call	dds_update		;Berechnung und Ausgabe an den DDS
		btfss	lcdupdate, 0		;NIcht 0, keine LCD Ausgabe, da Hauptschleife noch nicht 255 durchlaufen wurde
		goto	start
		movlw	d'255'		;Hauptschleife 255 mal durchlaufen, also LCD Ausgabe ,löschen des lcdupdate bits 0 		
		movwf	lcdzaehler		;und setzen des Abwärtszaehlers lcdzaehler
		bcf	lcdupdate, 0
		call	LCD		;Führende Nullen unterdrücken, ASCII Wandlung, Einheit bestimmen, raus an LCD Anzeige
		goto	start
nichtgedreht		goto	start	
		
		
		
		



;================Soubroutinen================

;---------------------Einstellung der Schrittweite---------
schritteeinstellen	;btfss	taster		;warte bis der Taster nicht mehr gedrückt ist
		;goto	$-1
		
		
schrittermittlung	movlw	b'00000001'
		subwf	schrittzaehler, 0
		btfss	status, z 
		goto	$+2
		goto	Schritt1Hz
		movlw	b'00000010'
		subwf	schrittzaehler, 0
		btfss	status, z 
		goto	$+2
		goto	Schritt10Hz
		movlw	b'00000100'
		subwf	schrittzaehler, 0
		btfss	status, z 
		goto	$+2
		goto	Schritt100Hz
		movlw	b'00001000'
		subwf	schrittzaehler, 0
		btfss	status, z 
		goto	$+2
		goto	Schritt1kHz
		movlw	b'00010000'
		subwf	schrittzaehler, 0
		btfss	status, z 
		goto	$+2
		goto	Schritt10kHz
		movlw	b'00100000'
		subwf	schrittzaehler, 0
		btfss	status, z 
		goto	$+2
		goto	Schritt100kHz
		movlw	b'01000000'
		subwf	schrittzaehler, 0
		btfss	status, z 
		goto	$+2
		goto	Schritt1MHz
		goto	Schritt10MHz


Schritt1Hz		movlw	b'11000001'
		call	cmnd2LCD
		movlw	'1'
		call	data2LCD
		movlw	' '
		call	data2LCD
		movlw	'H'
		call	data2LCD
		movlw	'z'
		call	data2LCD
		movlw	' '
		call	data2LCD
		movlw	' '
		call	data2LCD
		movlw	' '
		call	data2LCD
		goto 	naechsteschrittweite
Schritt10Hz		movlw	b'11000001'
		call	cmnd2LCD
		movlw	'1'
		call	data2LCD
		movlw	'0'
		call	data2LCD
		movlw	' '
		call	data2LCD
		movlw	'H'
		call	data2LCD
		movlw	'z'
		call	data2LCD
		movlw	' '
		call	data2LCD
		movlw	' '
		call	data2LCD
		goto 	naechsteschrittweite

Schritt100Hz	movlw	b'11000001'
		call	cmnd2LCD
		movlw	'1'
		call	data2LCD
		movlw	'0'
		call	data2LCD
		movlw	'0'
		call	data2LCD
		movlw	' '
		call	data2LCD
		movlw	'H'
		call	data2LCD
		movlw	'z'
		call	data2LCD
		movlw	' '
		call	data2LCD
		goto 	naechsteschrittweite

Schritt1kHz		movlw	b'11000001'
		call	cmnd2LCD
		movlw	'1'
		call	data2LCD
		movlw	' '
		call	data2LCD
		movlw	'k'
		call	data2LCD
		movlw	'H'
		call	data2LCD
		movlw	'z'
		call	data2LCD
		movlw	' '
		call	data2LCD
		movlw	' '
		call	data2LCD
		goto 	naechsteschrittweite

		
		
Schritt10kHz	movlw	b'11000001'
		call	cmnd2LCD
		movlw	'1'
		call	data2LCD
		movlw	'0'
		call	data2LCD
		movlw	' '
		call	data2LCD
		movlw	'k'
		call	data2LCD
		movlw	'H'
		call	data2LCD
		movlw	'z'
		call	data2LCD
		movlw	' '
		call	data2LCD
		goto 	naechsteschrittweite



Schritt100kHz	movlw	b'11000001'
		call	cmnd2LCD
		movlw	'1'
		call	data2LCD
		movlw	'0'
		call	data2LCD
		movlw	'0'
		call	data2LCD
		movlw	' '
		call	data2LCD
		movlw	'k'
		call	data2LCD
		movlw	'H'
		call	data2LCD
		movlw	'z'
		call	data2LCD
		goto 	naechsteschrittweite



Schritt1MHz	movlw	b'11000001'
		call	cmnd2LCD
		movlw	'1'
		call	data2LCD
		movlw	' '
		call	data2LCD
		movlw	'M'
		call	data2LCD
		movlw	'H'
		call	data2LCD
		movlw	'z'
		call	data2LCD
		movlw	' '
		call	data2LCD
		movlw	' '
		call	data2LCD
		goto 	naechsteschrittweite



Schritt10MHz	movlw	b'11000001'
		call	cmnd2LCD
		movlw	'1'
		call	data2LCD
		movlw	'0'
		call	data2LCD
		movlw	' '
		call	data2LCD
		movlw	'M'
		call	data2LCD
		movlw	'H'
		call	data2LCD
		movlw	'z'
		call	data2LCD
		movlw	' '
		call	data2LCD
		goto 	naechsteschrittweite

		
		
naechsteschrittweite	call	dreh
		btfss	rauf_runter, 1	;wurde gedreht?
		goto	nichtgedreht1	;es wurde nicht gedreht 
		movlw	b'00000011'		;es wurde gedreht
		subwf	rauf_runter, 0	;wurde nach oben gedreht?
		btfss	status, z		
		goto	nachunten1		;nach unten
		goto	nachoben1		;nach oben
nachunten1		btfsc	schrittzaehler, 0	; 	
		goto	setzebit7		;bit 1 ist gesetzt
		goto	rotiererechts
setzebit7		movlw	b'10000000'		
		movwf	schrittzaehler
		goto	schrittermittlung
rotiererechts	bcf	status, c		;rotieren nach rechts
		rrf 	schrittzaehler, 1		
		goto	schrittermittlung

nachoben1		btfsc	schrittzaehler, 7	; 	
		goto	setzebit1		;bit 7 ist gesetzt
		goto	rotierelinks
setzebit1		movlw	b'00000001'		
		movwf	schrittzaehler
		goto	schrittermittlung
rotierelinks		bcf	status, c		;rotieren nach rechts
		rlf 	schrittzaehler, 1		
		goto	schrittermittlung
	
nichtgedreht1	btfsc	taster
		goto	naechsteschrittweite	;nein, nicht gedrückt
		call	wait_8ms	
		btfsc	taster
		goto	naechsteschrittweite
		
		btfss	taster		;ja, warte bis der taster wieder losgelassen wird
		goto	$-1
		call	wait_8ms
		btfss	taster
			;ja, warte bis der taster wieder losgelassen wird
		goto	$-4
		movlw	b'00000001'		;lösche LCD Anzeige
		call	cmnd2LCD
		call	LCD
		return
		
		
		
		
;--------------------------------Drehgeber abfragen
;=========================================================================
dreh		clrf	rauf_runter
		movf	porte, 0
		andlw	b'00000011'
		movwf	d_neu
		xorwf	d_alt, 0
		btfsc	status, z
		return		;keine Bewegung am Drehgeber, bei rücksprung ist bit 1 von rauf_runter gelöscht (keine drehung erfolgt)
	
		bcf	status, c
		rlf	d_alt, 1
		movf	d_neu, 0
		xorwf	d_alt, 1
		btfss	d_alt, 1
		goto	dreh1		;Drehgeber gedreht nach links
;-------------erhöhen der qrg			;Drehgeber gedreht nach rechts
		decfsz	drehzaehler, 1	;pro Rastung 2 Impulse, einer wird ausgemerzt
		goto	dreh3
		bsf	rauf_runter, 0	;bit, dass nach rechts gedreht wurde	
		bsf	rauf_runter, 1	;bit, dass überhaupt gedreht wurde
		goto	dreh2
		
;------------verringern der qrg		
dreh1		decfsz	drehzaehler, 1
		goto	dreh3
		bcf	rauf_runter, 0	;bit, dass nach links gedreht wurde
		bsf	rauf_runter, 1	;bit, dass überhaupt gedreht wurde
		goto	dreh2

;---ausmerzen des einen impulses
dreh3		call	wait_1ms
		movf	d_neu, 0
		movwf	d_alt
		goto	dreh


;------------rücksprung
dreh2		movf	d_neu, 0
		movwf	d_alt
		movlw	0x02
		movwf	drehzaehler
		return

;-------------------------------ENDE: Drehgeber abfragen


;--------------Erhöhen der Frequenz über Drehgeber im BCD Format
;--------------getestet,geht
qrgplus_1HZ	incf	_1HZ, 1
		movlw	d'10'
		subwf	_1HZ, 0
		btfss	status, c
		goto	qrttest
		clrf	_1HZ
		goto	$+1	
qrgplus_10HZ	incf	_10HZ, 1
		movlw	d'10'
		subwf	_10HZ, 0
		btfss	status, c
		goto	qrttest
		clrf	_10HZ
		goto	$+1
qrgplus_100HZ	incf	_100HZ, 1
		movlw	d'10'
		subwf	_100HZ, 0
		btfss	status, c
		goto	qrttest
		clrf	_100HZ
		goto	$+1
qrgplus_1KHZ	incf	_1KHZ, 1
		movlw	d'10'
		subwf	_1KHZ, 0
		btfss	status, c
		goto	qrttest
		clrf	_1KHZ
		goto	$+1
qrgplus_10KHZ	incf	_10KHZ, 1
		movlw	d'10'
		subwf	_10KHZ, 0
		btfss	status, c
		goto	qrttest
		clrf	_10KHZ
		goto	$+1
qrgplus_100KHZ	incf	_100KHZ, 1
		movlw	d'10'
		subwf	_100KHZ, 0
		btfss	status, c
		goto	qrttest
		clrf	_100KHZ
		goto	$+1
qrgplus_1MHZ	incf	_1MHZ, 1
		movlw	d'10'
		subwf	_1MHZ, 0
		btfss	status, c
		goto	qrttest
		clrf	_1MHZ
		goto	$+1
qrgplus_10MHZ	incf	_10MHZ, 1
		movlw	d'10'
		subwf	_10MHZ, 0
		btfss	status, c
		goto	qrttest
		clrf	_10MHZ
		goto	$+1
		incf	_100MHZ, 1
		
qrttest		movlw	d'0'		;test ob eine frequenz größer 160000000HZ eingestellt ist
		subwf	_100MHZ, 0		;wenn ja, dann gib 0 Hz aus über set0
		btfss	status, z
		goto	$+2
		goto	qrt
		
		movlw	d'1'
		subwf	_100MHZ, 0
		btfss	status, z
		goto	qrt
			
		movlw	d'6'
		subwf	_10MHZ, 0
		btfss	status, c
		goto	qrt
		
		movlw	d'6'
		subwf	_10MHZ, 0
		btfss	status, z
		goto	set0
						
		movlw	d'0'
		subwf	_1MHZ, 0
		btfss	status, z
		goto	set0
		
		movlw	d'0'
		subwf	_100KHZ, 0
		btfss	status, z
		goto	set0
		
		movlw	d'0'
		subwf	_10KHZ, 0
		btfss	status, z
		goto	set0
		
		movlw	d'0'
		subwf	_1KHZ, 0
		btfss	status, z
		goto	set0
		
		movlw	d'0'
		subwf	_100HZ, 0
		btfss	status, z
		goto	set0
		
		movlw	d'0'
		subwf	_10HZ, 0
		btfss	status, z
		goto	set0
		
		movlw	d'0'					
		subwf	_1HZ, 0		
		btfss	status, z
		goto	set0
		goto	qrt				
		
set0		movlw	0x00
		movwf	_1HZ		;qrg größer 160000000HZ d.h. zurück mit 0Hz
		movwf	_10HZ
		movwf	_100HZ
		movwf	_1KHZ
		movwf	_10KHZ
		movwf	_100KHZ
		movwf	_1MHZ
		movwf	_10MHZ
		movwf	_100MHZ
	
		goto	qrt
qrt		
		return
;---------------Ende Frequenzerhöhung über Drehgeber

;---------------Frequenzerniedrigung über Drehgeber, Berechnung im BCD Format
;---------------getestet, geht
qrgminus_1HZ	decf	_1HZ, 1
		movlw	d'10'	
		subwf	_1HZ, 0
		btfss	status, c
		goto	qrt1
		movlw	0x09
		movwf	_1HZ
		goto	$+1

qrgminus_10HZ	decf	_10HZ, 1
		movlw	d'10'	
		subwf	_10HZ, 0
		btfss	status, c
		goto	qrt1
		movlw	0x09
		movwf	_10HZ
		goto	$+1
qrgminus_100HZ	decf	_100HZ, 1
		movlw	d'10'	
		subwf	_100HZ, 0
		btfss	status, c
		goto	qrt1
		movlw	0x09
		movwf	_100HZ
		goto	$+1

qrgminus_1KHZ	decf	_1KHZ, 1
		movlw	d'10'	
		subwf	_1KHZ, 0
		btfss	status, c
		goto	qrt1
		movlw	0x09
		movwf	_1KHZ
		goto	$+1
qrgminus_10KHZ	decf	_10KHZ, 1
		movlw	d'10'	
		subwf	_10KHZ, 0
		btfss	status, c
		goto	qrt1
		movlw	0x09
		movwf	_10KHZ
		goto	$+1
qrgminus_100KHZ	decf	_100KHZ, 1
		movlw	d'10'	
		subwf	_100KHZ, 0
		btfss	status, c
		goto	qrt1
		movlw	0x09
		movwf	_100KHZ
qrgminus_1MHZ	decf	_1MHZ, 1		
		movlw	d'10'	
		subwf	_1MHZ, 0
		btfss	status, c
		goto	qrt1
		movlw	0x09
		movwf	_1MHZ
qrgminus_10MHZ	decf	_10MHZ, 1		
		movlw	d'10'	
		subwf	_10MHZ, 0
		btfss	status, c
		goto	qrt1
		movlw	0x09
		movwf	_10MHZ

		decf	_100MHZ, 1
		movlw	d'2'	
		subwf	_100MHZ, 0
		btfss	status, c
		goto	qrt1
		
		movlw	d'1'		;qrg kleiner 0 Hz d.h. 160.000000 Hz ausgeben
		movwf	_100MHZ
		movlw	d'6'
		movwf	_10MHZ
		movlw	d'0'
		movwf	_1MHZ
		movwf	_100KHZ
		movwf	_10KHZ
		movwf	_1KHZ
		movwf	_100HZ
		movwf	_10HZ
		movwf	_1HZ
		goto	qrt1
qrt1		return
;--------------Ende Frequenz verringern über Drehgeber

;------------------------------Ausgabe an den DDS Baustein
dds_update		call	con	;Wandlung BCD nach Hex der aktuellen Freqenz
		call	mpn	;Multiplikation des erhaltenen HEX Wertes mit 2^32/Frequenz des Referenzoszillators = DDS Frequenzwort
		call	frequ_update ;Serielle Ausgabe des DDS Frequenzwortes an den DDS
		return
;---------------------------------Wandlung BCD in ASCII und unterdrückung führender Nullen und Entscheidung ob MHz, KHz oder Hz ausgegeben werden + Punkte setzen
LCD		;ASCII Wandlung
		movf	_100MHZ, 0		;umkopieren der BCD Frequenz in Ausgaberegister
		movwf	_100MHZLCD
		movf	_10MHZ, 0
		movwf	_10MHZLCD
		movf	_1MHZ, 0
		movwf	_1MHZLCD
		movf	_100KHZ, 0
		movwf	_100KHZLCD
		movf	_10KHZ, 0
		movwf	_10KHZLCD
		movf	_1KHZ, 0
		movwf	_1KHZLCD
		movf	_100HZ, 0
		movwf	_100HZLCD
		movf	_10HZ, 0
		movwf	_10HZLCD
		movf	_1HZ, 0
		movwf	_1HZLCD
		movlw	0x30
		addwf	_100MHZLCD, 1	;ASCII Wandlung der Ausgaberegister durch addition von 0x30
		addwf	_10MHZLCD, 1
		addwf	_1MHZLCD, 1
		addwf	_100KHZLCD, 1 
		addwf	_10KHZLCD, 1
		addwf	_1KHZLCD, 1
		addwf	_100HZLCD, 1
		addwf	_10HZLCD, 1 
		addwf	_1HZLCD, 1
		
		;Unterdrückung führender Nullen		
		;100MHZ Stelle
		movlw 	0x30
		subwf	_100MHZLCD, 0	;test  _100MHZLCD auf Wert Null
		btfss	status, z
		goto	_100MHZLCDnichtnull	;_100MHZ ist nicht NULL
		goto	_100MHZLCDnull	;_100MHZ ist NULL
_100MHZLCDnichtnull	movlw	'.'		;Punkte zur Unterteilung von MHZ und KHZ einschalten
		movwf	MHZpunkt
		movlw	'.'		
		movwf	KHZpunkt
		call	ausgabeLCD	;Frequenz ausgeben
		call	ausgabeMHZ	;Einheit (MHZ) ausgeben	
		return
_100MHZLCDnull	movlw	b'00100000'		;ASCII: Blank		
		movwf	_100MHZLCD	
		;10MHZ Stelle
		movlw 	0x30
		subwf	_10MHZLCD, 0	;test  _10MHZLCD auf Wert Null
		btfss	status, z
		goto	_10MHZLCDnichtnull	;_10MHZ ist nicht NULL
		goto	_10MHZLCDnull	;_10MHZ ist NULL
_10MHZLCDnichtnull	movlw	'.'		;Punkte zur Unterteilung von MHZ und KHZ einschalten
		movwf	MHZpunkt
		movlw	'.'		
		movwf	KHZpunkt
		call	ausgabeLCD	;Frequenz ausgeben
		call	ausgabeMHZ	;Einheit (MHZ) ausgeben	
		return
_10MHZLCDnull	movlw	b'00100000'		;ASCII: Blank		
		movwf	_10MHZLCD	
		;1MHZ Stelle	
		movlw 	0x30
		subwf	_1MHZLCD, 0	;test  _1MHZLCD auf Wert Null
		btfss	status, z
		goto	_1MHZLCDnichtnull	;_1MHZ ist nicht NULL
		goto	_1MHZLCDnull	;_1MHZ ist NULL
_1MHZLCDnichtnull	movlw	'.'		;Punkte zur Unterteilung von MHZ und KHZ einschalten
		movwf	MHZpunkt
		movlw	'.'		
		movwf	KHZpunkt
		call	ausgabeLCD	;Frequenz ausgeben
		call	ausgabeMHZ	;Einheit (MHZ) ausgeben	
		return
_1MHZLCDnull	movlw	b'00100000'		;ASCII: Blank		
		movwf	_1MHZLCD	
		;100KHZ Stelle
		movlw 	0x30
		subwf	_100KHZLCD, 0	;test  _100KHZLCD auf Wert Null
		btfss	status, z
		goto	_100KHZLCDnichtnull	;_100KHZ ist nicht NULL
		goto	_100KHZLCDnull	;_100KHZ ist NULL
_100KHZLCDnichtnull	movlw	b'00100000'		;Punkte zur Unterteilung von MHZ ausschalten und KHZ einschalten
		movwf	MHZpunkt
		movlw	'.'		
		movwf	KHZpunkt
		call	ausgabeLCD	;Frequenz ausgeben
		call	ausgabeKHZ	;Einheit (KHZ) ausgeben	
		return
_100KHZLCDnull	movlw	b'00100000'		;ASCII: Blank		
		movwf	_100KHZLCD	
		;10KHZ Stelle
		movlw 	0x30
		subwf	_10KHZLCD, 0	;test  _10KHZLCD auf Wert Null
		btfss	status, z
		goto	_10KHZLCDnichtnull	;_10KHZ ist nicht NULL
		goto	_10KHZLCDnull	;_10KHZ ist NULL
_10KHZLCDnichtnull	movlw	b'00100000'		;Punkte zur Unterteilung von MHZ ausschalten und KHZ einschalten
		movwf	MHZpunkt
		movlw	'.'		
		movwf	KHZpunkt
		call	ausgabeLCD	;Frequenz ausgeben
		call	ausgabeKHZ	;Einheit (KHZ) ausgeben	
		return
_10KHZLCDnull	movlw	b'00100000'		;ASCII: Blank		
		movwf	_10KHZLCD	
		;1KHZ Stelle
		movlw 	0x30
		subwf	_1KHZLCD, 0	;test  _1KHZLCD auf Wert Null
		btfss	status, z
		goto	_1KHZLCDnichtnull	;_1KHZ ist nicht NULL
		goto	_1KHZLCDnull	;_1KHZ ist NULL
_1KHZLCDnichtnull	movlw	b'00100000'		;Punkte zur Unterteilung von MHZ ausschalten und KHZ einschalten
		movwf	MHZpunkt
		movlw	'.'		
		movwf	KHZpunkt
		call	ausgabeLCD	;Frequenz ausgeben
		call	ausgabeKHZ	;Einheit (HZ) ausgeben	
		return
_1KHZLCDnull	movlw	b'00100000'		;ASCII: Blank		
		movwf	_1KHZLCD	
		;100HZ Stelle
		movlw 	0x30
		subwf	_100HZLCD, 0	;test  _100HZLCD auf Wert Null
		btfss	status, z
		goto	_100HZLCDnichtnull	;_100HZ ist nicht NULL
		goto	_100HZLCDnull	;_100HZ ist NULL
_100HZLCDnichtnull	movlw	b'00100000'		;Punkte zur Unterteilung von MHZ und KHZ ausschalten
		movwf	MHZpunkt
		movlw	b'00100000'		
		movwf	KHZpunkt
		call	ausgabeLCD	;Frequenz ausgeben
		call	ausgabeHZ		;Einheit (HZ) ausgeben	
		return
_100HZLCDnull	movlw	b'00100000'		;ASCII: Blank		
		movwf	_100HZLCD	
		;10HZ Stelle
		movlw 	0x30
		subwf	_10HZLCD, 0	;test  _10HZLCD auf Wert Null
		btfss	status, z
		goto	_10HZLCDnichtnull	;_10HZ ist nicht NULL
		goto	_10HZLCDnull	;_10HZ ist NULL
_10HZLCDnichtnull	movlw	b'00100000'		;Punkte zur Unterteilung von MHZ und KHZ ausschalten
		movwf	MHZpunkt
		movlw	b'00100000'		
		movwf	KHZpunkt
		call	ausgabeLCD	;Frequenz ausgeben
		call	ausgabeHZ		;Einheit (HZ) ausgeben	
		return
_10HZLCDnull	movlw	b'00100000'		;ASCII: Blank		
		movwf	_10HZLCD	
		;1HZ Stelle (auf jeden Fall ausgeben, auch wenn es Null ist)
		movlw	b'00100000'		;Punkte zur Unterteilung von MHZ und KHZ ausschalten
		movwf	MHZpunkt
		movlw	b'00100000'		
		movwf	KHZpunkt
		call	ausgabeLCD	;Frequenz ausgeben
		call	ausgabeHZ		;Einheit (HZ) ausgeben	
		return
;------------------------ENDE: Wandlung BCD in ASCII und unterdrückung führender Nullen und Entscheidung ob MHz, KHz oder Hz ausgegeben werden + Punkte setzen

;-------------------------------LCD Ausgabe der Frequenz
ausgabeLCD	movlw	b'10000001'			;Start Adresse des Ersten Zeichens (0x00 für 1. Zeile, 1. Zeichen)
		call	cmnd2LCD			;Kommandobyte für LCD
		movf	_100MHZLCD, 0
		call	data2LCD		
		movf	_10MHZLCD, 0
		call	data2LCD		
		movf	_1MHZLCD, 0
		call	data2LCD		
		movf	MHZpunkt, 0
		call	data2LCD		
		movf	_100KHZLCD, 0
		call	data2LCD		
		movf	_10KHZLCD, 0
		call	data2LCD		
		movf	_1KHZLCD, 0
		call	data2LCD		
		movf	KHZpunkt, 0
		call	data2LCD
		movf	_100HZLCD, 0
		call	data2LCD		
		movf	_10HZLCD, 0
		call	data2LCD		
		movf	_1HZLCD, 0
		call	data2LCD		
		return
;-------------------------------ENDE:LCD Ausgabe der Frequenz
;------------------------------LCD Ausgabe Einheiten
ausgabeMHZ	;Einheit MHZ	
		movlw	' '
		call	data2LCD
		movlw	'M'
		call	data2LCD
		movlw	'H'
		call	data2LCD
		movlw	'z'
		call	data2LCD
		return
ausgabeKHZ	;Einheit KHZ
		movlw	' '
		call	data2LCD
		movlw	'k'
		call	data2LCD
		movlw	'H'
		call	data2LCD
		movlw	'z'
		call	data2LCD
		return
ausgabeHZ		;Einheit HZ
		movlw	' '
		call	data2LCD
		movlw	' '
		call	data2LCD
		movlw	'H'
		call	data2LCD
		movlw	'z'
		call	data2LCD
		return
;------------------------------ENDE: LCD Ausgabe Einheiten		
		
;-------------BCD in DUAL Umwandlung der Frequenz
;----------Frequenzeingabe über BCD Register ___100MHZ - ___1HZ, 
;----------Binärdaten in f_b0 - f_b3 (f_b0 = niedrigstwertiges Byte); mit diesen kann dann das dds wort berechnet werden 
;-------getestet, geht gut
con   	  	clrf	f_b0
        		clrf    	f_b1
       		clrf    	f_b2
        		clrf	f_b3
		movf	_100MHZ, 0
		call	bin
		movf	_10MHZ, 0
		call	bin
		movf	_1MHZ,0
		call	bin
		movf	_100KHZ, 0
		call 	bin
		movf	_10KHZ, 0
		call	bin
		movf	_1KHZ, 0
		call	bin
		movf	_100HZ, 0
		call	bin
		movf	_10HZ, 0
		call	bin
		movf	_1HZ, 0
		call	bin
		return

bin   		movwf	mem
      		bcf	status,0
        		rlf	f_b0,f
       		rlf	f_b1,f
    		rlf	f_b2,f
      		rlf	f_b3, f
      		movf	f_b0,w
     		movwf	sr0
    		movf	f_b1,w
      		movwf	sr1
      		movf	f_b2,w
      		movwf	sr2
      		movf	f_b3,w
      		movwf	sr3
      		bcf	status,0
      		rlf	f_b0,f
       		rlf	f_b1,f
       		rlf	f_b2,f
       		rlf	f_b3,f
       		bcf	status,0
       		rlf	f_b0,f
       		rlf	f_b1,f
       		rlf	f_b2,f
     		rlf	f_b3, f
       		clrf	cf
       		movf	sr0,w
       		movwf	sum
       		movf	f_b0,w
      		call	adc
     		movwf	f_b0
	
      		movf	sr1,w
      		movwf	sum
      		movf	f_b1,w
       		call	adc
       		movwf	f_b1

        		movf	sr2,w
     	   	movwf	sum
        		movf	f_b2,w
	        	call	adc
	        	movwf	f_b2
	
	        	movf	sr3,w
     	   	movwf	sum
        		movf	f_b3,w
	        	call	adc
	        	movwf	f_b3
	
	        	
	        	clrf	cf
	        	movf	mem,w
	        	movwf	sum
	        	movf	f_b0,w
	        	call	adc
	        	movwf	f_b0
	        	clrf	sum
	        	movf	f_b1,w
	        	call	adc
	        	movwf	f_b1
	        	movf	f_b2,w
	        	call	adc
	        	movwf	f_b2
		movf	f_b3,w
	        	call	adc
	        	movwf	f_b3
		
	        	return

adc	     	addwf	cf,w
	        	btfss	status,0
	        	bcf	cf,0
	        	addwf	sum,w
	        	btfsc	status,0
	        	bsf	cf,0
	        	return
;------------Ende BCD nach BINÄR Wandlung

;-------------QRG für DDS berechnen (0 HZ - 999999HZ)
;-------------Multiplikation mit "Konstanten"
;-------------const:=2^32/400.000.000Hz=0a.bc c7 71 Hex
;----------eingabe f_b0 bis f_b3 aus der bcd nach hex wandlung:
;----------ausgabe die genutzt wird: f_b6 (höchstes) bis f_b3 (niedrigstes) byte 
;----------des DDS Frequenzwortes
;-------------getestet, geht

mpn		movlw	d'33'
	        	movwf	mem
	        	clrf	f_b4
	        	clrf	f_b5
	        	clrf	f_b6
	        	clrf	f_b7
	        	bcf	status,0
mpn1   		rrf	f_b7,f
	        	rrf	f_b6,f
	        	rrf	f_b5,f
	        	rrf	f_b4,f
	
	        	rrf	f_b3,f
	        	rrf	f_b2,f
	        	rrf	f_b1,f
	        	rrf	f_b0,f
	        	btfss	status,0
	        	goto	mpn2
	
	        	clrf	cf
	        	movf	ref_clk0, 0		;niedrigstes byte des multiplikationsfaktors
	        	movwf	sum
	        	movf	f_b4,w
	        	call	adc
	        	movwf	f_b4
		
	        	movf	ref_clk1, 0
	        	movwf	sum
	        	movf	f_b5,w
	        	call	adc
	        	movwf	f_b5
	
	        	movf	ref_clk2, 0
	        	movwf	sum
	        	movf	f_b6,w
	        	call	adc
	        	movwf	f_b6
	
	        	movf	ref_clk3, 0	;höchstes byte des multiplikationsfaktors
	        	movwf	sum
	        	movf	f_b7,w
	        	call	adc
	        	movwf	f_b7

mpn2		decfsz	mem,f
	        	goto	mpn1
	        	return
;-------------Ende QRG Berechnung


;-------------------------------Initialisierung der LCD Anzeige
init_lcd		call    	wait_16ms         ; Wait for LCD to power up
		movlw   	b'00110000'              ; LCD init instruction (First)
		movwf   	portb             ; Send to LCD via RB7..RB0
		bsf     	lcd_e       ; Set the LCD E line high,
		call    	wait_16ms         ;   wait a "long" time,
		bcf     	lcd_e       ;   and then Clear E 
		movlw   	b'00110000'              ; LCD init instruction (Second)
		movwf   	portb             ; Send to LCD via RB7..RB0
		bsf     	lcd_e       ; Set E high,
		call    	wait_16ms         ;   wait a while,
		bcf     	lcd_e       ;   and then Clear E 
		movlw   	b'00110000'              ; LCD init instruction (Third)
		movwf   	portb             ; Send to LCD via RB7..RB0
		bsf     	lcd_e       ; Set E high,
		call    	wait_16ms         ;   wait a while,
		bcf     	lcd_e       ;   and then Clear E
		movlw   	b'00101000'              ; 4-bit mode instruction, 2 lines, 5*7 dot matrix
		call	cmnd2LCD	    ;Send command to LCD
		movlw   	b'00001000'              ; Display off, cursor and blink off 
		call    	cmnd2LCD          ; Send command to LCD
		movlw   	b'00000001'              ; Clear and reset cursor
		call    	cmnd2LCD          ; Send command in w to LCD
		movlw   	b'00000110'              ; Set cursor to move right, no shift
		call    	cmnd2LCD          ; Send command in w to LCD
		movlw   	b'00001100'              ; Display on, cursor and blink off
		call    	cmnd2LCD          ; Send command in w to LCD
		return                    ; 

;*****************************************************************************
; * Purpose:  Send Command or Data byte to the LCD                            *
; *           Entry point cmnd2LCD:  Send a Command to the LCD                *
; *           Entry Point data2LCD:  Send a Data byte to the LCD              *
; *                                                                           *
; *   Input:  W has the command or data byte to be sent to the LCD.           *
; *                                                                           *
; *  Output:  None                                                            *
; *****************************************************************************
;
cmnd2LCD   ; ****** Entry point ******
		movwf   	lcd_char          ; Save byte to write to LCD
		clrf    	rs_value          ; Remember to clear RS  (clear rs_value)   
		bcf     	lcd_rs      ; Set RS for Command to LCD
		goto    	write2LCD         ; Go to common code
data2LCD   ; ****** Entry point ********
		movwf   	lcd_char          ; Save byte to write to LCD
		bsf     	rs_value,0        ; Remember to set RS (set bit 0 of rs_value)
		bsf     	lcd_rs      ; Set RS for Data to LCD
write2LCD
		call    	busy_check        ; Check to see if LCD is ready for new data
		clrf    	portb             ; Clear all of Port B (inputs and outputs)
		bsf     	status, rp0      ; Switch to bank 1 for Tristate operation
		movlw   	0x00              ; Set up to enable portb data pins
		movwf   	trisb             ; All pins (RB7..RB0) are back to outputs
		bcf     	status, rp0      ; Switch to bank 0
		bcf     	lcd_rw      ; Set LCD back to Write mode  (RW = 0)
		bcf     	lcd_rs      ; Guess RS should be clear              
		btfsc   	rs_value,0        ; Should RS be clear?  (is bit 0 == 0?) 
		bsf     	lcd_rs      ; No, set RS                            
;
; Transfer Most Significant nibble  (XXXX portion of XXXXYYYY)
;
		movlw   	0x0F              ; Set up mask                                 
		andwf   	portb,f           ; Clear old RB7..RB4 
		movf    	lcd_char,w        ; Put byte of data into W
		andlw   	0xF0              ; Mask to give XXXX0000 in W
		iorwf   	portb,f           ; Send to RB7..RB4 without changing RB3..RB0
		bsf     	lcd_e       ; Pulse the E line high,
		nop            	           ;   wait, 
		bcf     	lcd_e       ;   and drop it again
;
; Transfer Least Significant nibble  (YYYY portion of XXXXYYYY)
;
		movlw  	0x0F              ; Set up mask                                 
		andwf   	portb,f           ; Clear old RB7..RB4
		swapf   	lcd_char,w        ; Move LS nibble of data to MS position in W
		andlw   	0xF0              ; Mask to give YYYY0000 in W
		iorwf   	portb,f           ; Send to RB7..RB4 without changing RB3..RB0
		bsf     	lcd_e       ; Pulse the E line high,
		nop          		             ;   wait, 
		bcf     	lcd_e       ;   and drop it again
		return
;
;
; *****************************************************************************
; *                                                                           *
; * Purpose:  Check if LCD is done with the last operation.                   *
; *           This subroutine polls the LCD busy flag to determine if         *
; *           previous operations are completed.                              *
; *                                                                           *
; *   Input:  None                                                            *
; *                                                                           *
; *  Output:  PortB set as:  RB7..RB4 inputs                                  *
; *                          RB3..RB0 outputs                                 *
; *****************************************************************************
; 
busy_check
		clrf    	portb             ; Clear all outputs on PortB
		bsf     	status, rp0      ; Switch to bank 1 for Tristate operation
		movlw   	b'11110000'       ; Set RB7..RB4 as inputs, RB3..RB0 outputs
		movwf   	trisb             ;   via Tristate
		bcf     	status, rp0      ; Switch back to bank 0
		bcf     	lcd_rs      ; Set up LCD for Read Busy Flag (RS = 0) 
		bsf     	lcd_rw      ; Set up LCD for Read (RW = 1)  
		movlw   	0xff              ; Set up constant 255
		movwf   	timer1            ;   for timer loop counter
LCD_is_busy
		bsf     	lcd_e       ; Set E high
		movf    	portb, w           ; Read PortB into W
		movwf   	lcd_read          ; Save W for later testing
		bcf     	lcd_e       ; Drop E again
		nop                       		; Wait a
		nop            	           ;   while
		bsf     	lcd_e       ; Pulse E high (dummy read of lower nibble),
		nop            	           ;   wait,
		bcf     	lcd_e       ;   and drop E again
		decf    	timer1,f          ; Decrement loop counter
		btfsc   	status, z        ; Is loop counter down to zero?
		goto    	not_busy          ; If yes, return regardless
		btfsc   	lcd_read,7        ; Is Busy Flag (RB7) in save byte clear?
		goto    	LCD_is_busy       ; If not, it is busy so jump back
not_busy
		return                    ; 
;


;--------------------------------Initialisierung des AD9951-------------
init		;register cfr2 des ad9951
		movlw	b'00000001'		;adresse cfr2 im schreibmodus	
		movwf	daten
		call	ser_aus
		movlw	b'00000000'		;höchstes byte vom cfr2 (intern im ad9951 nicht verwendet)
		movwf	daten
		call	ser_aus
		movlw	b'00000000'		;zweithöchstes byte vom cfr2
		movwf	daten
		call	ser_aus
		movlw	b'00101110'		;niedrigstes byte vom cfr2
		movwf	daten
		call	ser_aus
		bsf	strobe
		bcf	strobe
		return
;-------------------------------- ENDE Initialisierung des AD9951------------

;===================Serielle Ausgabe der frequenz an den DDS====
;---------------------------------Umkopieren der Frequenzdaten: Ausgabe des Frequenzwortes
frequ_update	movlw	b'00000100'		;adresse Frequenzwort im schreibmodus
		movwf	daten
		call	ser_aus
		movf	f_b6, 0		;höchstes byte vom Frequenzwort
		movwf	daten
		call	ser_aus
		movf	f_b5, 0		;zweithöchstes byte vom Frequenzwort
		movwf	daten
		call	ser_aus
		movf	f_b4, 0		;dritthöchstes byte vom Frequenzwort
		movwf	daten
		call	ser_aus
		movf	f_b3, 0		;niedrigstes byte vom Frequenzwort
		movwf	daten
		call	ser_aus
		bsf	strobe
		bcf	strobe
		return	


;--------------------------------serielle Datenausgabe an DDS AD9951 immer 8 bit-----
ser_aus		movlw	0x08
		movwf	ausgabezaehler
		bcf	status, c
naechstes_bit	rlf 	daten, 1
		btfss	status, c
		goto	daten_null
		goto	daten_eins
			
daten_null		bcf	datenausgabe
		bsf	clok					
		goto	$+3
daten_eins		bsf	datenausgabe
		bsf	clok
		bcf	clok
		
		decfsz	ausgabezaehler, 1
		goto	naechstes_bit
		bcf	datenausgabe
				
		return
;===================ENDE serielle Ausgabe der frequenz an den DDS====

;==================PAUSEN======================
; *****************************************************************************
; *                                                                           *
; * Purpose:  Wait for a specified number of milliseconds.                    *
; *                                                                           *
; *           Entry point wait_128ms:  Wait for 128 msec                      *
; *           Entry point wait_64ms :  Wait for 64 msec                       *
; *           Entry point wait_32ms :  Wait for 32 msec                       *
; *           Entry point wait_16ms :  Wait for 16 msec                       *
; *           Entry point wait_8ms  :  Wait for 8 msec                        *
; *                                                                           *
; *   Input:  None                                                            *
; *                                                                           *
; *  Output:  None                                                            *
; *                                                                           *
; *****************************************************************************
;
wait_128ms  ; ****** Entry point ******    
		movlw  	0xff              ; Set up outer loop 
		movwf   	timer1            ;   counter to 255
		goto    	outer_loop        ; Go to wait loops
wait_64ms  ; ****** Entry point ******     
		movlw   	0x80              ; Set up outer loop
		movwf   	timer1            ;   counter to 128
		goto    	outer_loop        ; Go to wait loops
wait_32ms   ; ****** Entry point ******    
		movlw   	0x40              ; Set up outer loop
		movwf   	timer1            ;   counter to 64
		goto    	outer_loop        ; Go to wait loops
wait_16ms   ; ****** Entry point ******    
		movlw   	0x20              ; Set up outer loop
		movwf   	timer1            ;   counter to 32  
		goto    	outer_loop        ; Go to wait loops
wait_8ms   ; ****** Entry point ******     
		movlw   	0x10              ; Set up outer loop
		movwf   	timer1            ;   counter to 16
		goto 	outer_loop	  ; Fall through into wait loops
wait_1ms   ; ****** Entry point ******     
		movlw   	0x02              ; Set up outer loop
		movwf   	timer1            ;   counter to 2
		goto	outer_loop

;-----------------------------diese pausen sind immer  um 5ms kürzer als angegeben
wait_10ms   ; ****** Entry point ******     
		movwf	d'9'              ; Set up outer loop
		movwf   	timer1            ;   counter to 9
		goto	outer_loop
wait_20ms   ; ****** Entry point ******     
		movwf	d'29'              ; Set up outer loop
		movwf   	timer1            ;   counter to 29 
		goto	outer_loop
wait_50ms   ; ****** Entry point ******     
		movwf	d'87'              ; Set up outer loop
		movwf   	timer1            ;   counter to 87
		goto	outer_loop
wait_100ms   ; ****** Entry point ******     
		movwf	d'194'              ; Set up outer loop
		movwf   	timer1            ;   counter to 194
		goto	outer_loop



		
; Wait loops used by other wait routines
;  - 1 microsecond per instruction (with a 4 MHz microprocessor crystal)
;  - 510 instructions per inner loop
;  - (Timer1 * 514) instructions (.514 msec) per outer loop
;  - Round off to .5 ms per outer loop
;
outer_loop                        
		movlw   	0xff              ; Set up inner loop counter
		movwf   	timer2            ;   to 255
inner_loop
		decfsz  	timer2,f          ; Decrement inner loop counter
		goto    	inner_loop        ; If inner loop counter not down to zero, 
					  ;   then go back to inner loop again
		decfsz  	timer1,f          ; Yes, Decrement outer loop counter
		goto  	outer_loop        ; If outer loop counter not down to zero,
					  ;   then go back to outer loop again
		return                    ; Yes, return to caller
;       
; *****************************************************************************

;=================================================
;=================================================
;=================PAGE 1 des Speichers
		org	0x800
;---------------------------lauter Subroutinen
wobbeln		;retten des Frequenzregisters
		movf	_100MHZ, 0
		movwf	temp_100MHZ
		movf	_10MHZ, 0
		movwf	temp_10MHZ
		movf	_1MHZ, 0
		movwf	temp_1MHZ
		movf	_100KHZ, 0
		movwf	temp_100KHZ
		movf	_10KHZ, 0
		movwf	temp_10KHZ
		movf	_1KHZ, 0
		movwf	temp_1KHZ
		movf	_100HZ, 0
		movwf	temp_100HZ
		movf	_10HZ, 0
		movwf	temp_10HZ
		movf	_1HZ, 0
		movwf	temp_1HZ

wobbelnauspage2	movlw	b'00000001'
		page0
		call	cmnd2LCD
		movlw	b'10000001'
		page0
		call	cmnd2LCD
		movlw	'W'
		page0
		call	data2LCD
		movlw	'o'
		page0
		call	data2LCD
		movlw	'b'
		page0
		call	data2LCD
		movlw	'b'
		page0
		call	data2LCD
		movlw	'e'
		page0
		call	data2LCD
		movlw	'l'
		page0
		call	data2LCD
		movlw	'n'
		page0
		call	data2LCD
		page1
		btfss	taster
		goto	$-1
		
		
		;movlw	d'60'
		;movwf	zaehler
;wartezeit0		page0
;		call	wait_8ms
;		page1
;		decfsz	zaehler, 1
;		goto	wartezeit0
;

;		movlw	b'00000001'		;lösche LCD Anzeige
;		page0
;		call	cmnd2LCD
		
;-------------------------------Ausgabe der Startfrequenz	zum Wobbeln + Einstellen der Frequenz und dabei auch Änderung der Schrittweite

	;hole startfrequenz aus eeprom
		page3
		call	startqrg_eeprom_lies
		page1
	;------gib sie an LCD aus
		page3
		call	startfrequenz	
		page1
wobbelnuntereqrg	;btfsc	taster
		;goto	drehtestung		;taster gedrückt, unterprogramm zur einstellung der schrittweite
		;call	wait_8ms		
		;btfsc	taster
		;goto	drehtestung
		;call	schritteeinstellen
		
		;test wie lange der taster gedrückt wird
		movlw	d'100'
		movwf	zaehler	
		btfsc	taster
		goto	drehtestungwobbelnup
		page0
		call	wait_8ms
		page1
		btfsc	taster
		goto	drehtestungwobbelnup
		
		page3
		call	schritteeinstellenlcd
		page1
		
testtasterwobbelnup	btfss	taster
		goto	testzaehlerwobbelnup
schrittweitetest1	movlw	b'00000001'
		page0
		call	cmnd2LCD
		page3
		call	schritteeinstellenlcd
		page0
		call	schritteeinstellen	;taster war kürzer als 1 Sekunde gedrückt
		page3
		call	startfrequenz
		page1
		goto	drehtestungwobbelnup

testzaehlerwobbelnup	page0
		call	wait_8ms
		page1
		decfsz	zaehler, 1
		goto	testtasterwobbelnup
wobbelntest1	goto	wobbelnobereqrgstart		;taster war länger als 1 Sekunde gedrückt
		
		;ENDE test wie lange der taster gedrückt wird
	
	
drehtestungwobbelnup	page0
		call	dreh		;taster nicht gedrückt, drehgeber routine aufrufen
		page1
		btfss	rauf_runter, 1	;wurde gedreht?
		goto	nichtgedrehtwobbeln	;es wurde nicht gedreht 
		movlw	b'00000011'		;es wurde gedreht
		subwf	rauf_runter, 0	;wurde nach oben gedreht?
		btfss	status, z		
		goto	nachuntenwobbeln	;nach unten
		goto	nachobenwobbeln	;nach oben
nachuntenwobbeln	btfss	schrittzaehler, 0	;1 Hz Schritte? 
		goto	a		;nein
		page0
		call	qrgminus_1HZ	;ja
		page1
		goto	undrauswobbeln		
a		btfss	schrittzaehler, 1	;10 Hz Schritte?
		goto	ab		;nein
		page0
		call	qrgminus_10HZ	;ja
		page1
		goto	undrauswobbeln
ab		btfss	schrittzaehler, 2	;100 Hz Schritte?
		goto	abc		;nein
		page0
		call	qrgminus_100HZ	;ja
		page1
		goto	undrauswobbeln
abc		btfss	schrittzaehler, 3	;1 kHz Schritte?
		goto	abcd		;nein
		page0
		call	qrgminus_1KHZ	;ja
		page1
		goto	undrauswobbeln
abcd		btfss	schrittzaehler, 4	;10 kHz Schritte?
		goto	abcde		;nein
		page0
		call	qrgminus_10KHZ	;ja
		page1
		goto	undrauswobbeln
abcde		btfss	schrittzaehler, 5	;100 kHz Schritte?
		goto	abcdef		;nein
		page0
		call	qrgminus_100KHZ	;ja
		page1
		goto	undrauswobbeln
abcdef		btfss	schrittzaehler, 6	;1 MHz Schritte?
		goto	abcdefg		;nein
		page0
		call	qrgminus_1MHZ	;ja
		page1
		goto	undrauswobbeln
abcdefg		page0
		call	qrgminus_10MHZ	;dann könnens ja blos noch 10MHZ Schritte sein
		page1
		goto	undrauswobbeln


nachobenwobbeln	btfss	schrittzaehler, 0	;1 Hz Schritte? 
		goto	a1		;nein
		page0
		call	qrgplus_1HZ	;ja
		page1
		goto	undrauswobbeln		
a1		btfss	schrittzaehler, 1	;10 Hz Schritte?
		goto	ab1		;nein
		page0
		call	qrgplus_10HZ	;ja
		page1
		goto	undrauswobbeln
ab1		btfss	schrittzaehler, 2	;100 Hz Schritte?
		goto	abc1		;nein
		page0
		call	qrgplus_100HZ	;ja
		page1
		goto	undrauswobbeln
abc1		btfss	schrittzaehler, 3	;1 kHz Schritte?
		goto	abcd1		;nein
		page0
		call	qrgplus_1KHZ	;ja
		page1
		goto	undrauswobbeln
abcd1		btfss	schrittzaehler, 4	;10 kHz Schritte?
		goto	abcde1		;nein
		page0
		call	qrgplus_10KHZ	;ja
		page1
		goto	undrauswobbeln
abcde1		btfss	schrittzaehler, 5	;100 kHz Schritte?
		goto	abcdef1		;nein
		page0
		call	qrgplus_100KHZ	;ja
		page1
		goto	undrauswobbeln
abcdef1		btfss	schrittzaehler, 6	;1 MHz Schritte?
		goto	abcdefg1		;nein
		page0
		call	qrgplus_1MHZ	;ja
		page1
		goto	undrauswobbeln
abcdefg1		page0
		call	qrgplus_10MHZ	;dann könnens ja blos noch 10MHZ Schritte sein
		page1
		goto	undrauswobbeln
			


undrauswobbeln	;call	dds_update		;Berechnung und Ausgabe an den DDS
		page0
		call	LCD		;Führende Nullen unterdrücken, ASCII Wandlung, Einheit bestimmen, raus an LCD Anzeige
		page1
		goto	wobbelnuntereqrg
nichtgedrehtwobbeln 	goto	wobbelnuntereqrg	
		
;-------------------------------ENDE Ausgabe der Startfrequenz zum Wobbeln + Einstellen der Frequenz und dabei auch Änderung der Schrittweite
		



;-------------------------------Ausgabe der Endfrequenz	zum Wobbeln + Einstellen der Frequenz und dabei auch Änderung der Schrittweite
wobbelnobereqrgstart	;retten der unteren startfrequenz
		movf	_100MHZ, 0
		movwf	tempstart_100MHZ
		movf	_10MHZ, 0
		movwf	tempstart_10MHZ
		movf	_1MHZ, 0
		movwf	tempstart_1MHZ
		movf	_100KHZ, 0
		movwf	tempstart_100KHZ
		movf	_10KHZ, 0
		movwf	tempstart_10KHZ
		movf	_1KHZ, 0
		movwf	tempstart_1KHZ
		movf	_100HZ, 0
		movwf	tempstart_100HZ
		movf	_10HZ, 0
		movwf	tempstart_10HZ
		movf	_1HZ, 0
		movwf	tempstart_1HZ
	
		;hole Endfrequenz aus eeprom
obereqrgzuniedrig	page3
		call	endqrg_eeprom_lies
		page1
	;------gib sie an LCD aus
		page3
		call	endfrequenz	
		page1

		btfss	taster
		goto	$-1
		

wobbelnobereqrg	;btfsc	taster
		;goto	drehtestung		;taster gedrückt, unterprogramm zur einstellung der schrittweite
		;call	wait_8ms		
		;btfsc	taster
		;goto	drehtestung
		;call	schritteeinstellen
		
		;test wie lange der taster gedrückt wird
		movlw	d'100'
		movwf	zaehler	
		btfsc	taster
		goto	drehtestungwobbelnup1
		page0
		call	wait_8ms
		page1
		btfsc	taster
		goto	drehtestungwobbelnup1

		page3
		call	schritteeinstellenlcd
		page1


testtasterwobbelnup1	btfss	taster
		goto	testzaehlerwobbelnup1
schrittweitetest2	movlw	b'00000001'
		page0
		call	cmnd2LCD
		page3
		call	schritteeinstellenlcd
		page0
		call	schritteeinstellen	;taster war kürzer als 1 Sekunde gedrückt
		page3
		call	endfrequenz
		page1
		goto	drehtestungwobbelnup1

testzaehlerwobbelnup1	page0
		call	wait_8ms
		page1
		decfsz	zaehler, 1
		goto	testtasterwobbelnup1
wobbelntest2	goto	testobereqrg		;taster war länger als 1 Sekunde gedrückt
		
		;ENDE test wie lange der taster gedrückt wird
	
	
drehtestungwobbelnup1	page0
		call	dreh		;taster nicht gedrückt, drehgeber routine aufrufen
		page1
		btfss	rauf_runter, 1	;wurde gedreht?
		goto	nichtgedrehtwobbeln1	;es wurde nicht gedreht 
		movlw	b'00000011'		;es wurde gedreht
		subwf	rauf_runter, 0	;wurde nach oben gedreht?
		btfss	status, z		
		goto	nachuntenwobbeln1	;nach unten
		goto	nachobenwobbeln1	;nach oben
nachuntenwobbeln1	btfss	schrittzaehler, 0	;1 Hz Schritte? 
		goto	a2		;nein
		page0
		call	qrgminus_1HZ	;ja
		page1
		goto	undrauswobbeln1		
a2		btfss	schrittzaehler, 1	;10 Hz Schritte?
		goto	ab2		;nein
		page0
		call	qrgminus_10HZ	;ja
		page1
		goto	undrauswobbeln1
ab2		btfss	schrittzaehler, 2	;100 Hz Schritte?
		goto	abc2		;nein
		page0
		call	qrgminus_100HZ	;ja
		page1
		goto	undrauswobbeln1
abc2		btfss	schrittzaehler, 3	;1 kHz Schritte?
		goto	abcd2		;nein
		page0
		call	qrgminus_1KHZ	;ja
		page1
		goto	undrauswobbeln1
abcd2		btfss	schrittzaehler, 4	;10 kHz Schritte?
		goto	abcde2		;nein
		page0
		call	qrgminus_10KHZ	;ja
		page1
		goto	undrauswobbeln1
abcde2		btfss	schrittzaehler, 5	;100 kHz Schritte?
		goto	abcdef2		;nein
		page0
		call	qrgminus_100KHZ	;ja
		page1
		goto	undrauswobbeln1
abcdef2		btfss	schrittzaehler, 6	;1 MHz Schritte?
		goto	abcdefg2		;nein
		page0
		call	qrgminus_1MHZ	;ja
		page1
		goto	undrauswobbeln1
abcdefg2		page0
		call	qrgminus_10MHZ	;dann könnens ja blos noch 10MHZ Schritte sein
		page1
		goto	undrauswobbeln1


nachobenwobbeln1	btfss	schrittzaehler, 0	;1 Hz Schritte? 
		goto	a3		;nein
		page0
		call	qrgplus_1HZ	;ja
		page1
		goto	undrauswobbeln1		
a3		btfss	schrittzaehler, 1	;10 Hz Schritte?
		goto	ab3		;nein
		page0
		call	qrgplus_10HZ	;ja
		page1
		goto	undrauswobbeln1
ab3		btfss	schrittzaehler, 2	;100 Hz Schritte?
		goto	abc3		;nein
		page0
		call	qrgplus_100HZ	;ja
		page1
		goto	undrauswobbeln1
abc3		btfss	schrittzaehler, 3	;1 kHz Schritte?
		goto	abcd3		;nein
		page0
		call	qrgplus_1KHZ	;ja
		page1
		goto	undrauswobbeln1
abcd3		btfss	schrittzaehler, 4	;10 kHz Schritte?
		goto	abcde3		;nein
		page0
		call	qrgplus_10KHZ	;ja
		page1
		goto	undrauswobbeln1
abcde3		btfss	schrittzaehler, 5	;100 kHz Schritte?
		goto	abcdef3		;nein
		page0
		call	qrgplus_100KHZ	;ja
		page1
		goto	undrauswobbeln1
abcdef3		btfss	schrittzaehler, 6	;1 MHz Schritte?
		goto	abcdefg3		;nein
		page0
		call	qrgplus_1MHZ	;ja
		page1
		goto	undrauswobbeln1
abcdefg3		page0
		call	qrgplus_10MHZ	;dann könnens ja blos noch 10MHZ Schritte sein
		page1
		goto	undrauswobbeln1
			


undrauswobbeln1	;call	dds_update		;Berechnung und Ausgabe an den DDS
		page0
		call	LCD		;Führende Nullen unterdrücken, ASCII Wandlung, Einheit bestimmen, raus an LCD Anzeige
		page1
		goto	wobbelnobereqrg
nichtgedrehtwobbeln1 	goto	wobbelnobereqrg	


;-------------------------------ENDE Ausgabe der Endfrequenz zum Wobbeln + Einstellen der Frequenz und dabei auch Änderung der Schrittweite

;--------------------------------Teste ob Endfrequenz größer als Startfrequenz ist, wenn nicht gehe nach "wobbelnobereqrg", wenn ja mach weiter		
testobereqrg		;Retten der oberen Endfrequenz
		movf	_100MHZ, 0
		movwf	up_100MHZ
		movf	_10MHZ, 0
		movwf	up_10MHZ
		movf	_1MHZ, 0
		movwf	up_1MHZ
		movf	_100KHZ, 0
		movwf	up_100KHZ
		movf	_10KHZ, 0
		movwf	up_10KHZ
		movf	_1KHZ, 0
		movwf	up_1KHZ
		movf	_100HZ, 0
		movwf	up_100HZ
		movf	_10HZ, 0
		movwf	up_10HZ
		movf	_1HZ, 0
		movwf	up_1HZ


	 ;Endfrequenz in up_100MHZ usw, Startfrequenz in tempstart_100MHZ
		movf	up_100MHZ, 0
		subwf	tempstart_100MHZ, 0
		btfss	status ,0
		goto	passt	;kein überlauf
		btfss	status, 2
		goto	passtnicht
		movf	up_10MHZ, 0
		subwf	tempstart_10MHZ, 0
		btfss	status ,0
		goto	passt	;kein überlauf
		btfss	status, 2
		goto	passtnicht
		movf	up_1MHZ, 0
		subwf	tempstart_1MHZ, 0
		btfss	status ,0
		goto	passt	;kein überlauf
		btfss	status, 2
		goto	passtnicht
		movf	up_100KHZ, 0
		subwf	tempstart_100KHZ, 0
		btfss	status ,0
		goto	passt	;kein überlauf
		btfss	status, 2
		goto	passtnicht
		movf	up_10KHZ, 0
		subwf	tempstart_10KHZ, 0
		btfss	status ,0
		goto	passt	;kein überlauf
		btfss	status, 2
		goto	passtnicht
		movf	up_1KHZ, 0
		subwf	tempstart_1KHZ, 0
		btfss	status ,0
		goto	passt	;kein überlauf
		btfss	status, 2
		goto	passtnicht
		movf	up_100HZ, 0
		subwf	tempstart_100HZ, 0
		btfss	status ,0
		goto	passt	;kein überlauf
		btfss	status, 2
		goto	passtnicht
		movf	up_10HZ, 0
		subwf	tempstart_10HZ, 0
		btfss	status ,0
		goto	passt	;kein überlauf
		btfss	status, 2
		goto	passtnicht
		movf	up_1HZ, 0
		subwf	tempstart_1HZ, 0
		btfss	status ,0
		goto	passt	;kein überlauf
		;goto	passtnicht

passtnicht		;btfss	taster
		;goto	$-1
		goto	obereqrgzuniedrig
;---------------------Endqrg größer Startqrg? Abfragen der Wobbelschrittgröße		
passt		;--------------------------------ENDE Testabfragen ob Endfrequenz größer als Startfrequenz ist	, Startqrg ist gerettet in tempstart_100MHZ und Endfrequenz in up_100MHZ

;------------------------------ Abfragen der Wobbelschrittgröße		
		;btfss	taster		;warte bis der Taster nicht mehr gedrückt ist
		;goto	$-1
		movlw	b'00000001'		;lösche LCD
		page0
		call	cmnd2LCD
		page1
		movlw	b'10000001'		;gib als erstes mal die aktuelle schrittweite aus		
		page0
		call	cmnd2LCD
		page1
		movlw	'W'
		page0
		call	data2LCD
		page1
		movlw	'o'
		page0
		call	data2LCD
		page1
		movlw	'b'
		page0
		call	data2LCD
		page1
		movlw	'b'
		page0
		call	data2LCD
		page1
		movlw	'e'
		page0
		call	data2LCD
		page1
		movlw	'l'
		page0
		call	data2LCD
		page1
		movlw	's'
		page0
		call	data2LCD
		page1
		movlw	'c'
		page0
		call	data2LCD
		page1
		movlw	'h'
		page0
		call	data2LCD
		page1
		movlw	'r'
		page0
		call	data2LCD
		page1
		movlw	'i'
		page0
		call	data2LCD
		page1
		movlw	't'
		page0
		call	data2LCD
		page1
		movlw	't'
		page0
		call	data2LCD
		page1
		movlw	'e'
		page0
		call	data2LCD
		page1
		
		btfss	taster		;warte bis der Taster nicht mehr gedrückt ist
		goto	$-1
		
		
schrittermittlungwobbel	movlw	b'00000001'
		subwf	schrittzaehler, 0
		btfss	status, z 
		goto	$+2
		goto	WobbelSchritt1Hz
		movlw	b'00000010'
		subwf	schrittzaehler, 0
		btfss	status, z 
		goto	$+2
		goto	WobbelSchritt10Hz
		movlw	b'00000100'
		subwf	schrittzaehler, 0
		btfss	status, z 
		goto	$+2
		goto	WobbelSchritt100Hz
		movlw	b'00001000'
		subwf	schrittzaehler, 0
		btfss	status, z 
		goto	$+2
		goto	WobbelSchritt1kHz
		movlw	b'00010000'
		subwf	schrittzaehler, 0
		btfss	status, z 
		goto	$+2
		goto	WobbelSchritt10kHz
		movlw	b'00100000'
		subwf	schrittzaehler, 0
		btfss	status, z 
		goto	$+2
		goto	WobbelSchritt100kHz
		movlw	b'01000000'
		subwf	schrittzaehler, 0
		btfss	status, z 
		goto	$+2
		goto	WobbelSchritt1MHz
		goto	WobbelSchritt10MHz


WobbelSchritt1Hz	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	'1'
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'H'
		page0
		call	data2LCD
		page1
		movlw	'z'
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		goto 	naechsteschrittweitewobbel

WobbelSchritt10Hz	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	'1'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'H'
		page0
		call	data2LCD
		page1
		movlw	'z'
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		goto 	naechsteschrittweitewobbel

WobbelSchritt100Hz	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	'1'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'H'
		page0
		call	data2LCD
		page1
		movlw	'z'
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		goto 	naechsteschrittweitewobbel

WobbelSchritt1kHz	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	'1'
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'k'
		page0
		call	data2LCD
		page1
		movlw	'H'
		page0
		call	data2LCD
		page1
		movlw	'z'
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		goto 	naechsteschrittweitewobbel

		
		
WobbelSchritt10kHz	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	'1'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'k'
		page0
		call	data2LCD
		page1
		movlw	'H'
		page0
		call	data2LCD
		page1
		movlw	'z'
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		goto 	naechsteschrittweitewobbel



WobbelSchritt100kHz	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	'1'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'k'
		page0
		call	data2LCD
		page1
		movlw	'H'
		page0
		call	data2LCD
		page1
		movlw	'z'
		page0
		call	data2LCD
		page1
		goto 	naechsteschrittweitewobbel



WobbelSchritt1MHz	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	'1'
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'M'
		page0
		call	data2LCD
		page1
		movlw	'H'
		page0
		call	data2LCD
		page1
		movlw	'z'
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		goto 	naechsteschrittweitewobbel



WobbelSchritt10MHz	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	'1'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'M'
		page0
		call	data2LCD
		page1
		movlw	'H'
		page0
		call	data2LCD
		page1
		movlw	'z'
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		goto 	naechsteschrittweitewobbel


		
		
naechsteschrittweitewobbel	
		page0
		call	dreh
		page1
		btfss	rauf_runter, 1	;wurde gedreht?
		goto	nichtgedrehtwobbel	;es wurde nicht gedreht 
		movlw	b'00000011'		;es wurde gedreht
		subwf	rauf_runter, 0	;wurde nach oben gedreht?
		btfss	status, z		
		goto	nachuntenwobbel10		;nach unten
		goto	nachobenwobbel10		;nach oben
nachuntenwobbel10	btfsc	schrittzaehler, 0	; 	
		goto	setzebit70		;bit 1 ist gesetzt
		goto	rotiererechtswobbel
setzebit70		movlw	b'10000000'		
		movwf	schrittzaehler
		goto	schrittermittlungwobbel
rotiererechtswobbel	bcf	status, c		;rotieren nach rechts
		rrf 	schrittzaehler, 1		
		goto	schrittermittlungwobbel

nachobenwobbel10	btfsc	schrittzaehler, 7	; 	
		goto	setzebit10		;bit 7 ist gesetzt
		goto	rotierelinkswobbel
setzebit10		movlw	b'00000001'		
		movwf	schrittzaehler
		goto	schrittermittlungwobbel
rotierelinkswobbel	bcf	status, c		;rotieren nach rechts
		rlf 	schrittzaehler, 1		
		goto	schrittermittlungwobbel
	
nichtgedrehtwobbel	btfsc	taster
		goto	naechsteschrittweitewobbel	;nein, nicht gedrückt
		page0
		call	wait_8ms	
		page1
		btfsc	taster
		goto	naechsteschrittweitewobbel
		
;wobbelwart		btfss	taster		;ja, warte bis der taster wieder losgelassen wirdel
;		goto	$-1
;		page0
;		call	wait_8ms
;		page1
;		btfss	taster
;			;ja, warte bis der taster wieder losgelassen wird
;		goto	wobbelwart
;		movlw	b'00000001'		;lösche LCD Anzeige
;		page0
;		call	cmnd2LCD
;		page1


;-----------------------------------Frage nach der Haltezeit	, wie lange sollen die einzelnen qrgs stehen bleiben, Ergebnis in haltezaehler	
haltezeit		movlw	b'00000001'		;lösche LCD
		page0
		call	cmnd2LCD
		page1
		movlw	b'10000001'		;gib als erstes mal die aktuelle haltezeit aus		
		page0
		call	cmnd2LCD
		page1
		movlw	'H'
		page0
		call	data2LCD
		page1
		movlw	'a'
		page0
		call	data2LCD
		page1
		movlw	'l'
		page0
		call	data2LCD
		page1
		movlw	't'
		page0
		call	data2LCD
		page1
		movlw	'e'
		page0
		call	data2LCD
		page1
		movlw	'z'
		page0
		call	data2LCD
		page1
		movlw	'e'
		page0
		call	data2LCD
		page1
		movlw	'i'
		page0
		call	data2LCD
		page1
		movlw	't'
		page0
		call	data2LCD
		page1
		
		btfss	taster		;ja, warte bis der taster wieder losgelassen wirdel
		goto	$-1
		
		
haltezaehlerermittlung	movlw	b'00000001'
		subwf	haltezaehler, 0
		btfss	status, z 
		goto	$+2
		goto	haltezeit5ms
		movlw	b'00000010'
		subwf	haltezaehler, 0
		btfss	status, z 
		goto	$+2
		goto	haltezeit10ms
		movlw	b'00000100'
		subwf	haltezaehler, 0
		btfss	status, z 
		goto	$+2
		goto	haltezeit20ms
		movlw	b'00001000'
		subwf	haltezaehler, 0
		btfss	status, z 
		goto	$+2
		goto	haltezeit50ms
		movlw	b'00010000'
		subwf	haltezaehler, 0
		btfss	status, z 
		goto	$+2
		goto	haltezeit100ms
		movlw	b'00100000'
		subwf	haltezaehler, 0
		btfss	status, z 
		goto	$+2
		goto	haltezeit200ms
		movlw	b'01000000'
		subwf	haltezaehler, 0
		btfss	status, z 
		goto	$+2
		goto	haltezeit500ms
		goto	haltezeit1000ms


haltezeit5ms	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'5'
		page0
		call	data2LCD
		page1
		goto 	einheitms

haltezeit10ms	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'1'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		goto 	einheitms

haltezeit20ms	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'2'
		page0
		call	data2LCD
		page1
		movlw	'0' 
		page0
		call	data2LCD
		page1
		goto 	einheitms

haltezeit50ms	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'5'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		goto 	einheitms

		
		
haltezeit100ms	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'1'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		goto 	einheitms



haltezeit200ms	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'2'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		movlw	'0' 
		page0
		call	data2LCD
		page1
		goto 	einheitms



haltezeit500ms	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'5'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		goto 	einheitms



haltezeit1000ms	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	'1'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		
einheitms		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'm'
		page0
		call	data2LCD
		page1
		movlw	'S'
		page0
		call	data2LCD
		page1
		
		
		
naechsteschrittweitehaltezaehler	
		page0
		call	dreh
		page1
		btfss	rauf_runter, 1	;wurde gedreht?
		goto	nichtgedrehthaltezaehler	;es wurde nicht gedreht 
		movlw	b'00000011'		;es wurde gedreht
		subwf	rauf_runter, 0	;wurde nach oben gedreht?
		btfss	status, z		
		goto	nachuntenhaltezaehler		;nach unten
		goto	nachobenhaltezaehler	;nach oben
nachuntenhaltezaehler	btfsc	haltezaehler, 0	; 	
		goto	setzebit700		;bit 1 ist gesetzt
		goto	rotiererechtshaltezaehler
setzebit700		movlw	b'10000000'		
		movwf	haltezaehler
		goto	haltezaehlerermittlung
rotiererechtshaltezaehler	bcf	status, c		;rotieren nach rechts
		rrf 	haltezaehler, 1		
		goto	haltezaehlerermittlung

nachobenhaltezaehler	btfsc	haltezaehler, 7	; 	
		goto	setzebit100		;bit 7 ist gesetzt
		goto	rotierelinkshaltezaehler
setzebit100		movlw	b'00000001'		
		movwf	haltezaehler
		goto	haltezaehlerermittlung
rotierelinkshaltezaehler	bcf	status, c		;rotieren nach rechts
		rlf 	haltezaehler, 1		
		goto	haltezaehlerermittlung
	
nichtgedrehthaltezaehler	btfsc	taster
		goto	naechsteschrittweitehaltezaehler	;nein, nicht gedrückt
		page0
		call	wait_8ms	
		page1
		btfsc	taster
		goto	naechsteschrittweitehaltezaehler
		
;haltezaehlerwart		btfss	taster		;ja, warte bis der taster wieder losgelassen wirdel
;		goto	$-1
;		page0
;		call	wait_8ms
;		page1
;		btfss	taster
;			;ja, warte bis der taster wieder losgelassen wird
;		goto	haltezaehlerwart
		
;-----------------------ENDE Zeitdauer der einzelnen Frequenzen ermitteln Ergebnis in Haltezaehler


;----------------------Ermittlung wie lange nach der letzten frequenz gewartet werden soll, bis es wieder mit der Anfangsqrg losgeht, Ergebnis in haltezaehler1
endpause		movlw	b'00000001'		;lösche LCD
		page0
		call	cmnd2LCD
		page1
		movlw	b'10000001'		;gib als erstes mal die aktuelle haltezeit aus		
		page0
		call	cmnd2LCD
		page1
		movlw	'E'
		page0
		call	data2LCD
		page1
		movlw	'n'
		page0
		call	data2LCD
		page1
		movlw	'd'
		page0
		call	data2LCD
		page1
		movlw	'p'
		page0
		call	data2LCD
		page1
		movlw	'a'
		page0
		call	data2LCD
		page1
		movlw	'u'
		page0
		call	data2LCD
		page1
		movlw	's'
		page0
		call	data2LCD
		page1
		movlw	'e'
		page0
		call	data2LCD
		page1
		
		btfss	taster
		goto	$-1		
		
haltezaehlerermittlung1	movlw	b'00000001'
		subwf	haltezaehler1, 0
		btfss	status, z 
		goto	$+2
		goto	haltezeit5ms1
		movlw	b'00000010'
		subwf	haltezaehler1, 0
		btfss	status, z 
		goto	$+2
		goto	haltezeit10ms1
		movlw	b'00000100'
		subwf	haltezaehler1, 0
		btfss	status, z 
		goto	$+2
		goto	haltezeit20ms1
		movlw	b'00001000'
		subwf	haltezaehler1, 0
		btfss	status, z 
		goto	$+2
		goto	haltezeit50ms1
		movlw	b'00010000'
		subwf	haltezaehler1, 0
		btfss	status, z 
		goto	$+2
		goto	haltezeit100ms1
		movlw	b'00100000'
		subwf	haltezaehler1, 0
		btfss	status, z 
		goto	$+2
		goto	haltezeit200ms1
		movlw	b'01000000'
		subwf	haltezaehler1, 0
		btfss	status, z 
		goto	$+2
		goto	haltezeit500ms1
		goto	haltezeit1000ms1


haltezeit5ms1	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		goto 	einheitms1

haltezeit10ms1	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'1'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		goto 	einheitms1

haltezeit20ms1	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'2'
		page0
		call	data2LCD
		page1
		movlw	'0' 
		page0
		call	data2LCD
		page1
		goto 	einheitms1

haltezeit50ms1	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'5'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		goto 	einheitms1

		
		
haltezeit100ms1	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'1'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		goto 	einheitms1



haltezeit200ms1	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'2'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		movlw	'0' 
		page0
		call	data2LCD
		page1
		goto 	einheitms1



haltezeit500ms1	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'5'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		goto 	einheitms1



haltezeit1000ms1	movlw	b'11000001'
		page0
		call	cmnd2LCD
		page1
		movlw	'1'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		movlw	'0'
		page0
		call	data2LCD
		page1
		
einheitms1		movlw	' '
		page0
		call	data2LCD
		page1
		movlw	'm'
		page0
		call	data2LCD
		page1
		movlw	'S'
		page0
		call	data2LCD
		page1
		
		
		
naechsteschrittweitehaltezaehl1	
		page0
		call	dreh
		page1
		btfss	rauf_runter, 1	;wurde gedreht?
		goto	nichtgedrehthaltezaehler1	;es wurde nicht gedreht 
		movlw	b'00000011'		;es wurde gedreht
		subwf	rauf_runter, 0	;wurde nach oben gedreht?
		btfss	status, z		
		goto	nachuntenhaltezaehler1		;nach unten
		goto	nachobenhaltezaehler1	;nach oben
nachuntenhaltezaehler1	btfsc	haltezaehler1, 0	; 	
		goto	setzebit7000		;bit 1 ist gesetzt
		goto	rotiererechtshaltezaehler1
setzebit7000	movlw	b'10000000'		
		movwf	haltezaehler1
		goto	haltezaehlerermittlung1
rotiererechtshaltezaehler1	bcf	status, c		;rotieren nach rechts
		rrf 	haltezaehler1, 1		
		goto	haltezaehlerermittlung1

nachobenhaltezaehler1	btfsc	haltezaehler1, 7	; 	
		goto	setzebit1000		;bit 7 ist gesetzt
		goto	rotierelinkshaltezaehler1
setzebit1000	movlw	b'00000001'		
		movwf	haltezaehler1
		goto	haltezaehlerermittlung1
rotierelinkshaltezaehler1	bcf	status, c		;rotieren nach rechts
		rlf 	haltezaehler1, 1		
		goto	haltezaehlerermittlung1
	
nichtgedrehthaltezaehler1	btfsc	taster
		goto	naechsteschrittweitehaltezaehl1	;nein, nicht gedrückt
		page0
		call	wait_8ms	
		page1
		btfsc	taster
		goto	naechsteschrittweitehaltezaehl1
		
;haltezaehlerwart1	btfss	taster		;ja, warte bis der taster wieder losgelassen wirdel
;		goto	$-1
;		page0
;		call	wait_8ms
;		page1
;		btfss	taster
;			;ja, warte bis der taster wieder losgelassen wird
;		goto	haltezaehlerwart1
	
;___________________ENDE Endpause ermitteln
		
;------------------------------------Gehe auf Seite 3		
		page2
		goto	startstopwobbeln1
		page1
		
;----------------------------------------Hierendet alles, die ganze wobbelitis		
		return
;===========================================
;===========================================
;======================PAGE 2 des Speichers

		org	0x1000
;------------------------------------Einmal oder mehrmals Wobbeln;Ergebnis in Register oftwobbeln 
startstopwobbeln1	page3
		call	mehrfachwobbelnlcd
		page2
				
		btfss	taster		;ja, warte bis der taster wieder losgelassen wirdel
		goto	$-1
		
		
wobbelermittlung	btfss	oftwobbeln, 0
		goto	mehrfachwobbeln
		goto	einmalwobbeln
		

mehrfachwobbeln	page3
		call	mehrfachwobbelnlcd
		page2
		
		goto 	naechstesoftwobbeln

einmalwobbeln	page3
		call	einmalwobbelnlcd
		page2	
				
naechstesoftwobbeln	
		page0
		call	dreh
		page2
		btfss	rauf_runter, 1	;wurde gedreht?
		goto	nichtgedrehtoftwobbeln	;es wurde nicht gedreht 
		movlw	b'00000011'		;es wurde gedreht
		subwf	rauf_runter, 0	;wurde nach oben gedreht?
		btfss	status, z		
		goto	nachuntenwobbelermitt		;nach unten
		goto	nachobenwobbelermitt	;nach oben


nachuntenwobbelermitt	btfsc	oftwobbeln, 0	; 	
		goto	loeschebitwobb		;bit 0 ist gesetzt
		goto	setzebitoftwobb		;bit 0 ist gelöscht
setzebitoftwobb	bsf	oftwobbeln, 0	;bit 0 war gelöscht, dann setzen wir es
		goto	wobbelermittlung
loeschebitwobb	bcf	oftwobbeln, 0	;bit 0 war gesetzt, dann löschen wird
		goto	wobbelermittlung




nachobenwobbelermitt	btfsc	oftwobbeln, 0	; 	
		goto	loeschebitwobb1		;bit 0 ist gesetzt
		goto	setzebitoftwobb1
setzebitoftwobb1	bsf	oftwobbeln, 0	;bit 0 war gelöscht, dann setzen wir es
		goto	wobbelermittlung
loeschebitwobb1	bcf	oftwobbeln, 0	;bit 0 war gesetzt, dann löschen wird
		goto	wobbelermittlung
	
nichtgedrehtoftwobbeln	btfsc	taster
		goto	naechstesoftwobbeln	;nein, nicht gedrückt
		page0
		call	wait_8ms	
		page2
		btfsc	taster
		goto	naechstesoftwobbeln
;------------------------------Taster gedrückt, jetzt wird gerechnet bis dass der rauch aufgeht
startpruefen		page3		;ausgabe "Start?"
		call	startlcd
		page2 	
		
		btfss	taster	;warte bis der Taster wieder losgelassen wurde
		goto	$-1		
		
keinstart		btfsc	taster	;schau, ob der Taster gedrückt ist: Startsignal
		goto	keinstart	;nein, nicht gedrückt
		page0
		call	wait_8ms	
		page2
		btfsc	taster
		goto	keinstart
		
		page3
		call	wiwawobbellcd	;Ausgabe dass ich wobble, zweite zeile Stopp
		page2
		btfss	taster	;warte bis der Taster wieder losgelassen wurde und dann fang mit wobbeln endgültig an
		goto	$-1
		


		;--------------hole Startfrequnez
_160mhzueberschr	bsf	trigger	;schalte einen port auf high, trigger signal
		movf	tempstart_100MHZ, 0
		movwf	_100MHZ
		movf	tempstart_10MHZ, 0
		movwf	_10MHZ
		movf	tempstart_1MHZ, 0
		movwf	_1MHZ
		movf	tempstart_100KHZ, 0
		movwf	_100KHZ
		movf	tempstart_10KHZ, 0
		movwf	_10KHZ
		movf	tempstart_1KHZ, 0
		movwf	_1KHZ
		movf	tempstart_100HZ, 0
		movwf	_100HZ
		movf	tempstart_10HZ, 0
		movwf	_10HZ
		movf	tempstart_1HZ, 0
		movwf	_1HZ
		bcf	trigger	;und trigger wieder aus
		;gib Startfrequenz an DDS aus
wierwobbelnweiter	page0
		call	dds_update
		page2
		
		;------------------schau ob wird zum wobbeln aufhören sollen
		btfss	taster
		goto	sofortaufhoeren	;Taster gedrückt
		;Hole Warteheit 		;Taster nicht gedrückt
		movlw	b'00000001'
		subwf	haltezaehler, 0
		btfss	status, z 
		goto	$+2
		goto	zeit5ms
		movlw	b'00000010'
		subwf	haltezaehler, 0
		btfss	status, z 
		goto	$+2
		goto	zeit10ms
		movlw	b'00000100'
		subwf	haltezaehler, 0
		btfss	status, z 
		goto	$+2
		goto	zeit20ms
		movlw	b'00001000'
		subwf	haltezaehler, 0
		btfss	status, z 
		goto	$+2
		goto	zeit50ms
		movlw	b'00010000'
		subwf	haltezaehler, 0
		btfss	status, z 
		goto	$+2
		goto	zeit100ms
		movlw	b'00100000'
		subwf	haltezaehler, 0
		btfss	status, z 
		goto	$+2
		goto	zeit200ms
		movlw	b'01000000'
		subwf	haltezaehler, 0
		btfss	status, z 
		goto	$+2
		goto	zeit500ms
		goto	zeit1000ms	
			
zeit5ms		call	pause5ms
		goto	weitermachenendpause
zeit10ms		call	pause10ms
		goto	weitermachenendpause
zeit20ms		call	pause20ms
		goto	weitermachenendpause
zeit50ms		call	pause50ms
		goto	weitermachenendpause
zeit100ms		call	pause100ms
		goto	weitermachenendpause
zeit200ms		call	pause200ms
		goto	weitermachenendpause
zeit500ms		call	pause500ms
		goto	weitermachenendpause
zeit1000ms		call	pause1000ms
		goto	weitermachenendpause
		;------------------Jetzt haben wir die qrg so lange stehen lassen
		;-----------------Schau ob wir aufhören sollen
weitermachenendpause	btfss	taster
		goto	sofortaufhoeren	;Taster gedrückt	
		;---------Hole Schrittweite und erhöhe qrg; Taster nicht gedrückt	
		btfss	schrittzaehler, 0	;1 Hz Schritte? 
		goto	testauf10hz		;nein
		goto	qrgplus_1HZ	;ja
			
testauf10hz		btfss	schrittzaehler, 1	;10 Hz Schritte?
		goto	testauf100hz	;nein
		goto	qrgplus_10HZ1000	;ja
		
testauf100hz	btfss	schrittzaehler, 2	;100 Hz Schritte?
		goto	testauf1khz		;nein
		goto	qrgplus_100HZ1000	;ja
		
testauf1khz		btfss	schrittzaehler, 3	;1 kHz Schritte?
		goto	testauf10khz	;nein
		goto	qrgplus_1KHZ1000	;ja
		
testauf10khz	btfss	schrittzaehler, 4	;10 kHz Schritte?
		goto	testauf100khz	;nein
		goto	qrgplus_10KHZ1000	;ja
		
testauf100khz	btfss	schrittzaehler, 5	;100 kHz Schritte?
		goto	testauf1mhz		;nein
		goto	qrgplus_100KHZ1000	;ja
		
testauf1mhz		btfss	schrittzaehler, 6	;1 MHz Schritte?
		goto	testauf10mhz	;nein
		goto	qrgplus_1MHZ1000	;ja
		
testauf10mhz	goto	qrgplus_10MHZ1000	;dann könnens ja blos noch 10MHZ Schritte sein
		
	
	;---------------neue Frequenz ist berechnet, jetzt muss getestet werden ob die obere Endfrequenz noch nicht überschritten ist	
	;--------------Erhöhen der Frequenz im BCD Format
;--------------getestet,geht
qrgplus_1HZ1000	incf	_1HZ, 1
		movlw	d'10'
		subwf	_1HZ, 0
		btfss	status, c
		goto	qrttest1000
		clrf	_1HZ
		goto	$+1	
qrgplus_10HZ1000	incf	_10HZ, 1
		movlw	d'10'
		subwf	_10HZ, 0
		btfss	status, c
		goto	qrttest1000
		clrf	_10HZ
		goto	$+1
qrgplus_100HZ1000	incf	_100HZ, 1
		movlw	d'10'
		subwf	_100HZ, 0
		btfss	status, c
		goto	qrttest1000
		clrf	_100HZ
		goto	$+1
qrgplus_1KHZ1000	incf	_1KHZ, 1
		movlw	d'10'
		subwf	_1KHZ, 0
		btfss	status, c
		goto	qrttest1000
		clrf	_1KHZ
		goto	$+1
qrgplus_10KHZ1000	incf	_10KHZ, 1
		movlw	d'10'
		subwf	_10KHZ, 0
		btfss	status, c
		goto	qrttest1000
		clrf	_10KHZ
		goto	$+1
qrgplus_100KHZ1000	incf	_100KHZ, 1
		movlw	d'10'
		subwf	_100KHZ, 0
		btfss	status, c
		goto	qrttest1000
		clrf	_100KHZ
		goto	$+1
qrgplus_1MHZ1000	incf	_1MHZ, 1
		movlw	d'10'
		subwf	_1MHZ, 0
		btfss	status, c
		goto	qrttest1000
		clrf	_1MHZ
		goto	$+1
qrgplus_10MHZ1000	incf	_10MHZ, 1
		movlw	d'10'
		subwf	_10MHZ, 0
		btfss	status, c
		goto	qrttest1000
		clrf	_10MHZ
		goto	$+1
		incf	_100MHZ, 1
		
qrttest1000		movlw	d'0'		;test ob eine frequenz größer 160000000HZ eingestellt ist
		subwf	_100MHZ, 0		;wenn ja, dann gib bei dauerwobbeln startfrequenz aus und bei einmal wobeln goto 
		btfss	status, z		;sofortaufhoeren über set1000
		goto	$+2
		goto	qrt1000
		
		movlw	d'1'
		subwf	_100MHZ, 0
		btfss	status, z
		goto	qrt1000
			
		movlw	d'6'
		subwf	_10MHZ, 0
		btfss	status, c
		goto	qrt1000
		
		movlw	d'6'
		subwf	_10MHZ, 0
		btfss	status, z
		goto	set1000
						
		movlw	d'0'
		subwf	_1MHZ, 0
		btfss	status, z
		goto	set1000
		
		movlw	d'0'
		subwf	_100KHZ, 0
		btfss	status, z
		goto	set1000
		
		movlw	d'0'
		subwf	_10KHZ, 0
		btfss	status, z
		goto	set1000
		
		movlw	d'0'
		subwf	_1KHZ, 0
		btfss	status, z
		goto	set1000
		
		movlw	d'0'
		subwf	_100HZ, 0
		btfss	status, z
		goto	set1000
		
		movlw	d'0'
		subwf	_10HZ, 0
		btfss	status, z
		goto	set1000
		
		movlw	d'0'					
		subwf	_1HZ, 0		
		btfss	status, z
		goto	set1000
		goto	qrt1000				
		
set1000		;qrg größer 160000000HZ d.h. zurück mit Startfreuqeuz bei mehrfach wobbeln
		;--------------
		btfss	oftwobbeln, 0	;bei einmal wobbeln sofortaufhoeren 
		goto	endpauseholen	
		goto	sofortaufhoeren

				
qrt1000		;prüfen ob obere Endfrequenz überschritten wird, wenn nicht :prüfen ob einmal oder mehrfach wobbeln
		;dann je nachdem zur endpauseholen		
		movf	up_100MHZ, 0
		subwf	_100MHZ, 0
		btfss	status ,0
		goto	passtnoch	;kein überlauf
		btfss	status, 2
		goto	passtnichtmehr
		movf	up_10MHZ, 0
		subwf	_10MHZ, 0
		btfss	status ,0
		goto	passtnoch	;kein überlauf
		btfss	status, 2
		goto	passtnichtmehr
		movf	up_1MHZ, 0
		subwf	_1MHZ, 0
		btfss	status ,0
		goto	passtnoch	;kein überlauf
		btfss	status, 2
		goto	passtnichtmehr
		movf	up_100KHZ, 0
		subwf	_100KHZ, 0
		btfss	status ,0
		goto	passtnoch	;kein überlauf
		btfss	status, 2
		goto	passtnichtmehr
		movf	up_10KHZ, 0
		subwf	_10KHZ, 0
		btfss	status ,0
		goto	passtnoch	;kein überlauf
		btfss	status, 2
		goto	passtnichtmehr
		movf	up_1KHZ, 0
		subwf	_1KHZ, 0
		btfss	status ,0
		goto	passtnoch	;kein überlauf
		btfss	status, 2
		goto	passtnichtmehr
		movf	up_100HZ, 0
		subwf	_100HZ, 0
		btfss	status ,0
		goto	passtnoch	;kein überlauf
		btfss	status, 2
		goto	passtnichtmehr
		movf	up_10HZ, 0
		subwf	_10HZ, 0
		btfss	status ,0
		goto	passtnoch	;kein überlauf
		btfss	status, 2
		goto	passtnichtmehr
		movf	up_1HZ, 0
		subwf	_1HZ, 0
		btfss	status ,0
		goto	passtnoch	;kein überlauf
		btfss	status, 2
		goto	passtnicht
		goto	passtnoch
		
	
;------------------------------Obere Wobbelfrequenz ist noch nicht überschritten, schau, ob wir aufhören wollen, wenn nein :weiter mit wobbeln
passtnoch		btfss	taster
		goto	sofortaufhoeren	;taster ist gedrückt
		goto	wierwobbelnweiter	;wir wobbeln weiter	

;----------------------Obere Wobbelfrequenz ist überschritten 
passtnichtmehr	btfss	oftwobbeln, 0
		goto	endpauseholen	;wir wobbeln weiter	
		goto	sofortaufhoeren	;nur einmal wobbeln
		
		
		
	;------------------holen der endpause , wenn höchste Frewquenz erreicht ist	
endpauseholen	;------------------schau ob wird zum wobbeln aufhören sollen
		btfss	taster
		goto	sofortaufhoeren	;Taster gedrückt
		;Hole Warteheit 		;Taster nicht gedrückt
		movlw	b'00000001'
		subwf	haltezaehler1, 0
		btfss	status, z 
		goto	$+2
		goto	zeit5ms1
		movlw	b'00000010'
		subwf	haltezaehler1, 0
		btfss	status, z 
		goto	$+2
		goto	zeit10ms1
		movlw	b'00000100'
		subwf	haltezaehler1, 0
		btfss	status, z 
		goto	$+2
		goto	zeit20ms1
		movlw	b'00001000'
		subwf	haltezaehler1, 0
		btfss	status, z 
		goto	$+2
		goto	zeit50ms1
		movlw	b'00010000'
		subwf	haltezaehler1, 0
		btfss	status, z 
		goto	$+2
		goto	zeit100ms1
		movlw	b'00100000'
		subwf	haltezaehler1, 0
		btfss	status, z 
		goto	$+2
		goto	zeit200ms1
		movlw	b'01000000'
		subwf	haltezaehler1, 0
		btfss	status, z 
		goto	$+2
		goto	zeit500ms1
		goto	zeit1000ms1	
			
zeit5ms1		goto	_160mhzueberschr	;keine endpause, sofort weitermachen mit wobbeln mit der Wobbelstartfrequenz
zeit10ms1		call	_0Hzraus		;Gib 0 Hz aus
		call	pause10ms
		goto	weitermachenendpause1
zeit20ms1		call	_0Hzraus
		call	pause20ms
		goto	weitermachenendpause1
zeit50ms1		call	_0Hzraus
		call	pause50ms
		goto	weitermachenendpause1
zeit100ms1		call	_0Hzraus
		call	pause100ms
		goto	weitermachenendpause1
zeit200ms1		call	_0Hzraus
		call	pause200ms
		goto	weitermachenendpause1
zeit500ms1		call	_0Hzraus
		call	pause500ms
		goto	weitermachenendpause1
zeit1000ms1	call	_0Hzraus
		call	pause1000ms
		goto	weitermachenendpause1
		;------------------Jetzt haben wir die qrg so lange stehen lassen
		;-----------------Schau ob wir aufhören sollen
weitermachenendpause1	goto	_160mhzueberschr


;-----------------------------da kommen wir her, wenn entweder der taster gedrückt war oder in oftwobbeln, 0: 1 (nur einmal wobbeln) stand
;---------------jetzt wird gefragt, ob noch einmal gewobbelt werden soll oder nicht, Auswahl über Drehgeber	
;------------------------------------Nochmal wobbeln: ja, nein, weiss nicht , vielleicht, ist mir wurscht 
sofortaufhoeren	page3
		call	nocheinmal
		page2
		btfss	taster
		goto	$-1	;warte bis der Taster wieder losgelassen wurde


auswahl		btfsc	taster	;taster drücken für auswahlmenu
		goto	$-1	;nein, nicht gedrückt
		page0
		call	wait_8ms	
		page2
		btfsc	taster
		goto	auswahl
		
		btfss	taster	;warte bis der taster wieder losgelassen wurde
		goto	$-1
		


nocheinswobbeln	movlw	b'00000001'
		subwf	nochmalzaehler, 0
		btfss	status, z 
		goto	$+2
		goto	neinwobbeln
		movlw	b'00000010'
		subwf	nochmalzaehler, 0
		btfss	status, z 
		goto	$+2
		goto	jawobbelnalt
		goto	jawobbelnneu
neinwobbeln		movlw	b'00000001'
		page0
		call	cmnd2LCD
		page2
		movlw	b'10000000'
		page0
		call	cmnd2LCD
		page2
		movlw	'N'
		page0
		call	data2LCD
		page2
		movlw	'e' 
		page0
		call	data2LCD
		page2
		movlw	'i'
		page0
		call	data2LCD
		page2
		movlw	'n'
		page0
		call	data2LCD
		page2
		goto 	naechstesprobieren

jawobbelnalt		movlw	b'00000001'
		page0
		call	cmnd2LCD
		page2
		movlw	b'10000000'
		page0
		call	cmnd2LCD
		page2
		movlw	'J'
		page0
		call	data2LCD
		page2
		movlw	'a'
		page0
		call	data2LCD
		page2
		movlw	','
		page0
		call	data2LCD
		page2
		movlw	'm'
		page0
		call	data2LCD
		page2
		movlw	'i'
		page0
		call	data2LCD
		page2
		movlw	't'
		page0
		call	data2LCD
		page2
		movlw	' '
		page0
		call	data2LCD
		page2
		movlw	'a'
		page0
		call	data2LCD
		page2
		movlw	'l'
		page0
		call	data2LCD
		page2
		movlw	't'
		page0
		call	data2LCD
		page2
		movlw	'e'
		page0
		call	data2LCD
		page2
		movlw	'n'
		page0
		call	data2LCD
		page2
		goto 	einstellungenlcd

jawobbelnneu	movlw	b'00000001'
		page0
		call	cmnd2LCD
		page2
		movlw	b'10000000'
		page0
		call	cmnd2LCD
		page2
		movlw	'J'
		page0
		call	data2LCD
		page2
		movlw	'a'
		page0
		call	data2LCD
		page2
		movlw	','
		page0
		call	data2LCD
		page2
		movlw	'm'
		page0
		call	data2LCD
		page2
		movlw	'i'
		page0
		call	data2LCD
		page2
		movlw	't'
		page0
		call	data2LCD
		page2
		movlw	' '
		page0
		call	data2LCD
		page2
		movlw	'n'
		page0
		call	data2LCD
		page2
		movlw	'e'
		page0
		call	data2LCD
		page2
		movlw	'u'
		page0
		call	data2LCD
		page2
		movlw	'e'
		page0
		call	data2LCD
		page2
		movlw	'n'
		page0
		call	data2LCD
		page2
		goto 	einstellungenlcd
		
		
einstellungenlcd	movlw	b'11000000'
		page0
		call	cmnd2LCD
		page2
		movlw	'E'
		page0
		call	data2LCD
		page2
		movlw	'i'
		page0
		call	data2LCD
		page2
		movlw	'n'
		page0
		call	data2LCD
		page2
		movlw	's'
		page0
		call	data2LCD
		page2
		movlw	't'
		page0
		call	data2LCD
		page2
		movlw	'e'
		page0
		call	data2LCD
		page2
		movlw	'l'
		page0
		call	data2LCD
		page2
		movlw	'l'
		page0
		call	data2LCD
		page2
		movlw	'u'
		page0
		call	data2LCD
		page2
		movlw	'n'
		page0
		call	data2LCD
		page2
		movlw	'g'
		page0
		call	data2LCD
		page2
		movlw	'e'
		page0
		call	data2LCD
		page2
		movlw	'n'
		page0
		call	data2LCD
		page2
		
		
naechstesprobieren	
		page0
		call	dreh
		page2
		btfss	rauf_runter, 1	;wurde gedreht?
		goto	nichtgedrehtaltneu	;es wurde nicht gedreht 
		movlw	b'00000011'		;es wurde gedreht
		subwf	rauf_runter, 0	;wurde nach oben gedreht?
		btfss	status, z		
		goto	nachuntenaltneu		;nach unten
		goto	nachobenaltneu	;nach oben
nachuntenaltneu	btfsc	nochmalzaehler, 0	; 	
		goto	setzebit700000	;bit 1 ist gesetzt
		goto	rotiererechtsaltneu
setzebit700000	movlw	b'00000100'		
		movwf	nochmalzaehler
		goto	nocheinswobbeln
rotiererechtsaltneu	bcf	status, c		;rotieren nach rechts
		rrf 	nochmalzaehler, 1		
		goto	nocheinswobbeln

nachobenaltneu	btfsc	nochmalzaehler, 2	; 	
		goto	setzebit100000	;bit 7 ist gesetzt
		goto	rotierelinksaltneu
setzebit100000	movlw	b'00000001'		
		movwf	nochmalzaehler
		goto	nocheinswobbeln
rotierelinksaltneu	bcf	status, c		;rotieren nach rechts
		rlf 	nochmalzaehler, 1		
		goto	nocheinswobbeln
	
nichtgedrehtaltneu	btfsc	taster
		goto	naechstesprobieren	;nein, nicht gedrückt
		page0
		call	wait_8ms	
		page2
		btfsc	taster
		goto	naechstesprobieren
;--------------------------------Taster ist gedrückt, jetzt schau was zu tune ist
		btfss	nochmalzaehler, 0
		goto	$+2
		goto	heim		;nicht mehr wobbeln
		btfss	nochmalzaehler, 1
		goto	$+2
		goto	startpruefen		;nochmal wobbeln mit alten werten
		page1			;nochmal wobbeln mit neuen werten nach page1
		goto	wobbelnauspage2
		
heim		;---------------alle entscheidungszaehler auf default 
		movlw	b'01000000'		;Schrittzähler setzen (Schrittweite)
		movwf	schrittzaehler
		
		movlw	b'00000001'		;Haltezeit beim Wobbeln setzen
		movwf	haltezaehler
	
		movlw	b'00000001'		;Wartezeit am Ende  beim Wobbeln setzen
		movwf	haltezaehler1
		
		movlw	b'00000000'		;Register, das geprüft wird, wenn man entscheidet ob einmal oder mehrfach gewobbelt wird
		movwf	oftwobbeln			
		
		movlw	b'00000001'		;Register in dem beim Ende des Wobbelns steht, ob nochmal gewobbelt werden soll oder nicht
		movwf	nochmalzaehler
		;zurückholen des Frequenzregisters
		movf	temp_100MHZ, 0
		movwf	_100MHZ
		movf	temp_10MHZ, 0
		movwf	_10MHZ
		movf	temp_1MHZ, 0
		movwf	_1MHZ
		movf	temp_100KHZ, 0
		movwf	_100KHZ
		movf	temp_10KHZ, 0
		movwf	_10KHZ
		movf	temp_1KHZ, 0
		movwf	_1KHZ
		movf	temp_100HZ, 0
		movwf	_100HZ
		movf	temp_10HZ, 0
		movwf	_10HZ
		movf	temp_1HZ, 0
		movwf	_1HZ
		page0
		call	dds_update
		page2
		movlw	b'00000001'
		page0
		call	cmnd2LCD
		page2
		page0
		call	LCD
		page2
		btfss	taster
		goto	$-1		;taster ist noch gedrückt 
		return
		
		





;===============================================================
;-----------------------Subroutinen in Page 2
;========Gib 0 Hz aus an DDS
_0Hzraus		movlw	0x00
		movwf	_100MHZ
		movwf	_10MHZ
		movwf	_1MHZ
		movwf	_100KHZ
		movwf	_10KHZ
		movwf	_1KHZ
		movwf	_100HZ
		movwf	_10HZ
		movwf	_1HZ
		page0
		call	dds_update
		page2
		return
;----------------ENDE Gib 0 Hz aus
;------------------Pausen in page 2: Pausen müssen immer 5mS kürzer sein als gewollt, da der DDS update ca. 5MS dauert
pause5ms		return

pause10ms		page0
		call	wait_10ms
		page2
		return
pause20ms		page0
		call	wait_20ms
		page2
		return
pause50ms		page0
		call	wait_50ms
		page2
		return
pause100ms	page0
		call	wait_100ms
		page2
		return
pause200ms	page0
		call	wait_100ms
		page2
		page0
		call	wait_100ms
		page2
		return
pause500ms	page0
		call	wait_100ms
		page2
		page0
		call	wait_100ms
		page2
		page0
		call	wait_100ms
		page2
		page0
		call	wait_100ms
		page2
		page0
		call	wait_100ms
		page2
		return
pause1000ms	page0	
		call	wait_100ms
		page2
		page0
		call	wait_100ms
		page2
		page0
		call	wait_100ms
		page2
		page0
		call	wait_100ms
		page2
		page0
		call	wait_100ms
		page2
		page0
		call	wait_100ms
		page2
		page0
		call	wait_100ms
		page2
		page0
		call	wait_100ms
		page2
		page0
		call	wait_100ms
		page2
		page0
		call	wait_100ms
		page2
		return
;----------------------Ende Pausen		


;===============================================
;=================================================
;==================PAGE 3 des Speichers
		org 0x1800

;---------------Frage ob nochmal gewobbelt werden soll Ausgabe ab LCD
nocheinmal		movlw	b'00000001'
		page0
		call	cmnd2LCD
		movlw	b'10000000'		;gib als erstes mal die aktuelle schrittweite aus		
		page0
		call	cmnd2LCD
		movlw	'N'
		page0
		call	data2LCD
		movlw	'o'
		page0
		call	data2LCD
		movlw	'c'
		page0
		call	data2LCD
		movlw	'h'
		page0
		call	data2LCD
		movlw	'm'
		page0
		call	data2LCD
		movlw	'a'
		page0
		call	data2LCD
		movlw	'l'
		page0
		call	data2LCD
		movlw	' '
		page0
		call	data2LCD
		movlw	'w'
		page0
		call	data2LCD
		movlw	'o'
		page0
		call	data2LCD
		movlw	'b'
		page0
		call	data2LCD
		movlw	'b'
		page0
		call	data2LCD
		movlw	'e'
		page0
		call	data2LCD
		movlw	'l'
		page0
		call	data2LCD
		movlw	'n'
		page0
		call	data2LCD
		movlw	'?'
		page0
		call	data2LCD
		return
;---------------ENDE Frage ob nochmal gewobbelt werden soll Ausgabe ab LCD

;----------------------Ausgabe des Begriffs "Start?" beim Wobbeln
startlcd		movlw	b'00000001'
		page0
		call	cmnd2LCD
		movlw	b'10000001'		;gib als erstes mal die aktuelle schrittweite aus		
		page0
		call	cmnd2LCD
		movlw	'S'
		page0
		call	data2LCD
		movlw	't'
		page0
		call	data2LCD
		movlw	'a'
		page0
		call	data2LCD
		movlw	'r'
		page0
		call	data2LCD
		movlw	't'
		page0
		call	data2LCD
		movlw	'?'
		page0
		call	data2LCD
		return
;--------------------------ENDE Begriff "Start?"	

;-------------------------Ausgabe dass gewobbelt wird und Frage nach Stop?
wiwawobbellcd	movlw	b'00000001'
		page0
		call	cmnd2LCD
		movlw	b'10000000'		;1. Zeile		
		page0
		call	cmnd2LCD
		movlw	'I'
		page0
		call	data2LCD
		movlw	'c'
		page0
		call	data2LCD
		movlw	'h'
		page0
		call	data2LCD
		movlw	' '
		page0
		call	data2LCD
		movlw	'w'
		page0
		call	data2LCD
		movlw	'i'
		page0
		call	data2LCD
		movlw	'-'
		page0
		call	data2LCD
		movlw	'w'
		page0
		call	data2LCD
		movlw	'a'
		page0
		call	data2LCD
		movlw	'-'
		page0
		call	data2LCD
		movlw	'w'
		page0
		call	data2LCD
		movlw	'o'
		page0
		call	data2LCD
		movlw	'b'
		page0
		call	data2LCD
		movlw	'b'
		page0
		call	data2LCD
		movlw	'l'
		page0
		call	data2LCD
		movlw	'e'
		page0
		call	data2LCD
		movlw	b'11000000'		;2. Zeile		
		page0
		call	cmnd2LCD
		movlw	'S'
		page0
		call	data2LCD
		movlw	't'
		page0
		call	data2LCD
		movlw	'o'
		page0
		call	data2LCD
		movlw	'p'
		page0
		call	data2LCD
		movlw	'p'
		page0
		call	data2LCD
		movlw	'?'
		page0
		call	data2LCD
		return


;-------------------------ENDE Ausgabe dass gewobbelt wird und Frage nach Stop?
;------------------------Ausgabe Schritteeinstellen an LCD
schritteeinstellenlcd	movlw	b'00000001'
		page0
		call	cmnd2LCD
		movlw	b'10000001'		;gib als erstes mal die aktuelle schrittweite aus		
		page0
		call	cmnd2LCD
		movlw	'A'
		page0
		call	data2LCD
		movlw	'b'
		page0
		call	data2LCD
		movlw	's'
		page0
		call	data2LCD
		movlw	't'
		page0
		call	data2LCD
		movlw	'i'
		page0
		call	data2LCD
		movlw	'm'
		page0
		call	data2LCD
		movlw	'm'
		page0
		call	data2LCD
		movlw	's'
		page0
		call	data2LCD
		movlw	'c'
		page0
		call	data2LCD
		movlw	'h'
		page0
		call	data2LCD
		movlw	'r'
		page0
		call	data2LCD
		movlw	'i'
		page0
		call	data2LCD
		movlw	't'
		page0
		call	data2LCD
		movlw	't'
		page0
		call	data2LCD
		movlw	'e'
		page0
		call	data2LCD
		return

;-----------------Ausgabe mehrfach wobbeln
mehrfachwobbelnlcd	movlw	b'00000001'
		page0
		call	cmnd2LCD
		movlw	b'10000001'
		page0
		call	cmnd2LCD
		movlw	'D'
		page0
		call	data2LCD
		movlw	'a'
		page0
		call	data2LCD
		movlw	'u'
		page0
		call	data2LCD
		movlw	'e'
		page0
		call	data2LCD
		movlw	'r'
		page0
		call	data2LCD
		movlw	'n'
		page0
		call	data2LCD
		movlw	'd'
		page0
		call	data2LCD
		movlw	' '
		page0
		call	data2LCD
		movlw	'W'
		page0
		call	data2LCD
		movlw	'o'
		page0
		call	data2LCD
		movlw	'b'
		page0
		call	data2LCD
		movlw	'b'
		page0
		call	data2LCD
		movlw	'e'
		page0
		call	data2LCD
		movlw	'l'
		page0
		call	data2LCD
		movlw	'n'
		page0
		call	data2LCD
		return
;-------------ENDE Ausgabe mehrfach wobbeln

;---------------------Ausgabe einmal wobbeln
einmalwobbelnlcd	movlw	b'00000001'
		page0
		call	cmnd2LCD
		movlw	b'10000001'
		page0
		call	cmnd2LCD
		movlw	'E'
		call	data2LCD
		movlw	'i'
		page0
		call	data2LCD
		movlw	'n'
		page0
		call	data2LCD
		movlw	'm'
		page0
		call	data2LCD
		movlw	'a'
		page0
		call	data2LCD
		movlw	'l'
		page0
		call	data2LCD
		movlw	' '
		page0
		call	data2LCD
		movlw	'W'
		page0
		call	data2LCD
		movlw	'o'
		page0
		call	data2LCD
		movlw	'b'
		page0
		call	data2LCD
		movlw	'b'
		page0
		call	data2LCD
		movlw	'e'
		page0
		call	data2LCD
		movlw	'l'
		page0
		call	data2LCD
		movlw	'n'
		page0
		call	data2LCD
		return
;----------------------ENDE Ausgabe einmal wobbeln		
		
		
;------------------------Ausgabe Begriff Startfrequenz
startfrequenz	movlw	b'00000001'
		page0
		call	cmnd2LCD
		movlw	b'11000001'
		page0
		call	cmnd2LCD
		movlw	'S'
		page0
		call	data2LCD
		movlw	't'
		page0
		call	data2LCD
		movlw	'a'
		page0
		call	data2LCD
		movlw	'r'
		page0
		call	data2LCD
		movlw	't'
		page0
		call	data2LCD
		movlw	'f'
		page0
		call	data2LCD
		movlw	'r'
		page0
		call	data2LCD
		movlw	'e'
		page0
		call	data2LCD
		movlw	'q'
		page0
		call	data2LCD
		movlw	'u'
		page0
		call	data2LCD
		movlw	'e'
		page0
		call	data2LCD
		movlw	'n'
		page0
		call	data2LCD
		movlw	'z'
		page0
		call	data2LCD
		page0
		call	LCD
		return
;___________ENDE Bergiff Startfrequenz		
;--------------------------Startfrequenz aus eeprom auslesen
startqrg_eeprom_lies	movlw	0x04
		movwf	adresse
		call	eepromread
		movwf	_100MHZ
		
		movlw	0x05
		movwf	adresse
		call	eepromread
		movwf	_10MHZ
		
		movlw	0x06
		movwf	adresse
		call	eepromread
		movwf	_1MHZ
		
		movlw	0x07
		movwf	adresse
		call	eepromread
		movwf	_100KHZ
		
		movlw	0x08
		movwf	adresse
		call	eepromread
		movwf	_10KHZ
		
		movlw	0x09
		movwf	adresse
		call	eepromread
		movwf	_1KHZ
				
		movlw	0x0a
		movwf	adresse
		call	eepromread
		movwf	_100HZ
		
		movlw	0x0b
		movwf	adresse
		call	eepromread
		movwf	_10HZ
		
		movlw	0x0c
		movwf	adresse
		call	eepromread
		movwf	_1HZ
		return		
;--------------------------ENDE Startfrequenz aus eeprom auslesen

;------------------------Ausgabe Begriff Endfrequenz
endfrequenz		movlw	b'00000001'
		page0
		call	cmnd2LCD
		movlw	b'11000001'
		page0
		call	cmnd2LCD
		movlw	'S'
		page0
		call	data2LCD
		movlw	't'
		page0
		call	data2LCD
		movlw	'o'
		page0
		call	data2LCD
		movlw	'p'
		page0
		call	data2LCD
		movlw	'p'
		page0
		call	data2LCD
		movlw	'f'
		page0
		call	data2LCD
		movlw	'r'
		page0
		call	data2LCD
		movlw	'e'
		page0
		call	data2LCD
		movlw	'q'
		page0
		call	data2LCD
		movlw	'u'
		page0
		call	data2LCD
		movlw	'e'
		page0
		call	data2LCD
		movlw	'n'
		page0
		call	data2LCD
		movlw	'z'
		page0
		call	data2LCD
		page0
		call	LCD
		return
;___________ENDE Bergiff Endfrequenz		


;--------------------------Endfrequenz aus eeprom auslesen
endqrg_eeprom_lies	movlw	0x0d
		movwf	adresse
		call	eepromread
		movwf	_100MHZ
		
		movlw	0x0e
		movwf	adresse
		call	eepromread
		movwf	_10MHZ
		
		movlw	0x0f
		movwf	adresse
		call	eepromread
		movwf	_1MHZ
		
		movlw	0x10
		movwf	adresse
		call	eepromread
		movwf	_100KHZ
		
		movlw	0x11
		movwf	adresse
		call	eepromread
		movwf	_10KHZ
		
		movlw	0x12
		movwf	adresse
		call	eepromread
		movwf	_1KHZ
				
		movlw	0x13
		movwf	adresse
		call	eepromread
		movwf	_100HZ
		
		movlw	0x14
		movwf	adresse
		call	eepromread
		movwf	_10HZ
		
		movlw	0x15
		movwf	adresse
		call	eepromread
		movwf	_1HZ
		return		
;--------------------------ENDE Endfrequenz aus eeprom auslesen


;--------------------------ref_clk Werte aus eeprom auslesen
refclk_eeprom_lies	movlw	0x00
		movwf	adresse
		call	eepromread
		movwf	ref_clk3
		
		movlw	0x01
		movwf	adresse
		call	eepromread
		movwf	ref_clk2
		
		movlw	0x02
		movwf	adresse
		call	eepromread
		movwf	ref_clk1
		
		movlw	0x03
		movwf	adresse
		call	eepromread
		movwf	ref_clk0
		
		return	
;--------------------------ENDE ref_clk Werte aus eeprom auslesen
		
;--------------------------EEPROM auslesen
eepromread		movf	adresse, 0
		bsf	status, rp1
		bcf	status, rp0
		movwf	eeadr
		bsf	status, rp0
		bcf	eecon1, eepgd		
		bsf	eecon1, rd
		bcf	status, rp0
		movf	eedata, 0
		bcf	status, rp0
		bcf	status, rp1
		return
;------------------Ende EEPROM auslesen

;--------------------------------ref_clk werte ins eeprom schreiben
refclk_eeprom_schreib	bsf	status, rp1
		bcf	status, rp0	;bank2
		movlw	0x00
		movwf	eeadr
		bcf	status, rp0
		bcf	status, rp1	;bank0
		movf	ref_clk3, 0
		bsf	status, rp1
		bcf	status, rp0	;bank2
		movwf	eedata
		call	eepromwrite

		bsf	status, rp1
		bcf	status, rp0	;bank2
		movlw	0x01
		movwf	eeadr
		bcf	status, rp0
		bcf	status, rp1	;bank0
		movf	ref_clk2, 0
		bsf	status, rp1
		bcf	status, rp0	;bank2
		movwf	eedata
		call	eepromwrite
		
		bsf	status, rp1
		bcf	status, rp0	;bank2
		movlw	0x02
		movwf	eeadr
		bcf	status, rp0
		bcf	status, rp1	;bank0
		movf	ref_clk1, 0
		bsf	status, rp1
		bcf	status, rp0	;bank2
		movwf	eedata
		call	eepromwrite
		
		bsf	status, rp1
		bcf	status, rp0	;bank2
		movlw	0x03
		movwf	eeadr
		bcf	status, rp0
		bcf	status, rp1	;bank0
		movf	ref_clk0, 0
		bsf	status, rp1
		bcf	status, rp0	;bank2
		movwf	eedata
		call	eepromwrite
		bcf	status, rp1
		bcf	status, rp0	;bank0
		return
;-------------------------------- ENDE ref_clk werte ins eeprom schreiben

;--------------------------EEPROM beschreiben
eepromwrite		bsf	status, rp0
		bsf	status, rp1	;bank3
		bcf	eecon1, eepgd
		bsf	eecon1, wren

		movlw	0x55
		movwf	eecon2
		movlw	0xaa
		movwf	eecon2
		bsf	eecon1, wr
		
		btfsc	eecon1, wr
		goto	$-1
		bcf	eecon1, wren
		return
;-------------------ENDE EEPROM beschreiben
		







		
		end
