	DEVICE PIC16f877,WDT_OFF

; Writing and reading to EEDATA space in the 16F87x series
; If using with the Mathias emulator you need the following:
; CVM87xM Member Module
; CVM03F  Advanced Family Module
; Version 2.6 or higher firmware
; TDE 4.1 or higher software

; by James Vroman - TechTools - 6/9/00
; Modified - 12/07/00
;	removed the SLEEP instruction for simplicity
;	added incrementing values and addressing for testing.
;	Add the 'ADDRESS, DATA and tmpDATA' registers
;	to the watch window to verify functionality
;	when the breakpoint is reached (see commented lines).

	ORG 70H        ;memory area common between banks

ADDRESS		DS 1
DATA       	DS 1
tmpDATA		DS 1

  ORG	00h
	jmp INIT

  ORG 05h

INIT     nop
    	setb RP1               ;Switch to
	clrb RP0               ;bank 2
	mov ADDRESS,#00h       ;address
	mov DATA,#128          ;data

START    nop

DATAWRITE nop              ;Write to the EEDATA space
    	setb RP1               ;Switch to
	clrb RP0               ;bank 2
	mov  EEADR,ADDRESS     ;set eeadr
	mov  EEDATA,DATA       ;set data
	setb RP0               ;switch to bank 3
	clrb EEPGD             ;set to DATA memory
	setb WREN              ;enable writes
	clrb GIE               ;disable interrupts
	mov  EECON2,#055h      ;
	mov  EECON2,#0AAh      ;
	setb EEWR              ;set write bit
:wait	jb   EEWR,:wait		; wait for completion
	clrb WREN              ;disable writes
	setb GIE               ;enable interrupts
 
DATAREAD nop               	;read from the DATA space
    	setb  RP1              ;Switch to
	clrb	RP0		;bank 2
	mov  EEADRH,ADDRESS    ;set eeadr
	setb RP0               ;switch to bank 3
	clrb EEPGD             ;set to DATA memory
	setb EERD              ; set to read
	clrb RP0               ;switch back to bank 2
	mov tmpDATA,EEDATA        ;move eedata to ram
	nop			; set breakpoint to see data read. *********
	inc	ADDRESS		; increment address to write to;
	inc	DATA		; increment data value;
	JNZ	START		; Do again until data resets to 0.
	jmp	$		; wait here when finished. (set breakpoint). ****
