Go Down

Topic: interfacing arduino to 24C65 (Read 722 times) previous topic - next topic

MiG

Jan 29, 2009, 03:55 pm Last Edit: Jan 29, 2009, 03:57 pm by MiG Reason: 1
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: [Select]
#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.

sv1eed

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 >> 8));  // first the MSB (8-bits)
 Wire.send((byte)memory_address);  // then the LSB (8-bits)
 Wire.send(data);
 Wire.endTransmission();


Luj06

#2
Feb 04, 2009, 08:03 am Last Edit: Feb 04, 2009, 08:05 am by julien@pobot.org Reason: 1
Sv1eed is right.

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

I don't understand this :

Code: [Select]

 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.

Grumpy_Mike

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.

MiG

Thanks guys, got it working.

Cheers

Go Up