EEPROM Library

EEPROM data memory is available with a number of 8051 family. The mikroPascal PRO for 8051 includes a library for comfortable work with MCU's internal EEPROM.

Note: EEPROM Library functions implementation is MCU dependent, consult the appropriate MCU datasheet for details about available EEPROM size and address range.

Library Routines

EEPROM_Read

Prototype

function EEPROM_Read(address: word): byte;

Returns

Byte from the specified address.

Description

Reads data from specified address.

Parameters :

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

Requires

Nothing.

Example
var eeAddr : word;
temp : byte;
...
eeAddr := 2
temp := EEPROM_Read(eeAddr);

EEPROM_Write

Prototype

function EEPROM_Write(address: word; wrdata: byte): byte;

Returns

  • 0 writing was successful
  • 1 if error occured

Description

Writes wrdata to specified address.

Parameters :

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

Note: Specified memory location will be erased before writing starts.

Requires

Nothing.

Example
var eeWrite : byte = 0x55;
    wrAddr : word = 0x732;
...
eeWrite := 0x55;
wrAddr := 0x732;
EEPROM_Write(wrAddr, eeWrite);

EEPROM_Write_Block

Prototype

function EEPROM_Write_Block(address: word; var ptrdata: byte): byte;

Returns

  • 0 writing was successful
  • 1 if error occured

Description

Writes one EEPROM row (32 bytes block) of data.

Parameters :

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

Note: Specified memory block will be erased before writing starts.

Requires

EEPROM module must support block write operations.

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 : word;
iArr : string[16];
...
wrAddr : 0x0100;
iArr := 'mikroElektronika';
EEPROM_Write_Block(wrAddr, iArr);

Library Example

This example demonstrates using the EEPROM Library with AT89S8253 MCU.

First, some data is written to EEPROM in byte and block mode; then the data is read from the same locations and displayed on P0, P1 and P2.


program EEPROM;
var dat : array [32] of byte;         // Data buffer, loop variable
    i : byte;

begin

  P0 := 0;
  P1 := 0;
  P2 := 0;

  for ii := 31 downto dat[ii] do
        dat[i] := i;                  // Fill data buffer   

  EEPROM_Write(2,0xAA);               // Write some data at address 2
  EEPROM_Write(0x732,0x55);           // Write some data at address 0x732
  EEPROM_Write_Block(0x100,dat);      // Write 32 bytes block at address 0x100

  Delay_ms(1000);                     // Blink P0 and P1 diodes
  P0 := 0xFF;                         //   to indicate reading start
  P1 := 0xFF;
  Delay_ms(1000);
  P0 := 0x00;
  P1 := 0x00;
  Delay_ms(1000);

  P0 := EEPROM_Read(2);                // Read data from address 2 and display it on PORT0
  P1 := EEPROM_Read(0x732);            // Read data from address 0x732 and display it on PORT1
  Delay_ms(1000);

  for ii := 0 to 31 do                 // Read 32 bytes block from address 0x100
    begin
      P2 := EEPROM_Read(0x100+ii);     //   and display data on PORT2
      Delay_ms(500);
    end;
end.
Copyright (c) 2002-2013 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