; 13LOV7.01-629
; 
; PIC power up programmer for ADF4113 using 12F629
;
; John Cooke GM8OTI
; 30May11
;
;
; 16F629 configuration register
;
; bits 13-12 - bandgap voltage (10)
;
; bit 11 - unimplemented (0)
; bit 10 - unimplemented (0)
; bit 9 - unimplemented (0)
; bit 8 - data code protection OFF (1)
;
; bit 7 - program code protection OFF (1)
; bit 6 - brown out reset enable ON (1)
; bit 5 - GP3 pin function is I/O (0)
; bit 4 - power up timer enable OFF (1)
;
; bit 3 - watchdog timer enable OFF (0)
; bits 2-0 - internal oscillator, I/O on GP4,GP5 (100)
;
	__config	h'21D4'
;
;
; instruction set constants
w		equ	0
f		equ	1
;
; register symbols
; status register
status		equ 	0x03	; status register address (bank 0)
irp		equ	7	; reserved (set to 0)
rp1		equ	6	; reserved (set to 0)
rp0     	equ 	5	; register bank select (direct addressing)
to		equ	4	; time-out bit
pd		equ	3	; power down bit
z		equ 	2	; zero bit
dc		equ	1	; hex digit carry (for digit arithmetic)
c		equ 	0	; carry bit
; io registers
gpio		equ	0x05	; GPIO port address
trisio		equ	0x85	; tri-state data direction register
; bit allocation in gpio:
;    GP0 - ICSPDAT
;    GP1 - ICSPCLK
;    GP2 - ADF8113 LE
;    GP3 - ICSPVpp
;    GP4 - ADF8113 DATA
;    GP5 - ADF8113 CLK
; comparator register
cmcon		equ	0x19	; comparator register (power up has comparators enabled)
;
;
; constants
fast		equ	0	; fast speed
slow		equ	1	; slow speed
;
; variables
speed		equ	0x20	;
slowdot		equ	0x21	;
fastdot		equ	0x22	;
ssecs		equ	0x27	; seconds timer - seconds
sfast		equ	0x28	;
smed		equ	0x29	;
sslow		equ	0x2a	;


; -------------------------------------------------------------------
; code base address and restart vector
		org	0x00
		goto	reset

; main program - entry on reset

reset		movlw	b'00000111'	; turn comparator off for GP1-GP0 input
		movwf	cmcon
		bsf	status,rp0	; select register bank 1 for trisio access
		movlw	b'00000000'	; set GP5-GP0 for output
		movwf	trisio
		bcf	status,rp0	; select register bank 0
;
		call	clklo		; set ADF4113 CLK low
		call	lelo		; set ADF4113 LE low
;
; test setup:
;		call setslow
;
; power-up delay:
		call setslow
		call stimer		; power up delay to allow PLL to settle
; run setup:
		call setfast
;		call setslow
;
; use 4 registers method for ADF4113 programming as in article on AD website
; program initialisation latch
		call writeinit
; program function latch
		call writefunc
; do AB load
		call writeAB
; do R load
		call writeR
; clear data
		call dat0
;
; internal reset pulse is now activated and
; the ADF4113 now runs, so go to sleep
;
; try infinite loop, sleep not working yet
; donothing	nop
;		goto	donothing
		sleep

;
; set ADF4113 clock high
clkhi		bsf	gpio,5		; set GPIO bit 5 high
		call	dtimer
		return
; set ADF4113 clock low
clklo		bcf	gpio,5		; set GPIO bit 5 low
		call	dtimer
		return
; set ADF4113 latch enable high
lehi		bsf	gpio,2		; set GPIO bit 2 high
		call	dtimer
		return
; set ADF4113 latch enable low
lelo		bcf	gpio,2		; set GPIO bit 2 low
		call	dtimer
		return
; set ADF4113 data to 1
dat1		bsf	gpio,4		; set GPIO bit 4 high
		call	dtimer
		return
; set ADF4113 data to 0
dat0		bcf	gpio,4		; set GPIO bit 4 low
		call	dtimer
		return
; toggle CLK	hi then lo
togclk		call clkhi
		call clklo
		return
; toggle LE hi then lo
togle		call lehi
		call lelo
		return

; program ADF4113 initialisation latch
writeinit	call dat0		; DB23
		call togclk
		call dat1		; DB22
		call togclk
		call dat0		; DB21
		call togclk
		call dat1		; DB20
		call togclk
		call dat1		; DB19
		call togclk
		call dat1		; DB18
		call togclk
		call dat1		; DB17
		call togclk
		call dat1		; DB16
		call togclk
		call dat1		; DB15
		call togclk
		call dat1		; DB14
		call togclk
		call dat1		; DB13
		call togclk
		call dat1		; DB12
		call togclk
		call dat1		; DB11
		call togclk
		call dat0		; DB10
		call togclk
		call dat0		; DB9
		call togclk
		call dat0		; DB8
		call togclk
		call dat1		; DB7 - PD polarity
		call togclk
		call dat1		; DB6 - M3 muxout
		call togclk
		call dat0		; DB5 - M2
		call togclk
		call dat0		; DB4 - M1
		call togclk
		call dat0		; DB3
		call togclk
		call dat1		; DB2
		call togclk
		call dat1		; DB1
		call togclk
		call dat1		; DB0
		call togclk
		call togle
		return

; program ADF4113 function latch
writefunc	call dat0		; DB23
		call togclk
		call dat1		; DB22
		call togclk
		call dat0		; DB21
		call togclk
		call dat1		; DB20
		call togclk
		call dat1		; DB19
		call togclk
		call dat1		; DB18
		call togclk
		call dat1		; DB17
		call togclk
		call dat1		; DB16
		call togclk
		call dat1		; DB15
		call togclk
		call dat1		; DB14
		call togclk
		call dat1		; DB13
		call togclk
		call dat1		; DB12
		call togclk
		call dat1		; DB11
		call togclk
		call dat0		; DB10
		call togclk
		call dat0		; DB9
		call togclk
		call dat0		; DB8
		call togclk
		call dat1		; DB7 - PD polarity
		call togclk
		call dat1		; DB6 - M3 muxout
		call togclk
		call dat0		; DB5 - M2
		call togclk
		call dat0		; DB4 - M1
		call togclk
		call dat0		; DB3
		call togclk
		call dat0		; DB2
		call togclk
		call dat1		; DB1
		call togclk
		call dat0		; DB0
		call togclk
		call togle
		return

; program ADF4113 reference (R) counter latch
writeR		call dat0		; DB23
		call togclk
		call dat0		; DB22
		call togclk
		call dat0		; DB21
		call togclk
		call dat0		; DB20
		call togclk
		call dat0		; DB19
		call togclk
		call dat0		; DB18
		call togclk
		call dat0		; DB17
		call togclk
		call dat0		; DB16
		call togclk
		call dat0		; DB15 - R14
		call togclk
		call dat0		; DB14 - R13
		call togclk
		call dat0		; DB13 - R12
		call togclk
		call dat0		; DB12 - R11
		call togclk
		call dat1		; DB11 - R10
		call togclk
		call dat1		; DB10 - R9
		call togclk
		call dat1		; DB9 - R8
		call togclk
		call dat1		; DB8 - R7
		call togclk
		call dat1		; DB7 - R6
		call togclk
		call dat0		; DB6 - R5
		call togclk
		call dat1		; DB5 - R4
		call togclk
		call dat0		; DB4 - R3
		call togclk
		call dat0		; DB3 - R2
		call togclk
		call dat0		; DB2 - R1
		call togclk
		call dat0		; DB1
		call togclk
		call dat0		; DB0
		call togclk
		call togle
		return

; program ADF4113 N (AB) counter latch
writeAB		call dat0		; DB23
		call togclk
		call dat0		; DB22
		call togclk
		call dat0		; DB21
		call togclk
		call dat1		; DB20 - B13
		call togclk
		call dat0		; DB19 - B12
		call togclk
		call dat1		; DB18 - B11
		call togclk
		call dat1		; DB17 - B10
		call togclk
		call dat1		; DB16 - B9
		call togclk
		call dat0		; DB15 - B8
		call togclk
		call dat0		; DB14 - B7
		call togclk
		call dat0		; DB13 - B6
		call togclk
		call dat1		; DB12 - B5
		call togclk
		call dat0		; DB11 - B4
		call togclk
		call dat0		; DB10 - B3
		call togclk
		call dat1		; DB9 - B2
		call togclk
		call dat0		; DB8 - B1
		call togclk
		call dat0		; DB7 - A6
		call togclk
		call dat0		; DB6 - A5
		call togclk
		call dat0		; DB5 - A4
		call togclk
		call dat1		; DB4 - A3
		call togclk
		call dat0		; DB3 - A2
		call togclk
		call dat0		; DB2 - A1
		call togclk
		call dat0		; DB1
		call togclk
		call dat1		; DB0
		call togclk
		call togle
		return

; delay timer
; the original delay timer code was for a QRSS cw keyer
dtimer		movf	speed,w		; load speed (sets Z flag for fast)
		btfsc	status,z	; test for zero (bit set means zero result)
iffast		return			; "fast" return, few cycles delay
;	-------	goto	dofast		; commented out original code
ifslow		call	setslow
		goto	stimer
dofast		call	setfast
		goto	ftimer
stimer		movf	slowdot,w	; delay length from slowdot
		movwf	ssecs
slslow		movlw	.1		; slow loop counter 1.002s temp 1 not 10
		movwf	sslow
slmed		movlw	.200		; med loop counter 100.2ms
		movwf	smed
slfast		movlw	.167		; fast loop counter 501us
		movwf	sfast
sdec		decfsz	sfast,f
		goto	sdec
		decfsz	smed,f
		goto	slfast
		decfsz	sslow,f
		goto	slmed
        	decfsz  ssecs,f
		goto	slslow
		return			;return from slow dot
ftimer		movf	fastdot,w	; dot length from fastdot
		movwf	sslow
flmed		movlw	.200		; med loop counter 100.2ms
		movwf	smed
flfast		movlw	.167		; fast loop counter 501us
		movwf	sfast
fdec		decfsz	sfast,f
		goto	fdec
		decfsz	smed,f
		goto	flfast
		decfsz	sslow,f
		goto	flmed
		return			; return from fastdot

; set fast dot length (in 100ms units)
setfast		movlw	fast
		movwf	speed
		movlw	.1		; number of 100ms dot length, max 255
		movwf	fastdot
		return

; set slow dot length (in 1s units)
setslow		movlw	slow
		movwf	speed
		movlw	.1		; number of seconds dot length, max 255
		movwf	slowdot
		return


		end

