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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM