# Saving data to External EEPROM with PIC18

#### ammar555

Joined Feb 14, 2013
7
Hi,

I have PIC18F87J11 with 25LC1024 external EEPROM, and I would like to be able to store some data on it and be able to read it later on. I have done some research, but unfortunately I could not find a tutorial that uses similar board. May I get some assistance that can help me get started ? Perhaps a link to some tutorial, quick example from you, or anything useful. Of course I am using MPLAB IDE with C18 compiler.

PIC18F87J11
http://www.microchip.com/...aspx?dDocName=en027149

25LC1024
http://www.microchip.com/...aspx?dDocName=en520389

Example I attempted to follow but uses different board
http://www.microchip.com/...4&appnote=en025489

Thank you!

#### MrChips

Joined Oct 2, 2009
24,441
All the information you need is in the datasheet. What is the problem?

#### ErnieM

Joined Apr 24, 2011
8,183
I've used I2C way more the SPI but they can both be a PITA to get working. The example app note doesn't use the PIC hardware to do SPI so it should work with any PIC.

You should be able to see the return data if you use a debugger (IE, PICkit). Where did you get stuck working this?

#### MrChips

Joined Oct 2, 2009
24,441
Get stuck? I think the op hasn't started.

#### ErnieM

Joined Apr 24, 2011
8,183
Get stuck? I think the op hasn't started.
Here we have a new member who actually links to the device data sheets he's using, plus the app note he's using as a source.

So is it really necessary to be snippy and dismissive so soon?

#### MrChips

Joined Oct 2, 2009
24,441
Sorry, I couldn't help it.
But it would help if folks who are stuck would show what they have done so far.
They should not expect us to provide solutions on a silver platter.

#### ammar555

Joined Feb 14, 2013
7
Hey guys,

I will tell you where I am stuck at, let us begin.

In order to write to the 25LC1024 external EEPROM I followed a tutorial from microchip here. The first problem is that this tut is written for PIC18F1220 and I use PIC18F87J11. So upon opening the project I get two files not found error, but I simply ignored them.

I copied the file AN1018.h and AN1018_SPI.c to the project I am working on, and I copied some piece of code from AN1018.c file.

Code from AN1018.c file (These are all the code I used and they are not placed in order like this, this is just to show you what I used)

Rich (BB code):
#define PAGESIZE    16
static unsigned char data[PAGESIZE];        // One-page data array
static unsigned char i;

init();                                     // Initialize PIC

data[0] = 0xCC;                             // Initialize first data byte

/* Low-density byte function calls */
LowDensByteWrite(data[0], 0x133);           // Write 1 byte of data at 0x133
data[0] = 0xFF;
printf("%x",data);

void init(void)
{
ADCON1 = 0x7F;                      // Configure digital I/O
PORTA = 0x08;                       // Set CS high (inactive)
TRISA = 0b11110111;                 // Configure PORTA I/O
PORTB = 0;                          // Clear all PORTB pins
TRISB = 0b11111100;                 // Configure PORTB I/O
} /
Every time I would run this code my printed output is always 1e0. This is the problem I faced and I didn't know why I am getting 1e0.

So

What am I doing wrong?
How can I confirm that data was stored to memory? (I have PICKit 3)

Last edited:

#### ammar555

Joined Feb 14, 2013
7
All the information you need is in the datasheet. What is the problem?
You should be able to see the return data if you use a debugger (IE, PICkit). Where did you get stuck working this?
Check my above post, I included the problem.

#### MrChips

Joined Oct 2, 2009
24,441
Where is your main( ) program?
You have to show the full program.

#### ammar555

Joined Feb 14, 2013
7
Where is your main( ) program?
You have to show the full program.
I do not know if this will help but my main program consists for many different codes that I can't share here. For the circuit diagram I do not really know much about it, sorry I am new to Microcontroller programming. However, I suspect that the problem might be linked to the init function as those were written merly for PIC18F1220.

As for the main program I created this for now, maybe it would help? You can ask me questions regarding other things if more info is required and I will answer to my best of knowledge.

Rich (BB code):
#include <p18f1220.h>
#include "AN1018.h"

/** C O N F I G ****************************************************/

#if defined(__18F87J11)

#pragma config FOSC = HSPLL, WDTEN = OFF, WDTPS = 128, XINST = ON

#endif

#define PAGESIZE    16                          // 16-byte page size for 25XX040

/** P R O T O T Y P E S ********************************************/
void init(void);                                // Init. function

void main(void)
{
static unsigned char data[PAGESIZE];        // One-page data array
static unsigned char i;                     // Loop counter

init();                                     // Initialize PIC

data[0] = 0xCC;                             // Initialize first data byte

/* Low-density byte function calls */
LowDensByteWrite(data[0], 0x133);           // Write 1 byte of data at 0x133
data[0] = 0xFF;
printf("%c",data);            // Read 1 byte of data at 0x133
}

void init(void)
{
ADCON1 = 0x7F;                      // Configure digital I/O
PORTA = 0x08;                       // Set CS high (inactive)
TRISA = 0b11110111;                 // Configure PORTA I/O
PORTB = 0;                          // Clear all PORTB pins
TRISB = 0b11111100;                 // Configure PORTB I/O
} // end init(void)
I apologize in advance if this is not what your are looking for.

Last edited:

#### t06afre

Joined May 11, 2009
5,934
Just to be sure. Do you use the MPLAB C18 compiler or any other compiler (like the XC8 compiler). This example is from 2005 so much has changed since then.

#### ammar555

Joined Feb 14, 2013
7
Just to be sure. Do you use the MPLAB C18 compiler or any other compiler (like the XC8 compiler). This example is from 2005 so much has changed since then.
I am using MPLAB C18 compiler. I was hoping for a recent example on Microchip but that's all they have.

#### t06afre

Joined May 11, 2009
5,934
As a start you must include include the correct header and lkr files with the correct one for your PIC. That will say replacing every
#include <p18f1220.h> with the correct one for your PIC(in both C files). And the same goes for the lkr file. I would HIGHLY recommend building a new project. Using the project wizard in MPLAB, and then include the correct files. Perhaps this document and section 3 may be of some help. http://ww1.microchip.com/downloads/en/DeviceDoc/41370C.pdf
Then the next step would be to find out which pins that are used for SPI on your PIC. Then verify the #defines in AN1018.h file and modify according to your PIC. And also the TRISx setting in the init function (AN1018.c)

#### ErnieM

Joined Apr 24, 2011
8,183
What am I doing wrong?
How can I confirm that data was stored to memory? (I have PICKit 3)
Gee, I dunno what you're doing wrong but I do know how to use the PICkit 3 as an in circuit debugger.

Leave it connected to your board as if you are programming. Change the build from Release to Debug, enable the PICkit as the debugger, and rebuild your program, and program the PIC.

You now get 3 breakpoints in the program where execution will stop, and you can peek at the contents of any register or variable. If you keep to 2 or less breakpoints you can step thru your C code line by line.

Try that and see what you get.

#### ErnieM

Joined Apr 24, 2011
8,183
BTW, unless you are doing something special like a bootloader or such the default linker script in C18 is fine, and if you don't tell it what to use then it uses the default.

Eazy peazy.