Arduino I2C + AC24C32 EEPROM

主要特性

AC24C32是Atmel的两线制串行EEPROM芯片,根据工作电压的不同,有-2.7、-1.8两种类型。主要特性有:

  • 工作范围:-2.7类型范围4.5~5.5V,-1.8类型1.8~5.5V。本文用的为-2.7类型。
  • 待机功耗:与工作电压有关,见下图
  • 容量:4096 x 8bits,即32k bits
  • 接口:I2C,工作在5V时支持最大时钟频率400kHz,其他电压时100kHz
  • 允许一次写一页(32-byte page write mode)
  • 一次写动作完成的时间:与工作电压有关,最大20ms
  • 写保护(write protect)功能
  • 输入脚有施密特触发器,用于噪声抑制
  • 可靠性:可写1百万次;数据可保存100年
  • 封装:8-Pin PDIP/SOIC/TSSOP

管脚定义

  • VCC:电源脚
  • GND:地
  • A0、A1、A2:器件I2C地址控制脚,7位I2C地址为0b1010A2A1A0。浮空时都为低电平。
  • SCL、SDA:I2C接口时钟线、数据线。
  • WP:写保护输入脚。当连接低电平时,器件正常读写;当连接高电平时,无法对前8k bits内容进行写入。浮空时为低电平。

与Arduino的连接

与Arduino UNO的I2C接口连接。

VCC连接5V;GND连接GND;AC24C32的SCL连接UNO的A5(SCL);AC24C32的SDA连接UNO的A4(SDA)。

功能调试

1. Page Write时,一次最多写入32个字节。当地址到达该页末尾时,会自动roll over到同一页的起始地址。

2. Sequential Read时,没有连续读取的字节数目限制(实际受限于Arduino的Wire库中buffer的大小)。当地址到达最后一页的末尾时,会自动roll over到首页的起始地址。

3. 写操作时,MCU发送stop后,AC24C32还需要一段tWR时间(tWR在5V供电时最大为10ms)进行内部工作,之后数据才正确写入。在tWR时间内,芯片不会回应任何接口的操作。

测试代码

以下代码向AC24C32写入了一段字符串,之后将写入的信息反复读出。

 1 /*
 2 access to EEPROM AT24C32 using Arduino
 3 storage capacity: 32K bits (4096 bytes)
 4 */
 5 
 6 #include <Wire.h>
 7 
 8 #define ADDRESS_AT24C32 0x50
 9 
10 word wordAddress = 0x0F00; //12-bit address, should not more than 4095(0x0FFF)
11 char str[] = "This is ZLBG."; //string size should not more than 32 and the buffer size
12 byte buffer[30]; 
13 
14 int i;
15 
16 void setup()
17 {
18     Wire.begin();
19     Serial.begin(9600);
20 
21     //write
22     Wire.beginTransmission(ADDRESS_AT24C32);
23     Wire.write(highByte(wordAddress));
24     Wire.write(lowByte(wordAddress));
25     for (i = 0; i < sizeof(str); i++)
26     {
27         Wire.write(byte(str[i]));
28     }
29     Wire.endTransmission();    
30 
31     delay(10); //wait for the internally-timed write cycle, t_WR
32 }
33 
34 void loop()
35 {
36     //read
37     Wire.beginTransmission(ADDRESS_AT24C32);
38     Wire.write(highByte(wordAddress));
39     Wire.write(lowByte(wordAddress));
40     Wire.endTransmission();
41     Wire.requestFrom(ADDRESS_AT24C32, sizeof(str));
42     if(Wire.available() >= sizeof(str))
43     {
44         for (i = 0; i < sizeof(str); i++)
45         {
46             buffer[i] = Wire.read();
47         }
48     }
49 
50     //print
51     for(i = 0; i < sizeof(str); i++)
52     {
53         Serial.print(char(buffer[i]));
54     }
55     Serial.println();
56 
57     delay(2000);
58 }
View Code

参考资料

AT24C32 - Atmel Corporation
Arduino playground: Using Arduino with an I2C EEPROM
xAppSoftware Blog: How to interface the 24LC256 EEPROM to Arduino

原文地址:https://www.cnblogs.com/zlbg/p/4230137.html