极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12080|回复: 0

如何用arduino读取at24c08的数据

[复制链接]
发表于 2012-8-19 10:31:50 | 显示全部楼层 |阅读模式
各位大神,小弟想用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;
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-29 11:50 , Processed in 0.045992 second(s), 17 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表