编辑:
我对位和字节有点困惑,文档指定位而不是字节,并将位读取为字节。这就解释了我的错误。为什么制造商使用位而不是字节来指定容量?这有点令人困惑
我找到了Atmel
24C02n
在一块备用板上安装2kb EEPROM,我想试试看这个芯片里有什么样的数据,以及我是否可以重用它。从未读/写过外部存储器。接线很简单(i2c),工作非常好。
运行i2cScanner,它在总线上找到了8个地址,0x50。。0x57。第一个想法:“奇怪,一个设备有8个地址。”文档中描述了有8x256字节,因此,好的,8页256字节中有8个地址。
然而,当我想访问另一个页面(例如0x51)时,我会得到相同的数据,它似乎会写入相同的内存页面。我错过什么了吗?
以下是我使用wire库的示例代码:
#include <Wire.h>
int A24C_PAGE_COUNT = 8;
uint8_t A24C_PAGE_ADDR[8] = { 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 };
int A24C_PAGE_SIZE = 256;
uint8_t eepromReadAddress(int address, int from_addr)
{
Wire.beginTransmission(address);
Wire.write(from_addr);
Wire.requestFrom(address, 1);
uint8_t iResult = (Wire.available())?Wire.read():0x32;
Wire.endTransmission();
return iResult;
}
void eepromWriteAddress(uint8_t address, uint8_t from_addr, uint8_t* data)
{
Wire.beginTransmission(address);
Wire.write(from_addr);
while( *data )
{ Wire.write( *data++ ); }
Wire.endTransmission();
}
void eepromRead()
{
int iByte = 0;
int iPage = 0;
while( iPage < A24C_PAGE_COUNT )
{
Serial.print( "PAGE: " );
Serial.println( iPage+1 );
while( iByte < A24C_PAGE_SIZE )
{
//Serial.print( "0x" );
Serial.print( (char)eepromReadAddress( A24C_PAGE_ADDR[ iPage ], iByte ) );
++iByte;
Serial.print(( iByte % 16 == 0 )?"\n":",");
}
iByte=0;
++iPage;
}
}
void setup()
{
Wire.begin();
Serial.begin(9600);
delay( 1000 );
//eepromWriteAddress( 0x50, 128, "Hello world!" );
eepromRead();
}
void loop() {
}
输出后写一次“你好,世界!”到第128位的第一页,我将得到:
PAGE: 1
G, ,?,?,, ,,k, ,, ,, ,., ,
,, ,!,,,,?, ,,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?,?,?
?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
PAGE: 8
G, ,?,?,, ,,k, ,, ,, ,., ,
,, ,!,,,,?, ,,!,?, ,Q,?,4
,?, ,,I,?, ,<, ,?,?,?,?,?,?,?
?, ,D, ,,<, ,,?, ,, ,,t, ,
?, ,?,?,?,?,?, , , , , , , , ,
, , , , , , , , , , , , , , ,
, , , , , ,?,?,?,?,?,?,?,?,?,?
?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
?,H,e,l,l,o, ,w,o,r,l,d,!,?,?,?
?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
每个地址指向同一页????
好的,另一种方法,将以下变量更改为读取一个大页面(只是尝试看看会发生什么):
int A24C_PAGE_COUNT = 1;
int A24C_PAGE_SIZE = 2048;
同样的结果,只显示第一页,但重复了8次。那么我错在哪里呢?为什么一台设备有8个地址?