Xadow Storage 存储器

From Wiki
Jump to: navigation, search

产品简介


300px-X Storage 01.jpg


现在使用的AVR内部都有一个小的EEPROM,用来存储一些变量,配置参数等等。比如说Xadow主板上的控制芯片Atmega32u4,内部有1k的EEPROM。如果你的Xadow项目需要一个较大的存储空间,那么这款产品正好可以满足你。Xadow Storage 使用了两片 AT24C256,提供了2X32K的存储空间。通过I2C协议,对EEPROM进行写入和读取数据的操作。

规格参数

工作电压:3.3V 通信方式: I2C EEPROM芯片: AT24C256C I2C 地址可变 外形尺寸: 25.43mm x 20.35mm I2C 地址设置

Xadow Storage中的两片存储芯片AT24C256是共用一个I2C总线。每一个芯片有一个八位的I2C 地址。I2C 地址是由存储芯片上的A2, A1, A0管脚决定的。A2, A1, A0管脚可以选择连接到GND或者VCC。如果管脚处于悬空状态,那么芯片内部会将它拉到GND。

从机地址[A6 - A0] A2, A1, A0 输入状态 A2 A1 A0 1010000(0x50) GND/悬空 GND/悬空 GND/悬空 1010001(0x51) GND/悬空 GND/悬空 VCC 1010010(0x52) GND/悬空 VCC GND/悬空 1010011(0x53) GND/悬空 VCC VCC 1010100(0x54) VCC GND/悬空 GND/悬空 1010101(default 0x55) VCC GND/悬空 VCC 1010110(0x56) VCC VCC GND/悬空 1010111(0x57) VCC VCC VCC 默认情况下,AT24C256(U1)的I2C 地址是0x50,AT24C256(U2)的I2C 地址是 0x51。

应用示例

下面将展示如何写入数据到Storage,并读取写入的数据。 硬件连接:


XadowStorage.jpg


测试代码如下:

  1. include <Wire.h> //I2C library

void setup() {

   char somedata[] = "this is data from the eeprom"; // data to write
   Wire.begin(); // initialise the connection
   Serial.begin(9600);
   i2c_eeprom_write_page(0x51, 0, (byte *)somedata, sizeof(somedata)); // write to EEPROM 
   delay(10); //add a small delay
   Serial.println("Memory written");
}

void loop() {

   int addr=0; //first address
   byte b = i2c_eeprom_read_byte(0x51, 0); // access the first address from the memory
   while (b!=0) 
   {
     Serial.print((char)b); //print content to serial port
     addr++; //increase address
     b = i2c_eeprom_read_byte(0x51, addr); //access an address from the memory
   }
   Serial.println(" ");
   delay(2000);

} void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {

   int rdata = data;
   Wire.beginTransmission(deviceaddress);
   Wire.write((int)(eeaddress >> 8)); // MSB
   Wire.write((int)(eeaddress & 0xFF)); // LSB
   Wire.write(rdata);
   Wire.endTransmission();
 }
 // WARNING: address is a page address, 6-bit end will wrap around
 // also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
 void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
   Wire.beginTransmission(deviceaddress);
   Wire.write((int)(eeaddresspage >> 8)); // MSB
   Wire.write((int)(eeaddresspage & 0xFF)); // LSB
   byte c;
   for ( c = 0; c < length; c++)
     Wire.write(data[c]);
   Wire.endTransmission();
 }

byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {

   byte rdata = 0xFF;
   Wire.beginTransmission(deviceaddress);
   Wire.write((int)(eeaddress >> 8)); // MSB
   Wire.write((int)(eeaddress & 0xFF)); // LSB
   Wire.endTransmission();
   Wire.requestFrom(deviceaddress,1);
   if (Wire.available()) rdata = Wire.read();
   return rdata;

} // maybe let's not read more than 30 or 32 bytes at a time! void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {

   Wire.beginTransmission(deviceaddress);
   Wire.write((int)(eeaddress >> 8)); // MSB
   Wire.write((int)(eeaddress & 0xFF)); // LSB
   Wire.endTransmission();
   Wire.requestFrom(deviceaddress,length);
   int c = 0;
   for ( c = 0; c < length; c++ )
   if (Wire.available()) buffer[c] = Wire.read();
 }


File:AT24C256C-SSHL-T.pdf