Go Down

Topic: read and write on at24c16 with wire.h (Read 942 times) previous topic - next topic

saiona

Do you know what is Arduino UNO? Do you know what is IDE?

Please, post the 15 lines codes that you have written.
i think u cant help me ... thanks  for attention :)
if another person can help me pls write that code. i write my code here now that i write but dont work . :)

GolamMostafa

You are second person whom I have got in this Forum as a Game Player?

saiona

You are second person whom I have got in this Forum as a Game Player?
Im game player and you Edison !!!! Thanks for Help ...

GolamMostafa

#18
Jul 24, 2018, 01:35 pm Last Edit: Jul 24, 2018, 01:39 pm by GolamMostafa
We wanted to help you; but, you are playing game. The chance is still there to get help if you say that you have an Arduino UNO/NANO/MEGA/DUE Kit.

saiona

We wanted to help you; but, you are playing game. The chance is still there to get help if you say that you have an Arduino UNO/NANO/MEGA/DUE Kit.
i have uno and i write code for internal eeprom to write in uno but in external eeprom i cant write code because wire.write just give 1 argument but eeprom.write give 2 argument. and now if u can help me do it pls.

AWOL

#20
Jul 24, 2018, 01:44 pm Last Edit: Jul 24, 2018, 01:44 pm by AWOL
We wanted to help you; but, you are playing game. The chance is still there to get help if you say that you have an Arduino UNO/NANO/MEGA/DUE Kit.
Why? What difference will it make?
The OP is more likely to get help if they show some EFFORT.

GolamMostafa

#21
Jul 24, 2018, 01:50 pm Last Edit: Jul 24, 2018, 01:51 pm by GolamMostafa
i have uno and i write code for internal eeprom to write in uno but in external eeprom i cant write code because wire.write just give 1 argument but eeprom.write give 2 argument. and now if u can help me do it pls.
Please, post your codes.

saiona

Please, post your codes.
its a code that i write long with convert to byte . but i have problem in that .







#include <Wire.h>
#define ali B01100000
int addressd = 4;
long number = 555667778;

void eeprom_i2c_write(int address, long data) {
 
 
    Wire.beginTransmission(ali);
 
 
    Wire.write((int)(address));
 
 
    Wire.write((long)(data));
 
 
    Wire.endTransmission();
 
 
    delay(5);
    byte four = (data & 0xFF);
      byte three = ((data >> 8) & 0xFF);
      byte two = ((data >> 16) & 0xFF);
      byte one = ((data>> 24) & 0xFF);
     
     Wire.write(address, four);
     Wire.write(address + 1, three);
      Wire.write(address + 2, two);
      Wire.write(address + 3, one);
     

   
    }





long eeprom_i2c_read(int address) {
   Wire.requestFrom(ali, 1);
  long four = Wire.read(address);
  Wire.requestFrom(ali, 1);
      long three = Wire.read(address + 1);
      Wire.requestFrom(ali, 1);
      long two = Wire.read(address + 2);
      Wire.requestFrom(ali, 1);
      long one = Wire.read(address + 3);


      return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);

}
 void setup()
      {
      Serial.begin(9600);
     
      }
void loop()
      {
       
        delay(5000);

     
      long address=0;

     
      eeprom_i2c_read(address, number);
      address+=4;

      Serial.println("If numbers are equals, it's working !");
      Serial.print(number1);
     
     
      Serial.println(eeprom_i2c_read(0));
      }







and this is the code i write for used internal eeprom :







#include <EEPROM.h>

long number1 = 1000000009;
long number2 = 987654321;


void EEPROMWritelong(int address, long value)
      {

      byte four = (value & 0xFF);
      byte three = ((value >> 8) & 0xFF);
      byte two = ((value >> 16) & 0xFF);
      byte one = ((value >> 24) & 0xFF);

 
      EEPROM.write(address, four);
      EEPROM.write(address + 1, three);
      EEPROM.write(address + 2, two);
      EEPROM.write(address + 3, one);
      }


long EEPROMReadlong(long address)
      {
 
      long four = EEPROM.read(address);
      long three = EEPROM.read(address + 1);
      long two = EEPROM.read(address + 2);
      long one = EEPROM.read(address + 3);

   
      return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
      }

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

void loop()
      {

      delay(5000);


      long address=0;

 
      EEPROMWritelong(address, number1);
      address+=4;

      EEPROMWritelong(address, number2);
      address+=4;


      Serial.println("If numbers are equals, it's working !");
      Serial.print(number1);
      Serial.print(" and ");
      //Reading and sending first long.
      Serial.println(EEPROMReadlong(0));

      Serial.print(number2);
      Serial.print(" and ");

      Serial.println(EEPROMReadlong(4));
      }

saiona

Why? What difference will it make?
The OP is more likely to get help if they show some EFFORT.
can u help me?

GolamMostafa

#24
Jul 24, 2018, 05:49 pm Last Edit: Jul 24, 2018, 07:17 pm by GolamMostafa
Finally, you have posted your codes. This is fine. Before you proceed to store data into your 24C16 EEPROM, the 8-pins of the chip must be terminated properly. Can you post the connection diagram of your memory, which will show the connections of the following signals of the chip.

Vcc
GND

PB0
PB1

Mode

PRE

SDA
SCL

Assuming that you have read the data sheets, and you have correctly terminated the signal/power lines of the memory chip.

