# EEPROM w/ PIC18F26K22

#### Peaches41

Joined Dec 15, 2016
70
Thank you all for reading my post,
I am, for the first time, using the eeprom on a PIC18F26K22 using assembly language. I am using the code examples used in the data sheet to try and save a value in to a location in eeprom and then read it back.

I first program the PIC with the "call write_eeprom" part of my program(see code below). I then reprogram the chip with the "write_eeprom" omitted so I am only reading that address and not writing to the eeprom (call read_eeprom).

After my reading of the eeprom I compare the value read to a literal value and light an LED accordingly. If I get a non match with my result I change the literal value to 'ff' and re-programm the "read" part of my program and the result was a match and the proper LED would light telling me that the eeprom did not hold the value I wanted after power was removed. I have my BOR activated in my config. settings and I have a one second delay at the beginning of the program before I do anything.

If I simulate this in MPLABX V 3.45, this will simulate fine. The only thing that I do different is I run the call write_eeprom and then continue on and do the call read_eeprom so it all runs as one code. I do not preserve the eeprom data during call write_eeprom, but I do preserve eeprom data during call read_eeprom programming. This is a check box in the properties window for the PM3 programmer that I am using.

Can anyone help me with understanding why I am unable to keep my value in the eeprom address after I program it?

Thank you,

Peaches

Mod edit: added paragraphs as suggested below.

#### Attachments

• 8.3 KB Views: 15
Last edited by a moderator:

Joined Jul 18, 2013
23,903
Also I don't see any clearing of the ANSELA/B/C etc, these are default analogue inputs.
Max.

#### dl324

Joined Mar 30, 2015
13,499
Thank you all for reading my post,

I've tried to reformat below.
I am, for the first time, using the eeprom on a PIC18F26K22 using assembly language. I am using the code examples used in the data sheet to try and save a value in to a location in eeprom and then read it back. I first program the PIC with the "call write_eeprom" part of my program(see code below). I then reprogram the chip with the "write_eeprom" omitted so I am only reading that address and not writing to the eeprom (call read_eeprom).

After my reading of the eeprom I compare the value read to a literal value and light an LED accordingly. If I get a non match with my result I change the literal value to 'ff' and re-programm the "read" part of my program and the result was a match and the proper LED would light telling me that the eeprom did not hold the value I wanted after power was removed. I have my BOR activated in my config. settings and I have a one second delay at the beginning of the program before I do anything.

If I simulate this in MPLABX V 3.45, this will simulate fine. The only thing that I do different is I run the call write_eeprom and then continue on and do the call read_eeprom so it all runs as one code. I do not preserve the eeprom data during call write_eeprom, but I do preserve eeprom data during call read_eeprom programming. This is a check box in the properties window for the PM3 programmer that I am using.

Can anyone help me with understanding why I am unable to keep my value in the eeprom address after I program it?
I'm not going to look for issues in your assembly code. If you draw the timing diagrams you're using for writing and reading, I'll comment on that.

#### Peaches41

Joined Dec 15, 2016
70
Also I don't see any clearing of the ANSELA/B/C etc, these are default analogue inputs.
Max.
Thank you for your reply. If you look in the Initialization section of my code you will see that I am clearing the ansela/b/c.

LFSR 0, ANSELA ;POINT TO ANSELA REGISTER USING INDIRECT ADDRESSING FSR0
CLRF POSTINC0 ;digital I/O SET PINS TO DIGITAL ANSELA
CLRF POSTINC0 ;digital I/O SET PINS TO DIGITAL ANSELB
CLRF POSTINC0 ;digital I/O SET PINS TO DIGITAL ANSELC

Peaches

#### JohnInTX

Joined Jun 26, 2012
4,594
Wow. You are doing things the hard way! I wouldn't use the PM3 like that.. I suppose you could write the EE, put the chip in the PM3 then read the EEPROM directly to see if your data is there.

Why not just
• Poll WR to be sure the EEPROM is ready to write.
• write the EEPROM'
• wait until the WR bit indicates that the write is done
• compare data and indicate the result.
• stop
Do you have a debugger like the PICkit 3?

Last edited:

#### LesJones

Joined Jan 8, 2017
3,570
This is the EEPROM code that I used for a PIC18F2431

Code:
;*****************************************************************************
;
; subroutine to read from EEPROM
;
;
;
;  Output:  Date read from EEPROM  (In "W" register)
;
;*****************************************************************************
; subroutine to read EEPROM memory  (For PIC18F2431)
BCF    EECON1, EEPGD, 0    ;Point to DATA memory (The "0" is to use access bank)
BCF   EECON1, CFGS, 0     ;(The "0" is to use access bank)
BSF    EECON1, RD, 0     ;EEPROM Read  (The "0" is to use access bank)
MOVF    EEDATA, W ,0     ;(The "0" is to use access bank)
return

;*****************************************************************************
;
; subroutine to write to EEPROM
;
; Function : EWRITE
;       Writes data in "W" register to internal EEPROM
;     Address of EEPROM to be written must first be put in EEADR register
;
;  Input:  Value to write to internal EEPROM
;
;
;*****************************************************************************

; subroutine to write to EEPROM

EWRITE           ;By default access bank used - no need to set BSR
MOVWF    EEDATA      ; Data Memory Value to write
BCF    EECON1, EEPGD    ; Point to DATA memory
BCF   EECON1, CFGS
BSF    EECON1, WREN    ; Enable writes
BCF    INTCON, GIE    ; Disable Interrupts
MOVLW    0x55      ;
MOVWF    EECON2      ; Write 55h
MOVLW    0xAA      ;
MOVWF    EECON2      ; Write AAh
BSF    EECON1, WR    ; Set WR bit to begin write
BSF    INTCON, GIE    ; Enable Interrupts
EW01   BTFSS   PIR2, EEIF   ;Test for write complete
GOTO   EW01
BCF   PIR2, EEIF   ;clear write complete interrupt flag
BCF    EECON1, WREN    ; Disable writes
RETURN

;*****************************************************************************
I think it will probably also work with the PIC18F26K22

Les.

#### Peaches41

Joined Dec 15, 2016
70

I do not have a debugger like the PICkit3. The only way I am able to debug is within the MPLABX IDE. As this is my first time using eeprom I just went to the data sheet and used the example code. In troubleshooting my errors I thought the way I described in my first post would be the best way that I knew of to do it. To me the six points you suggest are all handled in the code provided from the sample code. I am going to have to rethink this.

Peaches

#### Peaches41

Joined Dec 15, 2016
70
Thank you Les. I will review your code. Thank you for sharing it with me.

Peaches

#### JohnInTX

Joined Jun 26, 2012
4,594
To me the six points you suggest are all handled in the code provided from the sample code. I am going to have to rethink this.
They are all handled but in two different builds. When you do that, you introduce the question whether the PM3 actually preserves the EEPROM or not - on a lot of chips it doesn't. Note that during programming for most PICs, the chip is bulk-erased including EEPROM. The programmer is responsible for reading the EE before erasing then reprogramming after the bulk-erase. Sometimes it doesn't always work...

But in any application, you are going to have to store data into the EE then read it back under normal program execution. What I describe is a way to do it in one build and have it execute like it would in a real application.

#### paulfjujo

Joined Mar 6, 2014
23
hello,

don't forget , this PIC has 1024 bytes of eeprom
so Eeprom adresse must be define with 16 bits ..