Pages: [1]   Go Down
Author Topic: interfacing arduino to 24C65  (Read 633 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys.

I'm new to arduino, and am working on a project which will heavily involve using i2c protocals. As such I wanted to work out the basic read/write functions. So I connected it to a 24C65 EEPROM chip, and using an example I found on this forum, tried to write to and then read from the external memory chip. My code is below:

Code:
#include <Wire.h>
#define CHIP B1010000

void setup()
{
  Wire.begin();
  Serial.begin(115200);
  
  Wire.beginTransmission(CHIP);
  Wire.send(0); //sets memory pointer to 0
  Wire.send(2); // sends value 2 to that location
  Wire.endTransmission();
  delay(1000);
  Wire.beginTransmission(CHIP);
  Wire.send(0x02); // moves pointer to memory location 02
  Wire.send(2);      // sends value two to that location
  Wire.endTransmission();
  delay(1000);
  Wire.beginTransmission(CHIP);
  Wire.send(0x03);
  Wire.send(2);
  Wire.endTransmission();
  delay(1000);
  
  Wire.beginTransmission(CHIP);
  Wire.send(0);
  Wire.endTransmission();
  Wire.requestFrom(CHIP,8);
  while(Wire.available())
  {
    char c = Wire.receive();
    Serial.print(c,HEX);
    Serial.print(", ");
  }


  
  
}

Now, I've got some partial success on the read side, in so much as it does read the required amount of bytes, if I ask it to read 8 bytes, I get 8 values in the serial window, if I ask it to read 16 bytes I get 16 values in the serial monitor etc, etc.

The problem I have is that it always reads the same value, -1 (in DEC), despite what else I try to write to those memory locations.

Can anyone help as to what I am doing wrong?

Cheer sfor any help.
« Last Edit: January 29, 2009, 09:57:40 am by MiG » Logged

Athens - Greece
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

your line:
  Wire.send(0); //sets memory pointer to 0
doesnt seem to be ok.

it sends a byte, but in order to set the memory address on your i2c eeprom, you need to send a 2 bytes long address, e.g. an int, since int for arduino is 16bit (2-byte) long.

quoting from an i2c eeprom datasheet:
Quote
Each data byte in the memory has a 16-bit (two
byte wide) address. The Most Significant Byte (Table
4) is sent first, followed by the Least significant
Byte (Table 5). Bits b15 to b0 form the address of
the byte in memory. Bit b15 is treated as Don't
Care bits on the M24256-A memory.

so, you need to send a 16bit memory address, for example, to write a byte you can:
 int memory_address = 0; // this is 16bit long
  Wire.beginTransmission(i2c_address);
  Wire.send((byte)(memory_address >> smiley-cool);  // first the MSB (8-bits)
  Wire.send((byte)memory_address);  // then the LSB (8-bits)
  Wire.send(data);
  Wire.endTransmission();

Logged

Sophia-Antipolis (06)
Offline Offline
God Member
*****
Karma: 4
Posts: 618
Rejoignez le club !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sv1eed is right.

Look at this code : http://www.arduino.cc/playground/Code/I2CEEPROM

I don't understand this :

Code:
 Wire.send((int)(eeaddresspage >> 8)); // MSB
  Wire.send((int)(eeaddresspage & 0xFF)); // LSB

Why a cast (int) as Wire.send() takes a byte (as explained by Sv1eed) and what is the need for "& 0xFF" ? There is a short note about 6-bit end will wrap around..

These questions are only for culture, as both code may work.
« Last Edit: February 04, 2009, 02:05:54 am by julien@pobot.org » Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 598
Posts: 33335
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
what is the need for "& 0xFF"

This is a bit wise AND operation and zeros all bits except for the 8 least significant bits.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys, got it working.

Cheers
Logged

Pages: [1]   Go Up
Jump to: