EEPROM Library

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

  Important : Only 24F04KA201 and 24F16KA102 of PIC24 family of MCUs have EEPROM memory.

Library Routines

EEPROM_Erase

Prototype

procedure EEPROM_Erase(address : longint);

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
var eeAddr : longint;
...
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

procedure EEPROM_Erase_Block(address : longint);

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
var eeAddr : longint;
...
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

function EEPROM_Read(address : longint) : word;

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
var eeAddr : longint;
      temp : word;
...
eeAddr := 0x7FFC20;
temp := EEPROM_Read(eeAddr);
Notes

None.

EEPROM_Write

Prototype

procedure EEPROM_Write(address : longint; data_ : word);

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
var wrAddr : longint;
    eeData : word;
  ...
  eeData := 0xAAAA;
  wrAddr := 0x7FFC30;
  EEPROM_Write(wrAddr, eeData);
Notes

Specified memory location will be erased before writing starts.

EEPROM_Write_Block

Prototype

procedure EEPROM_Write_Block(address : longint; var data_ : array[100] of word);

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
var  wrAddr : longint;
     data : string[16];
...
wrAddr := 0x7FFC20;
data := 'mikroElektronika';
EEPROM_Write_Block(wrAddr, data);
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
program Eeprom;
var eeData, i : word;
    eeAddr : dword;
    dArr : array [16] of word;

begin

  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) do
    begin
      Eeprom_Write(eeAddr, eeData);         // Write data into EEPROM
      Inc(eeData);
      while (WR_bit) do                     // Wait for write to finish,
        ;
      LATB := Eeprom_Read(eeAddr);          // then, read the just-written data.
      eeAddr := eeAddr + 2;                 // Next address of EEPROM memory location

      Delay_ms(100);
    end;

  Delay_ms(1000);                           // Wait 1 second.
  
  eeData := 0xAAAA;
  for i := 0 to 15 do                       // Initializing array of 16 integers with data
    begin
      dArr[i] := eeData;
      eeData  := not eeData;
    end;

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

  eeAddr := 0x7FFC20;                       // Address of EEPROM where reading should start
  for i := 0 to 15 do                       // Read the data back
    begin
      LATB := Eeprom_Read(eeAddr);          // and show it on PORTB
      eeAddr := eeAddr + 2;                 // Next address of EEPROM memory location
      Delay_ms(500);
    end
end.
Copyright (c) 2002-2012 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