EEPROM Library
EEPROM data memory is available with a number of 8051 family. The mikroBasic 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 |
sub function EEPROM_Read(dim address as word) as byte |
|---|---|
| Returns |
Byte from the specified address. |
| Description |
Reads data from specified Parameters :
|
| Requires |
Nothing. |
| Example |
dim eeAddr as word temp as byte ... eeAddr = 2 temp = EEPROM_Read(eeAddr) |
EEPROM_Write
| Prototype |
sub function EEPROM_Write(dim address as word, dim wrdata as byte) as byte |
|---|---|
| Returns |
|
| Description |
Writes Parameters :
Note: Specified memory location will be erased before writing starts. |
| Requires |
Nothing. |
| Example |
dim eeWrite as byte = 0x55
wrAddr as word = 0x732
...
eeWrite = 0x55
wrAddr = 0x732
EEPROM_Write(wrAddr, eeWrite)
|
EEPROM_Write_Block
| Prototype |
sub function EEPROM_Write_Block(dim address as word, dim byref ptrdata as byte) as byte |
|---|---|
| Returns |
|
| Description |
Writes one EEPROM row (32 bytes block) of data. Parameters :
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 |
dim wrAddr as word iArr as 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
dim dat as byte[32] ' Data buffer, loop variable
i,tmp as byte
main:
P0 = 0
P1 = 0
P2 = 0
for i = 31 to 0
dat[i] = i ' Fill data buffer
next i
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 i = 0 to 31 ' Read 32 bytes block from address 0x100
tmp = EEPROM_Read(0x100+i)
P2 = not tmp ' and display data on PORT2
Delay_ms(100)
next i
end.
What do you think about this topic ? Send us feedback!



