B4R Question I2C External EEPROM - rwire

rodmcm

Active Member
Licensed User
I noticed that other questioners on this either never reported success or reverted to inline C

Enclosed is a B4r interpretation of the Arduino method for a single byte store and return to External EEPROM. I tested with I2C scan and the EEPROM does exist!

www.hobbytronics.co.uk/arduino-external-eeprom

Irrespective of the value or location I place the byte of data the return always is the same (3). I changed the program from reading one byte request to reading 10 and printed out and get the surprising answer
3,4,5,6,7,8,9,10,11,12

Has anyone successfully read to an external memory chip with B4r? Assistance required please
 

Attachments

  • ExtEEPROM.zip
    1.2 KB · Views: 365

rodmcm

Active Member
Licensed User
yes
I have used this method for a long time ago for arduino and ESP projects. Some commercial. I am porting them to b4r mainly due to the security of B4r serialization.

The only difference I can see between the arduino and what I have written in B4r is the method of writing for the location. The arduino writes the MSB and the LSB as two separate messages.


//Write to the EEPROM
//========================================================================
void WriteEEPROM(int deviceaddress, unsigned int eeaddress, byte data )
{
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write(data);
Wire.endTransmission();
delay(5);
}

//Read From the EEPROM
//========================================================================
byte ReadEEPROM(int deviceaddress, unsigned int eeaddress )
{
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
if (Wire.available()) rdata = Wire.read();
return rdata;
}
 
Upvote 0

rodmcm

Active Member
Licensed User
I have just tried the single writes of MSB and LSB as well and the answers are
81,82,83,84,85,86,87,88,89,90
 
Upvote 0

rodmcm

Active Member
Licensed User
had another go
Wrote and verified 20 random bytes (Arduino1)
Ran B4r and extracted 10 bytes that do no match the inserted values (B4r1)
With Arduino printed out the first 20 bytes again (Arduino2)

Low and behold the new bytes are not the same as those written originally
But the 10 extracted bytes by B4r appear on the new list from location 2 down!

My brain hurts, going to bed
 

Attachments

  • Arduino 2.JPG
    Arduino 2.JPG
    19.9 KB · Views: 333
  • Arduino1.JPG
    Arduino1.JPG
    30.2 KB · Views: 380
  • B4r1.JPG
    B4r1.JPG
    11.6 KB · Views: 315
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this:
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private wire As WireMaster
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   wire.Initialize   
End Sub

Sub WriteEEPROM (DeviceAddress As Int, EEAddress As UInt, Value As Byte) 
   Dim Data(5) As Byte
   Dim raf As RandomAccessFile
   raf.Initialize(Data, True)
   raf.WriteInt16(Bit.ShiftRight(EEAddress, 8), raf.CurrentPosition)
   raf.WriteInt16(Bit.And(EEAddress, 0xff), raf.CurrentPosition)
   raf.WriteByte(Value, raf.CurrentPosition)
   wire.WriteTo(DeviceAddress, Data)
End Sub
 
Upvote 0

rodmcm

Active Member
Licensed User
No, unfortunately..

I filled the first 20 ext EEProm memory locations with bytes 1 to 20. Then ran my original read and this 'read' based on the methods of your 'write'

B4X:
Sub ReadExtEEPROM (DeviceAddress As Int, EEAddress As UInt) As Byte
    Dim Data(5) As Byte
    Dim raf As RandomAccessFile
    raf.Initialize(Data, True)
    raf.WriteInt16(Bit.ShiftRight(EEAddress, 8), raf.CurrentPosition)
    raf.WriteInt16(Bit.And(EEAddress, 0xff), raf.CurrentPosition)
    wire.WriteTo(DeviceAddress, Data)
    Delay(5)
    Dim Answer() As Byte = wire.RequestFrom(DeviceAddress,10)      ' just for fun
    Log("Answer Length  ",Answer.Length)
    If Answer.Length>0 Then
        For i=0 To Answer.Length-1
            Log(Answer(i))
        Next
        Return Answer(0)
    End If
End Sub

in both cases when I then read the 20 positions with Arduino
0 = EEadress (tested by changing EEaddress a few times)
1=0
2=0
3=4
4=5 etc

The 10 read locations were always from the 3rd memory location
4,5,6,7,8,9,10,11,12,13

This has never changed
The write value was nowhere to me seen.. I suspect in 0,1, or 2 locations and overwritten
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
There's something possibly wrong in that code for a 24C256. The use of WriteInt16 (should be writeByte) to set the address adds 2 unnecessary bytes (only 2 are needed) to the array and they are written to it. When reading, 5 bytes are sent to set the addres instead of 2, so chip internally increments this address by 3 and starts reading the 4

Not tested, but the correct approach should be something like this :)
B4X:
Sub WriteEEPROM (DeviceAddress As Int, EEAddress As UInt, Value As Byte) 
   Dim Data(3) As Byte    <-- Will be 2 + data length
   Dim raf As RandomAccessFile
   raf.Initialize(Data, True)
   'raf.WriteInt16(Bit.ShiftRight(EEAddress, 8), raf.CurrentPosition)   '<- writing 2 bytes each time
   'raf.WriteInt16(Bit.And(EEAddress, 0xff), raf.CurrentPosition)
    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)
End Sub

Sub ReadExtEEPROM (DeviceAddress As Int, EEAddress As UInt) As Byte
    Dim Data(2) As Byte                  '<-- 2 bytes instead of 5, to set the address
    Dim raf As RandomAccessFile
    raf.Initialize(Data, True)
    ' raf.WriteInt16(Bit.ShiftRight(EEAddress, 8), raf.CurrentPosition)
    ' raf.WriteInt16(Bit.And(EEAddress, 0xff), raf.CurrentPosition)
    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,10)      ' just for fun
    Log("Answer Length  ",Answer.Length)
    If Answer.Length>0 Then
        For i=0 To Answer.Length-1
            Log(Answer(i))
        Next
        Return Answer(0)
    End If
End Sub
 
Upvote 0

rodmcm

Active Member
Licensed User
Well that worked!! Well spotted JordiCP I tested random locations up to 32000 on the 24LC256 (32K)
As I was doing a read straight after a write I got some funny answers until I included a delay after the write

So for all of you out there, with thanks to Erel and JordiCP
B4X:
Sub WriteExtEEPROM (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 ReadExtEEPROM (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
 
Upvote 0
Top