|
各位大神,小弟想用arduino mega 2560写入并且读取at24c08的数据,程序已经编好,但是不能正常工作,求指导
#include <Wire.h>
const byte EEPROM_ID = 0xa0; // I2C address for 24LC128 EEPROM
const byte EEPROM_read=0xa1;
// first visible ASCII character '!' is number 33:
int thisByte = 33;
void setup()
{
Serial.begin(9600);
Wire.begin();
}
void loop()
{
Serial.println("Writing 1024 bytes to EEPROM");
for(int i=0; i < 1024; i++)
{
I2CEEPROM_Write(i, thisByte);
// go on to the next character
thisByte++;
if(thisByte == 126) // you could also use if (thisByte == '~')
thisByte = 33; // start over
}
Serial.println("Reading 1024 bytes from EEPROM");
int thisByte = 33;
for(int i=0; i < 1024; i++)
{
char c = I2CEEPROM_Read(i);
Serial.print(c);
thisByte++;
if(thisByte == 126)
{
Serial.println();
thisByte = 33; // start over on a new line
}
}
Serial.println();
}
// This function is similar to EEPROM.write()
void I2CEEPROM_Write( unsigned int address, byte data )
{
Wire.beginTransmission(EEPROM_ID);
Wire.write((int)highByte(address) );
Wire.write((int)lowByte(address) );
Wire.write(data);
Wire.endTransmission();
delay(5); // wait for the I2C EEPROM to complete the write cycle
}
// This function is similar to EEPROM.read()
byte I2CEEPROM_Read(unsigned int address )
{
byte data;
Wire.beginTransmission(EEPROM_read);
Wire.write((int)highByte(address) );
Wire.write((int)lowByte(address) );
Wire.endTransmission();
Wire.requestFrom(EEPROM_read,(byte)1);
delay(5);
data = Wire.read();
return data;
} |
|