; EEPROM_2.ASM
;
; This is a fragment of code to illustrate how up to eight 64 bit 
; (8 byte) serial numbers associated with Dallas one-wire devices 
; might be stored in the 16C84s serial EEPROM and then used to 
; selectively address a specific device.
;
; The serial numbers are programmed into EEPROM on program download 
; using the assembler's "define EEPROM" (DE) directive.  Each serial 
; number requires eight bytes.  
;
; Thus, the 8-byte serial number associated with device "n" begins at 
; EEADR location "n*8".
;
; Note that the serial number associated with device 0 uses EEPROM 
; locations 00-07, device 1 uses 08-0F, ...,  device 7 uses 37-3F.  
;
; This routine consists simply of a subroutine "SEND_SERIAL_NUMBER".  
; The device identity (0-7) is passed to the subroutine in W.  This is 
; mapped into an EEADR address by multiplying by eight.
;
; copyright, Peter H. Anderson, H. Paul Roach, MSU, June 17, '97
;

	LIST p=16c84
#include <p16c84.inc>	
	__CONFIG 11h

BYTE_COUNTER	EQU 0CH
O_BYTE		EQU 0DH		; used by OUT_BYTE

	ORG 000H	

SEND_SERIAL_NUMBER:

	ANDLW 07H		; limit to 0 - 7
	MOVWF EEADR

	BCF STATUS, C		; multiply by 8
	RLF EEADR, F
	RLF EEADR, F
	RLF EEADR, F		

	MOVLW .8		; set up loop counter
	MOVWF BYTE_COUNTER
SER_NUM_LOOP:
	CALL READ_EEPROM	; fetch byte
	MOVWF O_BYTE		; and send to device
	CALL OUT_BYTE
	DECFSZ BYTE_COUNTER, F
	GOTO SER_NUM_LOOP	; continue through all 8 bytes
	RETURN

READ_EEPROM:	; reads eeprom data at location specified in EEADR
		; returns the value in W
	
	BSF STATUS, RP0		; bank 1
	BSF EECON1, RD		; set the RD bit
	BCF STATUS, RP0		; back to bank 0
	MOVF EEDATA, W		; fetch value of EEDATA
	RETURN

OUT_BYTE:			; dummy to get the code to assemble
	RETURN

	ORG 2100H	; starting point of EEPROM on 16C84

	DE 64H, 12H, 13H, 81H, 0AAH, 55H, 99H, 0E2H	; serial number 0
	DE 64H, 13H, 15H, 82H, 0FFH, 23H, 98H, 0C3H	; serial number 1
	; etc for up to eight devices

	END