EEprom write

Status
Not open for further replies.

Mosaic

Well-Known Member
Hello:
This is working code for those who are interested. 16f886


Code:
EEpromWrite;  write 'Tmp' number of GPR bytes into EEPROM address starting at 'wreg' value. FSR preloaded to GPR start addr.
		;FSR set for bank0,1, ; bank 2,3 GPR requires 'bsf STATUS,IRP' for FSR to work on those GPR. Preset this b4 calling this code.
	banksel EEADR
	movwf EEADR ; store dest. eprom address
	incf Tmp,f ; compensate for decfsz. Tmp is a multibank variable
EEWloop	decfsz Tmp,f ; skip next if 0, else
	goto Dowrite
	clrf STATUS; bank0, irp clr etc.
	return
Dowrite ; perform write of a gpr byte to eeprom. Costs 10ms!
	movf INDF,w;get gpr data
	movwf EEDAT ; data value to write
	banksel EECON1
	bcf EECON1,EEPGD; set to ee data mem
	bsf EECON1,WREN; enable ee write
	bcf INTCON,GIE; halt ISRs
	btfsc INTCON,GIE
	goto $-2 ; verify isr halts
	
	movlw 0x55 ; required
	movwf EECON2; required
	movlw 0xAA; required
	movwf EECON2 ; write AA
	bsf EECON1,WR; begin write
	btfsc	EECON1,WR	;WAIT FOR
	goto	$-1		;WRITE TO FINISH
	bsf INTCON,GIE; enable isrs
	bcf EECON1,WREN; disable write
	banksel EEADR
	incf FSR,f ; pt to next GPR byte
	incfsz EEADR,f ; pt to next eeprom byte, skip if overflow
	goto EEWloop
        clrf STATUS; bank0, clr IRP
        retlw 0xFF ;255 in wreg signifies an overflow happened!
 
Last edited:
Eeprom blk read

Here is the complement to the last bit of code: It is a block read but it uses a decimal 255 as an EOF marker, so there's no counter.

Code:
; EEPROM DATA READ to fill user GPR selected data block (FSR) ending at an EEPROM 0xff EOF byte
; handles banks 0 & 1, banks 2 & 3 need the IRP bit set in status reg for FSR to 'see' it.
; whatever the FSR is pointing to in bank0,1 will be the start of the block fill from eeprom address held in wreg.
EEPromRead
	banksel EEADR
	movwf EEADR ; set data memory to be accessed (whatever is in wreg)
	banksel EECON1;
	bcf EECON1,EEPGD ; set to data memory, not prg memory
	clrf STATUS; bank 0

EEdatanext
	banksel EECON1;
	bsf EECON1,RD ; execute the EEprom read on address pter EEADR.
	banksel EEDAT
	incfsz EEDAT,w ; EOF for EEDAT is 255 for this data block.
	goto Not_EOF
	clrf STATUS ; bank 0
	;bcf STATUS,IRP ;reset FSR bank select
	goto EOF
Not_EOF
	movf EEDAT,w ; get data byte into w
	movwf INDF ; place in correct cell.
	incf FSR,f ; pt to next dest. byte
	banksel EEADR
	incf EEADR,f; pt to next src EEprom byte
	goto EEdatanext

EOF ; continue rest of code
	return
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…