EEPROM Library

EEPROM data memory is available with a number of 8051 family. The mikroC 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 constrains.

Library Routines

EEPROM_Read

Prototype

unsigned short EEPROM_Read(unsigned int address);

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
unsigned int eeAddr = 2;
unsigned short temp;
...
temp = EEPROM_Read(eeAddr);

EEPROM_Write

Prototype

unsigned short EEPROM_Write(unsigned int address, unsigned short wrdata);

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
unsigned short  eeWrite = 0x55;
unsigned int  wrAddr = 0x732;
...
EEPROM_Write(wrAddr, eeWrite);

EEPROM_Write_Block

Prototype

unsigned short EEPROM_Write_Block(unsigned int address, unsigned short *ptrdata);

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
unsigned int  wrAddr = 0x0100;
unsigned short iArr[32] = {'m', 'i', 'k', 'r', 'o', 'E', 'l', 'e', 'k', 't', 'r', 'o', 'n', 'i', 'k', 'a', 0};
...
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.

char buffer[32], ii;                   // Data buffer, loop variable

void main(){

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

  for(ii = 31; buffer[ii] = ii; ii--) // 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, buffer);  // 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; ii < 32; ii++) {        // Read 32 bytes block from address 0x100
    P2 = EEPROM_Read(0x100+ii);       //   and display data on PORT2
    Delay_ms(250);
 }
}
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