EEPROM Library

EEPROM data memory is available with a number of dsPIC30 family and some PIC24 family MCU's. The mikroC PRO for dsPIC30/33 and PIC24 includes a library for comfortable work with MCU's internal EEPROM.

Library Routines

EEPROM_Erase

Prototype

void EEPROM_Erase(unsigned long address);

Description

Erases a single (16-bit) location from EEPROM memory.

Parameters
  • address: address of the EEPROM memory location to be erased.
Returns

Nothing.

Requires

Nothing.

Example
unsigned long eeAddr = 0x7FFC80;
...
EEPROM_Erase(eeAddr);
Notes

CPU is not halted for the Data Erase cycle. The user can poll WR bit, use NVMIF or Timer IRQ to detect the end of erase sequence.

EEPROM_Erase_Block

Prototype

void EEPROM_Erase_Block(unsigned long address);

Description

Erases one EEPROM row from EEPROM memory; For dsPIC30 family it is 16 words long, for 24F04KA201 and 24F16KA102 family it is 8 words long.

Parameters
  • address: starting address of the EEPROM memory block to be erased.
Returns

Nothing.

Requires

Nothing.

Example
unsigned long eeAddr = 0x7FFC20;
...
EEPROM_Erase_Block(eeAddr);
Notes

CPU is not halted for the Data Erase cycle. The user can poll WR bit, use NVMIF or Timer IRQ to detect the end of erase sequence.

EEPROM_Read

Prototype

unsigned int EEPROM_Read(unsigned long address);

Description

Reads data from specified address.

Parameters
  • address: address of the EEPROM memory location to be read.
Returns

Word from the specified address.

Requires

It is the user’s responsibility to obtain proper address parity (in this case, even).

Example
unsigned long eeAddr = 0x7FFC20;
unsigned int temp;
...
temp = EEPROM_Read(eeAddr);
Notes

None.

EEPROM_Write

Prototype

void EEPROM_Write(unsigned long address, unsigned int data_);

Description

Writes data to specified address.

Parameters
  • address: address of the EEPROM memory location to be written.
  • data: data to be written.
Returns

Nothing.

Requires

Nothing.

Example
unsigned int  eeWrite = 0xAAAA;
unsigned long  wrAddr = 0x7FFC30;
...
EEPROM_Write(wrAddr, eeWrite);
Notes

Specified memory location will be erased before writing starts.

EEPROM_Write_Block

Prototype

void EEPROM_Write_Block(unsigned long address, unsigned int *data);

Description

Writes one EEPROM row (16 words block) of data.

Parameters
  • address: starting address of the EEPROM memory block to be written.
  • data: data block to be written.
Returns

Nothing.

Requires

It is the user's responsibility to maintain proper address alignment. In this case, address has to be a multiply of 32, which is the size (in bytes) of one row of MCU's EEPROM memory.

Example
unsigned long  wrAddr = 0x7FFC20;
unsigned int iArr[16] = {'m', 'i', 'k', 'r', 'o', 'E', 'l', 'e', 'k', 0};
...
EEPROM_Write_Block(wrAddr, iArr);
Notes
  • Specified memory block will be erased before writing starts.
  • This routine is not applicable to the 24F04KA201 and 24F16KA102 family of MCUs, due to the architecture specifics.

Library Example

This project demonstrates usage of EEPROM library functions for dsPIC30F4013. Each EEPROM (16-bit) location can be written to individually, or in 16-word blocks, which is somewhat faster than the former. If Writing in blocks, EEPROM data start address must be a multiply of 16. Please read Help for more details on the library functions!

Copy Code To ClipboardCopy Code To Clipboard
unsigned int eeData;
unsigned long eeAddr;
unsigned int dArr[16];

void main() {

  unsigned i;

  ADPCFG = 0xFFFF;                         // Disable analog inputs
  
  TRISB = 0;                               // PORTB as output
  LATB = 0xFFFF;
  eeAddr = 0x7FFC00;                       // Start address of EEPROM
  eeData = 0;                              // Data to be written

  while (eeData <= 0x00FF) {
    EEPROM_Write(eeAddr, eeData++);        // Write data into EEPROM
    while(WR_bit);                         // Wait for write to finish,
    LATB = EEPROM_Read(eeAddr);            // then, read the just-written
                                           // data.
    eeAddr += 2;                           // Next address of EEPROM memory location

    Delay_ms(30);
  }

  Delay_ms(1000);                          // Wait 1 second.
  
  eeData = 0xAAAA;
  for (i=0; i<16; i++){                    // Initializing array of 16 integers with data
    dArr[i] = eeData;
    eeData = ~eeData;
  }

  EEPROM_Write_Block(0x7FFC20, dArr);      // Write entire row of EEPROM data
  while(WR_bit)
    ;                                      // Wait for write to finish

  eeAddr = 0x7FFC20;                       // Address of EEPROM where reading should start
  for (i=0; i<16; i++){                    // Read the data back
    LATB = EEPROM_Read(eeAddr);            // and show it on PORTB
    eeAddr += 2;                           // Next address of EEPROM memory location
    Delay_ms(500);
  }
}
Copyright (c) 2002-2018 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code