Go Down

Topic: 24C01 Read data from a specific address (Read 286 times) previous topic - next topic

Geforcer

Jul 15, 2015, 01:06 am Last Edit: Jul 15, 2015, 03:30 pm by Geforcer
Hello guys, I am a noob with Arduino and I need your help with reading from 24C01 EEPROM. I have Arduino Duemilanove, if that matters for this operation.


I found this code on Fritzing:

Quote
#include <Wire.h>

void eeprom_i2c_write(byte address, byte from_addr, byte data) {
  Wire.beginTransmission(address);
  Wire.send(from_addr);
  Wire.send(data);
  Wire.endTransmission();
}

byte eeprom_i2c_read(int address, int from_addr) {
  Wire.beginTransmission(address);
  Wire.send(from_addr);
  Wire.endTransmission();

  Wire.requestFrom(address, 1);
  if(Wire.available())
    return Wire.receive();
  else
    return 0xFF;
}

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

  for(int i = 0; i < 10; i++) {
    eeprom_i2c_write(B01010000, i, 'a'+i);
    delay(100);
  }

  Serial.println("Writen to memory!");
}

void loop() {
  for(int i = 0; i < 10; i++) {
    byte r = eeprom_i2c_read(B01010000, i);
    Serial.print(i);
    Serial.print(" - ");
    Serial.print(r);
    Serial.print("\n");
    delay(1000);
  }
}
How to modify this code to read from two specific addresses on 24C01 - 0x36 and 0x37 ?


Thank you in advance!

PaulS

Quote
How to modify this code to read from two specific addresses on 24C01 - 030 6h and 030 7h ?
Those "addresses" do not make sense. 030 is an octal value. 6h and 7h are in what base? Not octal, not hex, and not decimal.

Geforcer

Hey PaulS. Thanks for your reply. I am sorry for my noob-ism. :)

Let me explain like this, so maybe you could help me out somehow... I need to read (dump) a 24C01 EEPROM from a device. The data I really need is found in the 6th and 7th column of the 030 address when looked in the Hex editor.


PaulS

Quote
The data I really need is found in the 6th and 7th column of the 030 address when looked in the Hex editor.
A picture might be useful, but I'd guess that the next row starts with 040. That means that the data that you want is actually at addresses 036 and 037.

Geforcer

#4
Jul 15, 2015, 02:56 pm Last Edit: Jul 15, 2015, 02:58 pm by Geforcer
Yes, the next row starts with 040. The picture is in the attachment.

PaulS

So, the addresses are NOT 030 + anything. They are 0x30 + an offset (6 and 7). In other words, 0x36 and 0x37.

Geforcer

#6
Jul 15, 2015, 03:27 pm Last Edit: Jul 15, 2015, 03:29 pm by Geforcer
Great! Sorry for my lack of knowledge. Already learned something new today. :) 

So, how can I read the data from those addresses - 0x36 and 0x37 ? What do I need to edit in code from the first post?


Thank you!

PaulS

This statement
Code: [Select]
    byte r = eeprom_i2c_read(B01010000, i);
The first argument is the I2C address of the device you want to read from. The second argument is the address of the data. That second value should be 0x36 for one call and 0x37 for the second call.

Geforcer

#8
Jul 16, 2015, 09:27 am Last Edit: Jul 16, 2015, 10:06 am by Geforcer
So, should the code look like this?

Code: [Select]

#include <Wire.h>

void eeprom_i2c_write(byte address, byte from_addr, byte data) {
  Wire.beginTransmission(address);
  Wire.send(from_addr);
  Wire.send(data);
  Wire.endTransmission();
}

byte eeprom_i2c_read(int address, int from_addr) {
  Wire.beginTransmission(address);
  Wire.send(from_addr);
  Wire.endTransmission();

  Wire.requestFrom(address, 1);
  if(Wire.available())
    return Wire.receive();
  else
    return 0xFF;
}

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

  for(int i = 0; i < 10; i++) {
    eeprom_i2c_write(B01010000, i, 'a'+i);
    delay(100);
  }

  Serial.println("Writen to memory!");
}

void loop() {
  for(int i = 0; i < 10; i++) {
    byte r = eeprom_i2c_read(B01010000, 0x36);
    Serial.print(i);
    Serial.print(" - ");
    Serial.print(r);
    Serial.print("\n");
    delay(1000);
  }
}



Then, repeat the same code with 0x37? Insert code in Arduino, connect the EEPROM with Arduino, start monitor and it will read and show the data from the corresponding address?
Should I remove anything from the code? I don't want any writing to the EEPROM (I wont connect WP pin, just to be safe). :D

Thanks, Paul!

PaulS

Code: [Select]
void loop() {
  for(int i = 0; i < 10; i++) {
    byte r = eeprom_i2c_read(B01010000, 0x36);

Why do you want to read the data at address 0x36 10 times?

Quote
Should I remove anything from the code?
The part that writes data to the first 10 addresses, in setup().

Geforcer

#10
Jul 16, 2015, 01:50 pm Last Edit: Jul 16, 2015, 01:51 pm by Geforcer
Is it ok now, Paul?

Quote
#include <Wire.h>

void eeprom_i2c_write(byte address, byte from_addr, byte data) {
  Wire.beginTransmission(address);
  Wire.send(from_addr);
  Wire.send(data);
  Wire.endTransmission();
}

byte eeprom_i2c_read(int address, int from_addr) {
  Wire.beginTransmission(address);
  Wire.send(from_addr);
  Wire.endTransmission();

  Wire.requestFrom(address, 1);
  if(Wire.available())
    return Wire.receive();
  else
    return 0xFF;
}

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

}

void loop() {
  for(int i = 0; i < 1; i++) {
    byte r = eeprom_i2c_read(B01010000, 0x36);
    Serial.print(i);
    Serial.print(" - ");
    Serial.print(r);
    Serial.print("\n");
    delay(1000);
  }
}
 

PaulS

Why not read and print both addresses?
Code: [Select]
void loop()
{
  for(int i = 0x36; i <= 0x37; i++)
 {
    byte r = eeprom_i2c_read(B01010000, i);
    Serial.print(i);
    Serial.print(" - ");
    Serial.print(r);
    Serial.print("\n");
  }
  delay(1000);
}


There is no sense waiting a full second between reading the two values. There's no real sense for reading the data over and over again in loop(). The data can't change, can it?

Geforcer

#12
Jul 16, 2015, 03:27 pm Last Edit: Jul 16, 2015, 03:27 pm by Geforcer
That is awesome! No, the data doesn't change. :)

Thank you, Paul! I will try it soon and report back.

Geforcer

Hm. This it the result of the readout...

Quote
54 - 255
55 - 255
Something isn't right...

chucktodd

#14
Jul 17, 2015, 10:51 pm Last Edit: Jul 19, 2015, 11:19 pm by chucktodd Reason: my Pascal Heritage shows thru
Hm. This it the result of the readout...

Something isn't right...
When you do a Read operation you need to use the re-Start protocol.

Code: [Select]

uint8_t readByte(uint8_t i2cAddr, uint8_t dataAddr){
Wire.beginTransmission(i2cAddr);
Wire.write(dataAddr);
Wire.endTransmission(false); // don't send stop

Wire.requestFrom(i2cAddr,1);
return Wire.read();
}

Read Timing from AT24C01 Specification  


Go Up