I2C Eeprom Unit

This library unit implements commands to read from and write to an I2C eeprom. Two commands handle byte read/writes, while two handle string read/writes.

The library unit itself automatically calls upon the software I2C library unit.

Compilation Constants


This library unit proper requires no compilation constants, but obviously the I2C unit called by it does. Here's an example for the PIC16F88:

{$DEFINE SDA_PORT 'PortB'}    // I2C SDA port
{$DEFINE SDA_PIN 0}           // and its pin number
{$DEFINE SCL_PORT 'PortB'}    // I2C SCL port
{$DEFINE SCL_PIN 1}           // and its pin number


Commands


There are four commands. The first two are used to write  and read single bytes to and from the I2C eeprom.  The byte oriented commands are obvious:

procedure byteWrite(device : byte; address : word; value : byte);
Write a single byte to an address on a particular device.

procedure byteRead(device : byte; address : word; var value : byte);
read a single byte from an address on a particular device.

And here are the string oriented commands, A few comments will follow.

procedure stringWrite(device, pagesize : byte; address : word; str1 : string); Write string to eeprom, starting at specified address.

procedure stringRead(device : byte; address : word; var str1 : string);
Read an entire string from eeprom -- be sure to set str1[0]  to the number of bytes desired before calling the procedure.

With regard to the stringWrite() procedure, note that  you must also pass in the page size of the device,  since page-writes are used for fastest operation. This  has been tested on the AT24C32 and 24LC256 which  have page sizes of 32 and 64 bytes, respectively.

Before calling the stringRead() procedure, set  the length of the string to the number of characters  you wish to read into it from the eeprom. For example,  if the string to be filled is called Test and you  wish to fetch 10 characters, then execute

Test[0] := 10;

immediately before passing in Test.

There is no error detection. It is the duty of the  calling program to specify valid addresses within  the eeprom and to stay within the string size specified  at
compile time.

Add It to Your Library Collection


Go fetch the library unit "I2CEEPROM.pas" for your setup now and store it in your Common directory.


Be sure to read over the source code for additional details on how to use it.

Try It Out


If you'd like to see it in operation, then head over to the exercises to find the I2C Eeprom exercise. Click here to go to it.

No comments:

Post a Comment