Saving data to External EEPROM with PIC18

Discussion in 'Embedded Systems and Microcontrollers' started by ammar555, May 27, 2013.

  1. ammar555

    Thread Starter New Member

    Feb 14, 2013
    7
    0
    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!
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,414
    3,353
    All the information you need is in the datasheet. What is the problem?
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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?
     
  5. MrChips

    Moderator

    Oct 2, 2009
    12,414
    3,353
    Get stuck? I think the op hasn't started.
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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?
     
  7. MrChips

    Moderator

    Oct 2, 2009
    12,414
    3,353
    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.
     
  8. ammar555

    Thread Starter New Member

    Feb 14, 2013
    7
    0
    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.

    [​IMG]

    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)

    Code ( (Unknown Language)):
    1. #define PAGESIZE    16  
    2. static unsigned char data[PAGESIZE];        // One-page data array
    3. static unsigned char i;
    4.  
    5. init();                                     // Initialize PIC
    6.  
    7.     data[0] = 0xCC;                             // Initialize first data byte
    8.  
    9.     /* Low-density byte function calls */
    10.     LowDensByteWrite(data[0], 0x133);           // Write 1 byte of data at 0x133
    11.     data[0] = 0xFF;
    12.     LowDensByteRead(data, 0x133);      
    13.     printf("%x",data);
    14.  
    15. void init(void)
    16. {
    17.     ADCON1 = 0x7F;                      // Configure digital I/O
    18.     PORTA = 0x08;                       // Set CS high (inactive)
    19.     TRISA = 0b11110111;                 // Configure PORTA I/O
    20.     PORTB = 0;                          // Clear all PORTB pins
    21.     TRISB = 0b11111100;                 // Configure PORTB I/O
    22. } /
    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: May 27, 2013
  9. ammar555

    Thread Starter New Member

    Feb 14, 2013
    7
    0
    Check my above post, I included the problem.
     
  10. MrChips

    Moderator

    Oct 2, 2009
    12,414
    3,353
    Where is your main( ) program?
    You have to show the full program.
    Where is your circuit diagram?
     
  11. ammar555

    Thread Starter New Member

    Feb 14, 2013
    7
    0
    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.


    Code ( (Unknown Language)):
    1.  
    2. #include <p18f1220.h>
    3. #include "AN1018.h"
    4.  
    5. /** C O N F I G ****************************************************/
    6.  
    7. #if defined(__18F87J11)
    8.  
    9.     #pragma config FOSC = HSPLL, WDTEN = OFF, WDTPS = 128, XINST = ON  
    10.                                                        
    11. #endif
    12.  
    13. #define PAGESIZE    16                          // 16-byte page size for 25XX040
    14.  
    15. /** P R O T O T Y P E S ********************************************/
    16. void init(void);                                // Init. function
    17.  
    18. void main(void)
    19. {
    20.     static unsigned char data[PAGESIZE];        // One-page data array
    21.     static unsigned char i;                     // Loop counter
    22.  
    23.     init();                                     // Initialize PIC
    24.  
    25.     data[0] = 0xCC;                             // Initialize first data byte
    26.  
    27.     /* Low-density byte function calls */
    28.     LowDensByteWrite(data[0], 0x133);           // Write 1 byte of data at 0x133
    29.     data[0] = 0xFF;
    30.     LowDensByteRead(data, 0x133);  
    31.     printf("%c",data);            // Read 1 byte of data at 0x133
    32. }
    33.  
    34. void init(void)
    35. {
    36.     ADCON1 = 0x7F;                      // Configure digital I/O
    37.     PORTA = 0x08;                       // Set CS high (inactive)
    38.     TRISA = 0b11110111;                 // Configure PORTA I/O
    39.     PORTB = 0;                          // Clear all PORTB pins
    40.     TRISB = 0b11111100;                 // Configure PORTB I/O
    41. } // end init(void)
    42.  
    I apologize in advance if this is not what your are looking for.
     
    Last edited: May 27, 2013
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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.
     
  13. ammar555

    Thread Starter New Member

    Feb 14, 2013
    7
    0
    I am using MPLAB C18 compiler. I was hoping for a recent example on Microchip but that's all they have.
     
  14. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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)
     
  15. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  16. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
Loading...