Based on this article
'http://www.hobbytronics.co.uk/arduino-external-eeprom
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 600
#End Region
Sub Process_Globals
Dim bc As ByteConverter
Private wire As WireMaster
Dim I2Address As Byte = 0x57 ' 1010 A2 A1 A0 1010 111 = 57 ZS -042
Dim StoreLocation As UInt =470
Dim StoreInt As Int = 54
Dim StoreValue As Byte = 0x38 ' Number 8
Public Serial1 As Serial
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
RunNative("SetWireClock", 400000) '400khz
wire.Initialize
Delay(1000)
For i= 32000 To 32050 Step 2
WriteExtEEPROMInt(I2Address,i,i)
Next
For i= 32000 To 32050 Step 2
Log(ReadExtEEPROMInt(I2Address,i))
Next
WriteExtEEPROMInt(I2Address,StoreLocation,StoreInt)
Delay(1000)
Log(ReadExtEEPROMInt(I2Address,StoreLocation))
Log("Finish")
End Sub
#if C
void SetWireClock(B4R::Object* o) {
Wire.setClock (o->toULong());
}
#end if
Sub WriteExtEEPROMInt (DeviceAddress As Int, EEAddress As UInt, Value As Int)
Dim Data(4) As Byte
Dim raf As RandomAccessFile
raf.Initialize(Data, True)
raf.WriteByte(Bit.ShiftRight(EEAddress, 8), raf.CurrentPosition)
raf.WriteByte(Bit.And(EEAddress, 0xff), raf.CurrentPosition)
raf.WriteInt16(Value,raf.CurrentPosition)
wire.WriteTo(DeviceAddress, Data)
Delay(5)
End Sub
Sub ReadExtEEPROMInt (DeviceAddress As Int, EEAddress As UInt) As Int
Dim b(2) As Byte
b(0) = ReadExtEEPROMByte (DeviceAddress ,EEAddress )
b(1) = ReadExtEEPROMByte (DeviceAddress ,EEAddress +1)
Dim x As Int = bc.IntsFromBytes(b)(0)
Return x
End Sub
Sub WriteExtEEPROMByte (DeviceAddress As Int, EEAddress As UInt, Value As Byte)
Dim Data(3) As Byte
Dim raf As RandomAccessFile
raf.Initialize(Data, True)
raf.WriteByte(Bit.ShiftRight(EEAddress, 8), raf.CurrentPosition)
raf.WriteByte(Bit.And(EEAddress, 0xff), raf.CurrentPosition)
raf.WriteByte(Value, raf.CurrentPosition)
wire.WriteTo(DeviceAddress, Data)
Delay(5)
End Sub
Sub ReadExtEEPROMByte (DeviceAddress As Int, EEAddress As UInt) As Byte
Dim Data(2) As Byte
Dim raf As RandomAccessFile
raf.Initialize(Data, True)
raf.WriteByte(Bit.ShiftRight(EEAddress, 8), raf.CurrentPosition)
raf.WriteByte(Bit.And(EEAddress, 0xff), raf.CurrentPosition)
wire.WriteTo(DeviceAddress, Data)
Delay(5)
Dim Answer() As Byte = wire.RequestFrom(DeviceAddress,1)
If Answer.Length>0 Then
Return Answer(0)
End If
End Sub