STM32 I2C-EEPROM 的讀寫



STM32 I2C-EEPROM 的讀寫
個人引用的範例是 M24C08,而在這個範例中 i2c_ee.c 有幾個參數必數理解。

#define I2C_Speed              200000
#define I2C1_SLAVE_ADDRESS7    0xA0
#define I2C_PageSize           8

其第一條
#define I2C_Speed              200000

I2C 具有三種傳輸模式:
標準模式傳輸速率為100Kbit/s
快速模式傳輸速率為400Kbit/s
高速模式傳輸速率為3.4Mbit/s(但是目前大多的I2C設備都不支援高速模式)

STM32F103中 對I2C GPIO埠的初始化:
static void I2C_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*
致能與 I2C1 有關的時脈 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
/* PB6-I2C1_SCL
PB7-I2C1_SDA*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //
這裡不一定要用50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; //
開漏輸出
GPIO_Init(GPIOB, &GPIO_InitStructure);
}

這裡是設置I2C的傳輸速率,在調用初始化函數時,函數會根據我們輸入的數位經過運算後把分頻值寫入到I2C的時脈控制寄存器(我們寫入的這個值不能高於400KHz)


#define I2C1_SLAVE_ADDRESS7    0xA0
配置STM32I2C設備自己的位址,根據I2C協定,每一個連接到I2C匯流排上的設備都需要唯一的位址(主機也一樣)這裡設置為自訂。在這必需必了解硬體在設計上所使用的定址腳位。


#define I2C_PageSize           8
關於PageSize,不同容量的EEPROM:有不同的的PageSize,參考下面由Datdsheet,所節錄出來的相關資料,例 AT24C01, AT24C02, AT24C04 , PageSize=8,而AT24C08 PageSize=16AT24C64 PageSize=32
memory Map
AT24C01A, 1K SERIAL EEPROM:
Internally organized with 16 pages of 8 bytes each,
the 1K requires a 7-bit data word address for random word addressing.
AT24C02, 2K SERIAL EEPROM:
Internally organized with 32 pages of 8 bytes each,
the 2K requires an 8-bit data word address for random word addressing.
AT24C04, 4K SERIAL EEPROM:
Internally organized with 32 pages of 16 bytes each,
the 4K requires a 9-bit data word address for random word addressing.
AT24C08A, 8K SERIAL EEPROM:
Internally organized with 64 pages of 16 bytes
each, the 8K requires a 10-bit data word address for random word addressing.
AT24C16A, 16K SERIAL EEPROM:
Internally organized with 128 pages of 16 bytes
each, the 16K requires an 11-bit data word address for random word addressing

AT24C32/64, 32K/64K SERIAL EEPROM:
The 32K/64K is internally organized as 256
pages of 32 bytes each. Random word addressing requires a 12/13 bit data word address.

AT24C128/256, 128K/256K SERIAL EEPROM:
The 128K/256K is internally organized as
256/512 pages of 64-bytes each. Random word addressing requires a 14/15-bit data word address.


AT24C512, 512K SERIAL EEPROM:
The 512K is internally organized as 512 pages of 128-bytes each. Random word addressing requires a 16-bit data word address.

AT24C1024, 1024K SERIAL EEPROM:
The 1024K is internally organized as 512 pages of 256 bytes each. Random word addressing requires a 17-bit data word address.



按這個範例我去執行對 24C02, EEPROM,結果我發現很快會當機。看了ST 對這個問題的回應,也參考網路上的一些對應方法,最後選用了下面的這個方法,在i2c_ee.c 中找到下面兩個函數
void I2C_EE_BufferWrite(u8* pBuffer, u8 WriteAddr, u16
NumByteToWrite)
void I2C_EE_BufferRead(u8* pBuffer, u8 ReadAddr, u16
NumByteToRead)
在這兩個函數內的開頭先要執行一句I2C_EE_WaitEepromStandbyState();

這個作法雖不保證不當機,但在連續讀寫中可以相對穩定很多。
若想要穩定的 I2C-EEPROM 的讀寫,建議在網路上找GPIO模擬I2C的範例來應用會比較好。

下表 EEPROM 的規格供大家參考。

EEPROM
Memory (bit)
Memory (bytes)
Address length (bit)
Number of pages (bytes)
Pages (pages)
AT24C32
32768
4096
12
32
128
AT24C64
65536
8192
13
32
256
AT24C128
131072
16384
14
64
256
AT24C256
262144
32768
15
64
512
AT24C512
524288
65536
16
128
512


留言

這個網誌中的熱門文章

如何提高STM32 ADC的精度

ENC28J60+Uip TCP Server/Client