Let us try to write 0x23 in the location 0x0010 of the Block-0 of the memory chip. You know that there are 8 blocks in the 24C16 chip, and each block is composed of 256 locations. The address of the Block-0 space is: 0x50.

Untested Codes:
Code: [Select]
#include<Wire.h>

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

   Wire.beginTransmission(0x50);
   Wire.write(0x00);
   Wire.write(0x10);
   Wire.write(0x23);
   Wire.endTransmission();

   delay(10);

   //read back data and show on Serial Monitor
   Wire.beginTransmission(0x50);
   Wire.write(0x00);
   Wire.write(0x10);
   Wire.endTransmission();

   Wire.requestFrom(0x50, 1);
   byte x = Wire.read();
   Serial.print(x, HEX);   //should show: 23
}

void loop()
{

}

el_supremo

Quote
Let us try to write 0x23 in the location 0x0010
Your code doesn't do that.
The three low-order bits of the I2C address (0x50) specify which page will be read or written. The following byte that is written specifies where in that page the R/W will take place. You are writing two bytes for an address when only one is required.
Writing 0x23 into location 0x10 of page zero requires this code:
Code: [Select]
   Wire.beginTransmission(0x50);
   Wire.write(0x10);
   Wire.write(0x23);
   Wire.endTransmission();

The same addressing is required when setting up a read operation.

Pete
Don't send me technical questions via Private Message.

GolamMostafa

#26
Jul 24, 2018, 07:01 pm Last Edit: Jul 24, 2018, 07:16 pm by GolamMostafa
I think you are right.

I also initially thought like what you have said -- because memory blocks (I prefer to call block as per data sheets as there is something like page size of 16 bytes) are clearly defined by the lower three bits of the 7-bit address, 0x10 is good enough to refer to the memory location of the current block, and there is no need to place 0x0010 for the address. Later on, I was confused by the following diagram (addresses are shown as 0x0XXX) of the data sheets. (If I would have the chip, I would certainly do the experiment before making the post.)


saiona

Finally, you have posted your codes. This is fine. Before you proceed to store data into your 24C16 EEPROM, the 8-pins of the chip must be terminated properly. Can you post the connection diagram of your memory, which will show the connections of the following signals of the chip.

Vcc
GND

PB0
PB1

Mode

PRE

SDA
SCL

Assuming that you have read the data sheets, and you have correctly terminated the signal/power lines of the memory chip.

Let us try to write 0x23 in the location 0x0010 of the Block-0 of the memory chip. You know that there are 8 blocks in the 24C16 chip, and each block is composed of 256 locations. The address of the Block-0 space is: 0x50.

Untested Codes:
Code: [Select]
#include<Wire.h>

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

   Wire.beginTransmission(0x50);
   Wire.write(0x00);
   Wire.write(0x10);
   Wire.write(0x23);
   Wire.endTransmission();

   delay(10);

   //read back data and show on Serial Monitor
   Wire.beginTransmission(0x50);
   Wire.write(0x00);
   Wire.write(0x10);
   Wire.endTransmission();

   Wire.requestFrom(0x50, 1);
   byte x = Wire.read();
   Serial.print(x, HEX);   //should show: 23
}

void loop()
{

}


its the code for write one byte on eeprom . i want write 4 byte in eeprom with wire.h . thanks

GolamMostafa

Check that you are successful in writing 1-byte data into the EEPROM and then extend the concept up to 4-byte.

Please take note of the code correction/adjustment made by @el_supremo.

saiona

I think you are right.

I also initially thought like what you have said -- because memory blocks (I prefer to call block as per data sheets as there is something like page size of 16 bytes) are clearly defined by the lower three bits of the 7-bit address, 0x10 is good enough to refer to the memory location of the current block, and there is no need to place 0x0010 for the address. Later on, I was confused by the following diagram (addresses are shown as 0x0XXX) of the data sheets. (If I would have the chip, I would certainly do the experiment before making the post.)


Code: [Select]
#include <Wire.h>
#define ali B01100000
int addressd = 4;
long number = 555667778;

void eeprom_i2c_write(int address, long data) {
 
 
    Wire.beginTransmission(ali);
 
 
    Wire.write((int)(address));
 
 
    Wire.write((long)(data));
 
 
    Wire.endTransmission();
 
 
    delay(5);
    byte four = (data & 0xFF);
      byte three = ((data >> 8) & 0xFF);
      byte two = ((data >> 16) & 0xFF);
      byte one = ((data>> 24) & 0xFF);
     
     Wire.write(address, four);
     Wire.write(address + 1, three);
      Wire.write(address + 2, two);
      Wire.write(address + 3, one);
     

   
    }





long eeprom_i2c_read(int address) {
   Wire.requestFrom(ali, 1);
  long four = Wire.read(address);
  Wire.requestFrom(ali, 1);
      long three = Wire.read(address + 1);
      Wire.requestFrom(ali, 1);
      long two = Wire.read(address + 2);
      Wire.requestFrom(ali, 1);
      long one = Wire.read(address + 3);


      return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);

}
 void setup()
      {
      Serial.begin(9600);
     
      }
void loop()
      {
       
        delay(5000);

     
      long address=0;

     
      eeprom_i2c_read(address, number);
      address+=4;

      Serial.println("If numbers are equals, it's working !");
      Serial.print(number1);
     
     
      Serial.println(eeprom_i2c_read(0));
      }


this code give 1 byte to me . my problem is change it to 4 byte. say this.

Go Up