Absolute Position Encoder Electronics Upgrade - Appendix B.2
February 18th, 2000 - Bob Broilo

B.2 Buffer.asm

	list    p=16C55
;*******************************************************************
;                           BUFFER.ASM
; Fiber data stream buffer for VLA data converter
;
;*******************************************************************
;
;Copyright (C) 2002 Bob Broilo, National Radio Astronomy Observatory
;
;This program is free software;  you can redistribute it and/or
;modify it under the terms of the GNU General Public License
;as published by the Free Software Foundation;  either version 2
;of the License, or (at your option) any later version.
;
;This program is distributed in the hope that it will be useful,
;but WITHOUT ANY WARRANTY;  without even the implied warranty of
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;GNU General Public License for more details.
;
;You should have received a copy of the GNU General Public License
;along with this program; if not, write to the Free Software
;Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
;
;Bob Broilo - bbroilo@nrao.edu
;PO Box 0, Socorro NM  87801
;
;*******************************************************************
; Reads the 4 byte position from the fiber data stream and makes
; available to the data converter controller.
;
; Clock freq 4MHz => inst speed 1MHz => cycle time 1us
;
; Bob Broilo
; 7/25/99
;*******************************************************************
;
;
;
;*******************************************************************
; defines
;*******************************************************************
; status register
status	equ     3       ; status register
scarry	equ     0       ; 0     s0      carry bit
sdigit	equ     1       ; 1     s1      digit carry bit
szero	equ     2       ; 2     s2      zero bit
;
; porta
ctlp	equ     05      ; pins  porta - control port
fiberb	equ     0       ; 6     a0      fiber receive in
drecb	equ     1       ; 7     a1      data received signal in
drdyb	equ     2       ; 8     a2      data ready out
sync	equ     3       ; 9     a3      sync out, for testing
;
; portb
datap	equ     06      ; 10-17 portb - parallel data
;
; registers
cshi	equ     10      ; coarse high byte
cslo	equ     11      ; coarse low byte
fnhi	equ     12      ; fine high byte
fnlo	equ     13      ; fine low byte
ccount	equ     14      ; generic counter dudie
temp	equ     15      ; temp storage for fiber byte
;
; defines
wdtpre	equ     B'00111011'     ; WDT prescaler value 1:8
trisa	equ     B'00000011'     ; set porta I/O: bits 0-1 in, 2-3 out
dataon	equ     B'00000000'     ; set portb I/O: bits 0-7 out
dataoff	equ     B'11111111'     ; set portb I/O: bits 0-7 in (HiZ out)
;
;
;
;*******************************************************************
; Macros
;*******************************************************************
DELAY	macro arg               ; insert arg nops
	variable a = arg
	while a > 0
	  nop
	  a -= 1
	endw
endm
;
;
;
;*******************************************************************
; Initialization
;*******************************************************************
start	clrwdt                ; set up watchdog timer
	movlw wdtpre
	option 
	movlw trisa		; configure ports
	tris ctlp
	movlw dataoff
	tris datap
	bcf ctlp,sync		; turn off sync pulse
	bcf ctlp,drdyb		; turn off data ready line
	clrw			; clear data registers
	clrf cshi
	clrf cslo
	clrf fnhi
	clrf fnlo
;
;
;
;*******************************************************************
; Main
;*******************************************************************
;
; Wait for header pulse
main	btfsc ctlp,fiberb	; make sure we're low before searching
	goto main
;
eeep0	clrwdt			; don't watchdog out while no input
	btfss ctlp,fiberb	; wait for rising edge
	goto eeep0
	btfss ctlp,fiberb	; see if it's still high (2us)
	goto main		; if not, start over
	btfss ctlp,fiberb	; see if it's still high (4us)
	goto main             	; if not, start over
	btfss ctlp,fiberb  	; see if it's still high (6us)
	goto main           	; if not, start over
;	btfss ctlp,fiberb  	; see if it's still high (8us)
;	goto main		; if not, start over
	DELAY 4
	btfsc ctlp,fiberb	; make sure line went low
	goto main		; if not, something's wrong
;
	call rbyte		; read byte from fiber
	movf temp,0
	movwf cshi		; fine low
	call rbyte
	movf temp,0
	movwf cslo		; fine high
	call rbyte
	movf temp,0
	movwf fnhi		; coarse low
	call rbyte
	movf temp,0
	movwf fnlo		; coarse high
;
;write to external device
	bsf ctlp,drdyb		; generate data ready signal
eeep1	btfss ctlp,drecb	; wait until device is ready
	goto eeep1
	movlw dataon		; turn on the port
	tris datap
	movf fnlo,0		; fine low
	movwf datap		; make it available
eeep1a	btfsc ctlp,drecb	; wait for host to read
	goto eeep1a
eeep6	btfss ctlp,drecb	; wait for host ready
	goto eeep6
	movf fnhi,0		; fine high
	movwf datap
eeep2	btfsc ctlp,drecb	; wait until host has read it
	goto eeep2
eeep2a	btfss ctlp,drecb	; wait for host ready
	goto eeep2a
	movf cslo,0		; coarse low
	movwf datap
eeep3	btfsc ctlp,drecb	; wait until host has read it
	goto eeep3
eeep3a	btfss ctlp,drecb	; wait for host ready
	goto eeep3a
	movf cshi,0		; coarse high
	movwf datap
eeep4	btfsc ctlp,drecb	; wait until host has read it
	goto eeep4
	movlw dataoff		; turn off port (HiZ)
	tris datap
	bcf ctlp,drdyb		; release host
	movlw wdtpre
	option
	clrwdt			; reset watchdog
	goto main
;
;*******************************************************************
; rbyte - Receive byte routine
;*******************************************************************
;
; Receive a byte from the fiber
rbyte	movlw 08		; read eight bits
	movwf ccount
	clrf temp
mloop	btfss ctlp,fiberb	; wait for marker bit
	goto mloop		; marker bit syncs up rest of stream
	DELAY 2			; get in middle of pulse
	bcf status,scarry	; make sure carry is zero
rloop	rlf temp,1		; scoot bit up
	DELAY 3			; wait for next bit
;	DELAY 2			; added to compensate for removing below
			; ***********************       NOTE: if removed
	bsf ctlp,sync	; *** pulse sync line ***       be sure to add
	bcf ctlp,sync	; *** TESTING ONLY!!! ***       two cycles to
			; ***********************       delay above
	btfsc ctlp,fiberb
	incf temp		; if high, put '1' in byte
	decfsz ccount,1		; are we done?
	goto rloop		; if not, get next bit
; should go low here or something's wrong
	retlw 0			; return to main
	END