mega2560外部EEPROM地址出错
#include <Wire.h>void setup()
{
Serial.begin(9600);
delay(10);
Wire.begin();
}
byte val = 0;
int index = 0;
void loop()
{
byte a0 = 0x00;
byte b0 = 0x00;
write_data(0x50 ,0x00 ,0xbb);
//write_data(0x57 ,0x00 ,0x22);
a0 = read_data(0x50 ,0x00);
//byte b0 = read_data(0x57 ,0x00);
Serial.print("a=");delay(10);
Serial.println(a0,HEX);delay(10);
Serial.print("b=");delay(10);
Serial.println(b0,HEX);delay(10);
delay(2000);
}
void write_data(byte num ,byte add ,byte data)
{
// write
Wire.beginTransmission(int(num));
Wire.write(add); // sendaddress ww
Wire.write(data); // senddata ww
Wire.endTransmission(); //
delay(10);//very import ww
}
byte read_data(byte num ,byte add)
{
// read
Wire.beginTransmission(int(num));
Wire.write(add); // sendaddress
Wire.endTransmission(); //
Wire.requestFrom(int(num), 1); // request 2 bytes from slave device #112
if(1 <= Wire.available())
{
val = Wire.read();// receive high byte (overwrites previous reading)
}
delay(10);
return val;
}
我用AT24C02作为外部EEPROM,板子是mega2560,(SDA为Pin20,SCL为Pin21——这没错吧?)错误如下:
不管AT24C02硬件地址(A0A1A2)为多少,数据照样写入AT24C02中~
即:
write_data(0x50,0x00,0xaa);
write_data(0x57,0x00,0x11);
接着读取数据时
read_data(0x50,0x00)的值为0x11;
read_data(0x57,0x00)的值也为0x11~
我AT24C02的硬件地址为0x50~
求大神指教
页:
[1]