EEPROM Library

EEPROM data memory is available with a number of dsPIC30 family and some PIC24 family MCU's. The mikroBasic 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

sub procedure EEPROM_Erase(dim address as 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
dim eeAddr as 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

sub procedure EEPROM_Erase_Block(dim address as 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
dim eeAddr as 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

sub function EEPROM_Read(dim address as longint) as 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
dim eeAddr as longint
      temp as word
...
eeAddr = 0x7FFC20
temp = EEPROM_Read(eeAddr)
Notes

None.

EEPROM_Write

Prototype

sub procedure EEPROM_Write(dim address as longint, dim data_ as 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
dim wrAddr as longint
    eeData as word
  ...
  eeData = 0xAAAA
  wrAddr = 0x7FFC30
  EEPROM_Write(wrAddr, eeData)
Notes

Specified memory location will be erased before writing starts.

EEPROM_Write_Block

Prototype

sub procedure EEPROM_Write_Block(dim address as longint, dim byref data_ as word[100])

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
dim  wrAddr as longint
     data as 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
dim eeData, i as word
    eeAddr as longword
    dArr as word[16]

main:

  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
    Inc(eeData)
    while (WR_bit)                     ' Wait for write to finish,
      nop
    wend
      
    LATB = Eeprom_Read(eeAddr)         ' then, read the just-written data.
    eeAddr = eeAddr + 2                ' Next address of EEPROM memory location

    Delay_ms(100)
  wend

  Delay_ms(1000)                       ' Wait 1 second.
  
  eeData = 0xAAAA
  for i = 0 to 1                       ' Initializing array of 16 integers with data
    dArr[i] = eeData
    eeData  = not eeData
  next i

  Eeprom_Write_Block(0x7FFC20, dArr)   ' Write entire row of EEPROM data
  while(WR_bit)                        ' Wait for write to finish
    nop
  wend
    

  eeAddr = 0x7FFC20                    ' Address of EEPROM where reading should start
  for i = 0 to 15                      ' Read the data back
    LATB = Eeprom_Read(eeAddr)         ' and show it on PORTB
    eeAddr = eeAddr + 2                ' Next address of EEPROM memory location
    Delay_ms(500)
  next i
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