From Swordfish Wiki

SwordfishUser: I2CEEPROM

Download i2cEEPROM v1.0

Overview

This module provides support for I2C serial EEPROMs.

Supported I2C device types:

Features Include:

Example Usage:

//
// i2cEEPROM example
//
device = 18F26K22
clock = 64

include "intOSC.bas"
include "setdigitalio.bas"

// select interface (I2C, I2C2, or SI2C)
#option I2C_EE_INTERFACE = I2C
// set eeprom type
#option I2C_EE_TYPE = 24xx08
// define eeprom WP pin connection (optional)
'#option I2C_EE_WP = PORTC.2
// set any addtl options for i2c modules here
include "i2cEEPROM.bas"

//  
dim wb, rb as byte
dim wbuf(256) as byte
dim rbuf(256) as byte
dim i as word
dim LED as PORTA.4


// set all pins to digital
SetAllDigital()

// init interface
// if using the MSSP you can set speed (defaults to 100KHz)
#if (I2C_EE_INTERFACE = I2C) or (I2C_EE_INTERFACE = I2C2)
  i2cEE.Initialize(I2C_400_KHZ)
#else
  i2cEE.Initialize()
#endif

// check to see if device is detected
i2cEE.SetDevice(0)          // optional if only one device
if (i2cEE.IsPresent()) then
    high(LED)   // device recognized (ACKs its address)
else
    low(LED)    // device NOT recognized
endif

// if you have multiple devices on the bus then call SetDevice
// to switch back and forth
i2cEE.SetDevice(1)
if (i2cEE.IsPresent()) then
    high(LED)   // device recognized (ACKs its address)
else
    low(LED)    // device NOT recognized
endif

// enable eeprom write (if used)
#if isoption(I2C_EE_WP)
i2cEE.WriteProtect(WP_OFF)
#endif

// work with the first device
i2cEE.SetDevice(0)

// write and read a byte to address 0
wb = $AA
i2CEE.Write(0, wb)
i2cEE.Read(0, rb)
if (rb <> wb) then  // verify
    low(LED)        // no match... write failed
endif

// fill eeprom with $55
// if unspecified, default is to fill with $FF (ie erase)
i2cEE.Fill($55)

// read 256 bytes starting at 0
// should be $55 (from Fill())
i2cEE.SetAddress(0)
i2cEE.ReadArray(rbuf, sizeof(rbuf))

// fill write buffer with incrementing pattern
for i = 0 to bound(wbuf)
    wbuf(i) = i
next

// write wbuf array to chip starting at addr 10
i2cEE.Write(10, wbuf)
// read data just written into rbuf
i2cEE.Read(10, rbuf)

// if the chip supports page writes then it's usually much faster 
// to use that when writing a large array
i2cEE.Fill()
i2cEE.WritePage(10, wbuf, sizeof(wbuf))
// read data just written into rbuf
i2cEE.Read(10, rbuf)
Retrieved from http://www.sfcompiler.co.uk/wiki/pmwiki.php?n=SwordfishUser.I2CEEPROM
Page last modified on October 21, 2018, at 02:56 PM