Go Down

Topic: Writing/Reading to and from Eeprom I2C (Read 80 times) previous topic - next topic

cajodk

Hi,

I'm working on a project (but I don't have the setup here at the time), so I'm wondering if I did this correctly - and hopefully one of the helpful people in this forum can guide me in the right direction if I'm wrong.

What I want to do, is to send data (a byte) to an Eeprom, and then I'd like to read from it again. This bit of code is using the Rom as more of a passthrough than a data storage unit. Meaning that what I send to it, should be read initially afterwards.

Can someone please tell me if Im on the right track here? :) The thing I'm mostly questioning is the part where I read the data from the Eeprom.


Code: [Select]
void Eeprom(void) {
//Writing data to the Eeprom
  Wire.beginTransmission(eeprom);                 //Calling the Eeprom at the Eeprom address
  Wire.write(high);                               //Telling the Eeprom we want to access the High address
  Wire.write(low);                                //Telling the Eeprom we want to access the Low address
  Wire.write(byte(incomingtemp));                 //Writing the incoming temp data to Eeprom
   //Serial.println(incomingtemp);

  Wire.endTransmission();                         //Stop transmitting to the Eeprom
  delay(500);                                     //Inserting a 500 ms delay

//Reading data from the Eeprom
  Wire.beginTransmission(eeprom);                 //Calling the Eeprom again
  Wire.write(high);                               //Telling the Eeprom we want to access the High address
  Wire.write(low);                                //Telling the Eeprom we want to access the Low address
  Wire.endTransmission();                         //Ending transmission with the Eeprom
  char readdata = Wire.requestFrom(eeprom, 1);    //Requesting one byte from the Eeprom address
  Serial.println(readdata);
  delay(500);                                     //Inserting a 500 ms delay
    shelf++;                                      //Count "shelf" one up
  if (shelf > 255) shelf = 0;                     //When "shelf" 255 is reached, reset "shelf"
                                                  //and start writing to/reading from address 0 again
  if (low == 255) high++;                         //If low address reaches 255, count high address 1 up
  low++;                                          //Count low address one up to prepare for next writing/reading
}//eeprom

el_supremo

requestFrom only tells the device how many bytes you want to read. To read the data, you have to follow this with
Code: [Select]
readdata = Wire.read();

You would probably find it easier to address the byte by using a 16-bit integer instead of two bytes.
Code: [Select]

uint16_t i2c_address = 0;
.
.
.
  Wire.write(i2c_address >> 8);
  Wire.write(i2c_address & 0xff);
.
.
.
  // incrementing the address is then trivial
  i2c_address++;
.
.

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

cajodk

Thanks. :)

I'll stick with the high and low though, at least for now.. I understand it a bit better that way.

However, in the read part of the code.. is this the correct way otherwise?

Code: [Select]

//Reading data from the Eeprom
  Wire.beginTransmission(eeprom);                 //Calling the Eeprom again
  Wire.write(high);                               //Telling the Eeprom we want to access the High address
  Wire.write(low);                                //Telling the Eeprom we want to access the Low address
 Wire.endTransmission();                         //Ending transmission with the Eeprom
  Wire.requestFrom(eeprom, 1);    //Requesting one byte from the Eeprom address
char readdata = Wire.read(); 
Serial.println(readdata);
  delay(500);                                     //Inserting a 500 ms delay
    shelf++;                                      //Count "shelf" one up


I am particularly wondering about the first 4 lines of code, where I tell the Eeprom I want to access the high and low bytes - and then ending the transmission BEFORE actually requesting and reading my byte of data. Is this the correct way to do this? Cause it seems a bit weird and not very logic. Should the Wire.endTransmission not occur only after Wire.read?

el_supremo

#3
May 25, 2016, 09:20 pm Last Edit: May 26, 2016, 11:37 pm by el_supremo
What actually happens in this sequence:
Code: [Select]
  Wire.beginTransmission(eeprom);
  Wire.write(high);
  Wire.write(low);
 Wire.endTransmission();

is that beginTransmission initiates a write sequence to the device. This is followed by up to 32 Wire.write() (or whatever the buffer size is in the library). These writes do not occur immediately. The data is just stored in the library's buffer. The call to endTransmission then initiates the real write sequence to the device - in this case two bytes are written.
When you want to read from the device, you have to write the address to it first so you use the above sequence. Then you use requestFrom to specify how many bytes you want to read starting at that address. This must then be followed by a Wire.read() for each of the bytes to be read.

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy