2
votes
Platform MPLAB X
CPU PIC18F2520
Compiler XC8 v1.38

We are moving a project from an old (htc) compiler to the MPLAB X platform but cannot access the EEPROM.

The old compiler supported the eeprom_read and eeprom_write but XC8 does not, well, there are defines defining them but they are "empty". (xc.h includes htc.h which include pic18.h) In pic.h the line

#if _EEPROMSIZE > 0 && defined(_PLIB)

Is not triggered but instead the corresponding #else It seems that neither _EEPROMSIZE nor _PLIB is defined.

Why is it so that the older (eeprom_read and eeprom_write) is not supported in xc8 ?

What should we do to be able to access the EEPROM?

We tried to see what Microchip Code Configure would have done, but the CPU PIC18F2520 is not supported by MCC.

The chip do have 256 byte eeprom according to

http://ww1.microchip.com/downloads/en/DeviceDoc/39631E.pdf

Regards

2

2 Answers

1
votes

Yes, as you have discovered, Microchip has removed the EEPROM read/write functions for the PIC18 family of microcontrollers in the MPLAB X IDE. It has left the macros as empty shells (EEPROM_READ(), eeprom_read(), e.t.c) which will not throw a compiler error/warning, but won't actually do anything!

pic18.h suggests that if #if _EEPROMSIZE > 0 && defined(_PLIB) is true then the macros will be filled in, yet by manually adding the PLIB macro definition, the compiler then complained about not being able to find plib.h. I could not find a suitable place to download "plib" from.

One option is to revert back to direct register manipulation to perform EEPROM reads/writes (see page 75 of http://ww1.microchip.com/downloads/en/DeviceDoc/39626e.pdf for EEPROM register definitions for one particular PIC18 micro). I have written the following functions which abstract that away and provide similar functionality to what Microchip removed:

//! @brief      Reads a single byte of data from the EEPROM.
//! @param      address     The EEPROM address to write the data to (note that not all
//!                         16-bits of this variable may be supported).
//! @returns    The byte of data read from EEPROM.
//! @warning    This function does not return until read operation is complete.
uint8_t Eeprom_ReadByte(uint16_t address)
{

    // Set address registers
    EEADRH = (uint8_t)(address >> 8);
    EEADR = (uint8_t)address;

    EECON1bits.EEPGD = 0;       // Select EEPROM Data Memory
    EECON1bits.CFGS = 0;        // Access flash/EEPROM NOT config. registers
    EECON1bits.RD = 1;          // Start a read cycle

    // A read should only take one cycle, and then the hardware will clear
    // the RD bit
    while(EECON1bits.RD == 1);

    return EEDATA;              // Return data

}

//! @brief      Writes a single byte of data to the EEPROM.
//! @param      address     The EEPROM address to write the data to (note that not all
//!                         16-bits of this variable may be supported).
//! @param      data        The data to write to EEPROM.
//! @warning    This function does not return until write operation is complete.
void Eeprom_WriteByte(uint16_t address, uint8_t data)
{    
    // Set address registers
    EEADRH = (uint8_t)(address >> 8);
    EEADR = (uint8_t)address;

    EEDATA = data;          // Write data we want to write to SFR
    EECON1bits.EEPGD = 0;   // Select EEPROM data memory
    EECON1bits.CFGS = 0;    // Access flash/EEPROM NOT config. registers
    EECON1bits.WREN = 1;    // Enable writing of EEPROM (this is disabled again after the write completes)

    // The next three lines of code perform the required operations to
    // initiate a EEPROM write
    EECON2 = 0x55;          // Part of required sequence for write to internal EEPROM
    EECON2 = 0xAA;          // Part of required sequence for write to internal EEPROM
    EECON1bits.WR = 1;      // Part of required sequence for write to internal EEPROM

    // Loop until write operation is complete
    while(PIR2bits.EEIF == 0)
    {
        continue;   // Do nothing, are just waiting
    }

    PIR2bits.EEIF = 0;      //Clearing EEIF bit (this MUST be cleared in software after each write)
    EECON1bits.WREN = 0;    // Disable write (for safety, it is re-enabled next time a EEPROM write is performed)
}
0
votes

using MCC select Memory module and it will create memory.c with all the functions needed