Hi, Roger Froud wrote: >I'm having problems reading or writing the EEPROM data memory on a >PIC18F458. I've faithfully copied the simplest piece of code from the >PIC18FXX8 data sheet 5-1 but it doesn't return the correct value. The >value in EECON1 stays at 0xC0 unless you clear EEFS which then allows I think you need to get the latest datasheet, beacuse I don't recall = EEFS being available anymore. Anyway below is my routines, copied straight from the = datasheet(mostly)and I know they are working (used in a project). /Tony To save an ramblock to eeram use the following (only within one ram page = !): ; = *********************************************************************** ; EE_SAVE_RAM - Save ram parameters to eeram ;=20 EE_SAVE_RAM GLOBAL EE_SAVE_RAM ; clear eeadr CLRF EEADR ; calculate number of bytes to save (range <255, must be on same page) MOVLW LOW(RAM_BLOCK_START) ; start ram block within the page ; calc. length SUBLW LOW(RAM_BLOCK_END) ; end ram block withing same page MOVWF EETemp ; save number of bytes=20 ;setup fsr0 to source adress LFSR 0,RAM_BLOCK_START EE_SAVE_LOOP ; now write each byte to eeram MOVF POSTINC0,W CALL EE_WRITE_W_BYTEinc DECFSZ EETemp,F BRA EE_SAVE_LOOP ; all done RETURN To restore the same block (from eeram) use: ; = *********************************************************************** ; EE_INIT_RAM - Initilise ram parameters with data from eeram ;=20 EE_INIT_RAM GLOBAL EE_INIT_RAM ; clear eeadr CLRF EEADR ; get number of bytes to restore MOVLW LOW(RAM_BLOCK_START) ; calc. length SUBLW LOW(RAM_BLOCK_END) MOVWF EETemp ;setup fsr0 to destination adress LFSR 0,RAM_BLOCK_START EE_INIT_LOOP ; now read and store each byte in ram CALL EE_READ_BYTEinc MOVWF POSTINC0 ; save and set to next byte DECFSZ EETemp,F BRA EE_INIT_LOOP ; all done RETURN Read/write routines: ; = *********************************************************************** ; EE_WRITE_W_BYTEinc - Write the byte in w to *current* adress in ee=20 ; then polls the ee and returns when finished, increments eeadress=20 ; once for each call. ; NOTE! EEADR must be setup prior calling routine ;=20 EE_WRITE_W_BYTEinc GLOBAL EE_WRITE_W_BYTEinc MOVWF EEDATA ; setup data to write BCF EECON1,EEPGD ; point to data mem BSF EECON1,WREN ; enable writes BCF INTCON,GIE ; disable interrupts MOVLW 0x55 ; first *magic* number in required sequence MOVWF EECON2 ; write it MOVLW 0xAA ; second *magic* number in required sequence MOVWF EECON2 ; write it BSF EECON1,WR ; start write sequence BSF INTCON,GIE ; enable interrupts again BCF EECON1,WREN ; disable writes ( does not affect current write cycle = ) EE_WRITE_W_WAIT=09 ; wait for the write to complete before we return BTFSS PIR2,EEIF ; wait for interrupt flag to be set BRA EE_WRITE_W_WAIT ; not done yet ; clear interupt flag BCF PIR2,EEIF ;clear eewrite irq flag ; increase destination adress for eventual next byte INCF EEADR,F RETURN ; and return ; = *********************************************************************** ; EE_READ_BYTEinc - Reads the byte from*current* adress in ee=20 ; increments eeadress and returns with read data in w ; NOTE! EEADR must be setup prior calling routine ;=20 EE_READ_BYTEinc BCF EECON1,EEPGD ; set to read data memory BCF EECON1,CFGS ; set to read data memory BSF EECON1,RD ; set bit to read MOVF EEDATA,W ; move data to W INCF EEADR,F ; increase adress for eventual next call RETURN ; and return -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.