B4R Question External EEPROM

Discussion in 'B4R Questions' started by takhmin77, Feb 25, 2018.

  1. takhmin77

    takhmin77 Member

    hi
    i want to use external eeprom like AT24C02 for read & write
    is there any example for reading and writing ?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Do you have any Arduino C example for this?
     
  3. takhmin77

    takhmin77 Member

    hi
    My search resualt is :

    "You wire your chips on a breadboard, with the +/- left rail for Vcc/GND, and the right +/- rail for SCL/SDA, plus a little acrobatics to connect everything.
    Your chips can have 8 addresses, 0x50 to 0x57, set up with the A0/1/2 pins: all low = 0x00, all high = 0x07. The you read and write to the EEPROM of your choice with:"

    Code:
    char i2c_eeprom_read_char(int deviceaddress, unsigned int eeaddress) {
      char 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;
    }

    void i2c_eeprom_write_char(int deviceaddress, unsigned int eeaddress, char data) {
      int rdata = data;
      Wire.beginTransmission(deviceaddress);
      Wire.write((int)(eeaddress >> 8)); // MSB
      Wire.write((int)(eeaddress & 0xFF)); // LSB
      Wire.write(rdata);
      Wire.endTransmission();
    }

    and this link :
    http://playground.arduino.cc/Code/I2CEEPROM
    and also this :
    https://github.com/tardate/LittleAr...ster/playground/EEPROM/AT24C02/BasicReadWrite
     
    Last edited: Feb 26, 2018
  4. Erel

    Erel Administrator Staff Member Licensed User

    It should be quite simple to implement with rWire library.
     
  5. takhmin77

    takhmin77 Member

    hi
    how i can impliment this two line :

    #define eeprom 0x50

    Wire.write((int)(eeprom>> 8)); // MSB
    Wire.write((int)(eeprom& 0xFF)); // LSB

    ?
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Try this:
    Code:
    Dim bc As ByteConverter
    Dim msb As Int = Bit.ShiftRight(eeaddress, 8)
    Dim lsb As Int = Bit.And(eeaddress, 0xff)
    Dim data() As Byte = bc.IntsToBytes(Array As Int(msb, lsb))
    wire.WriteTo(
    address, data)
     
  7. takhmin77

    takhmin77 Member

    hi
    thanks for your reply
    i change my code to this code , but not any data to log:
    Code:
    Dim bc As ByteConverter
    Private wire As WireMaster
    Dim eeprom_Adress As Int = 0x50
    Dim Memory_Adress As UInt =1

    Dim msb As Int = Bit.ShiftRight(Memory_Adress, 8)
    Dim lsb As Int = Bit.And(Memory_Adress, 0xff)
    Dim data() As Byte = bc.IntsToBytes(Array As Int(msb, lsb))

    wire.WriteTo(eeprom_Adress, data)

    Dim data1() As Byte="A"
    wire.WriteTo(eeprom_Adress, data1)

    Delay(
    500)

    Dim msb As Int = Bit.ShiftRight(Memory_Adress, 8)
    Dim lsb As Int = Bit.And(Memory_Adress, 0xff)
    Dim data() As Byte = bc.IntsToBytes(Array As Int(msb, lsb))
    wire.WriteTo(eeprom_Adress, data)

    Dim b() As Byte = wire.RequestFrom(eeprom_Adress, 1)
    Log("data on address 1 : ",b)



    in arduino IDE , this code is work fine for me (return 65 ) :


    #include <Wire.h>
    #define eeprom 0x50 //defines the base address of the EEPROM

    void setup(void){
    Wire.begin();
    Serial.begin(9600);

    Serial.println("write");
    writeEEPROM(eeprom, 1, 'A');
    delay(5);
    Serial.print(readEEPROM(eeprom, 1), DEC);
    }

    void loop(){}

    void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) {
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddress >> 8)); //writes the MSB
    Wire.write((int)(eeaddress & 0xFF)); //writes the LSB
    Wire.write(data);
    Wire.endTransmission();
    }

    byte readEEPROM(int deviceaddress, unsigned int eeaddress ) {
    byte rdata = 0xFF;
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddress >> 8)); //writes the MSB
    Wire.write((int)(eeaddress & 0xFF)); //writes the LSB
    Wire.endTransmission();
    Wire.requestFrom(deviceaddress,1);
    if (Wire.available())
    rdata = Wire.read();
    return rdata;
    }
     
    Last edited: Jun 19, 2018
  8. Erel

    Erel Administrator Staff Member Licensed User

    I think that you confused Memory_Address and eeprom_Address.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice