EEPROM Library

EEPROM data memory is available with a number of PIC MCUs. mikroBasic PRO for PIC includes library for comfortable work with EEPROM.

Library Routines

EEPROM_Read

Prototype

// for PIC16
sub function EEPROM_Read(dim Address as byte) as byte

// for PIC18
sub function EEPROM_Read(dim Address as word) as byte

Returns

Returns byte from specified address.

Description

Reads data from specified address. Parameter address is MCU dependent; for PIC16 family it is of byte type, and for PIC18 family it is of word type. For PIC18 MCUs with more EEPROM data locations, it is programmer’s responsibility to set SFR EEADRH register appropriately.

Requires

Requires EEPROM module.

Ensure minimum 20ms delay between successive use of routines EEPROM_Write and EEPROM_Read. Although PIC will write the correct value, EEPROM_Read might return an undefined result.

Example
tmp = EEPROM_Read($3F)

EEPROM_Write

Prototype

// for PIC16
sub procedure EEPROM_Write(dim Address as byte, dim Data as byte)

// for PIC18
sub procedure EEPROM_Write(dim Address as word, dim Data as byte)

Returns

Nothing.

Description

Writes data to specified address. Parameter address is MCU dependent; for PIC16 family it is of byte type, and for PIC18 family it is of word type. For PIC18 MCUs with more EEPROM data locations, it is programmer’s responsibility to set SFR EEADRH register appropriately.

Be aware that all interrupts will be disabled during execution of EEPROM_Write routine (GIE bit of INTCON register will be cleared). Routine will set this bit on exit.

Requires

Requires EEPROM module.

Ensure minimum 20ms delay between successive use of routines EEPROM_Write and EEPROM_Read. Although PIC will write the correct value, EEPROM_Read might return an undefined result.

Example
EEPROM_Write($32, $AA)

Library Example

The example writes values at 20 successive locations of EEPROM. Then, it reads the written data and prints on PORTB for a visual check.

Copy Code To ClipboardCopy Code To Clipboard
program Eeprom

dim counter as byte                     ' loop variable

main:
  ANSEL =  0                            ' Configure AN pins as digital I/O
  ANSELH = 0
  C1ON_bit = 0                          ' Disable comparators
  C2ON_bit = 0

  PORTB = 0
  PORTC = 0
  PORTD = 0

  TRISB = 0
  TRISC = 0
  TRISD = 0
   
  for counter = 0 to 31                 ' Fill data buffer
    EEPROM_Write(0x80+counter, counter) ' Write data to address 0x80+ii
  next counter

  EEPROM_Write(0x02,0xAA)               ' Write some data at address 2
  EEPROM_Write(0x50,0x55)               ' Write some data at address 0150

  Delay_ms(1000)                        ' Blink PORTB and PORTC diodes
  PORTB = 0xFF                          '   to indicate reading start
  PORTC = 0xFF
  Delay_ms(1000)
  PORTB = 0x00
  PORTC = 0x00
  Delay_ms(1000)

  PORTB = EEPROM_Read(0x02)             ' Read data from address 2 and display it on PORTB
  PORTC = EEPROM_Read(0x50)             ' Read data from address 0x50 and display it on PORTC

  Delay_ms(1000)
  
  for counter = 0 to 31                 ' Read 32 bytes block from address 0x100
    PORTD = EEPROM_Read(0x80+counter)   '   and display data on PORTC
    Delay_ms(100)
  next counter
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