文章和固件代碼示例介紹了如何使用MAX2990電力線通信調制解調器上的IC接口與外部EEPROM 24C04接口。
介紹
本應用筆記和固件代碼示例描述了MAX2990電力線通信調制解調器上的IC接口如何與外部EEPROM 24C04接口。IC總線由MAX2990(主機)控制,24C04 EEPROM由從機控制。下面的示意圖顯示了此示例中使用的硬件配置。
固件說明
IC接口初始化
每當使能IC模塊時,SCL和SDA必須配置為漏極開路。此配置是IC通信正常運行所必需的。由于IC是GPIO端口的替代功能,固件必須確保在初始化期間禁用SCL和SDA輸入上的上拉(通過將零寫入該端口控制器輸出位)。
該示例具有 250kHz 時鐘頻率。首先,需要在MAX2990中設置IC接口,如下所示:
PO1_bit.Bit2 = 0; // Disables the GPIO function of the PO1_bit.Bit3 = 0; // I2C pins I2CCN_bit.I2CEN = 0; // Makes sure that I2C is disabled // to allow the changing of the I2C settings I2CCN_bit.I2CMST = 1; // Sets the I2C engine to master mode I2CCN_bit.I2CEA = 0; // 7-bit address mode I2CCK_bit.I2CCKL = 0x40; // 2μs CLK-low, to define I2C frequency I2CCK_bit.I2CCKH = 0x40; // 2μs CLK-high, to define I2C frequency I2CTO = 200; // I2C_TIMEOUT I2CST = 0x400; // Resets I2C status register I2CCN_bit.I2CEN = 1; // Enables the I2C engine
寫入模式
要寫入 24C04 EEPROM,必須通過 IC 接口寫入以下字節:
IC EEPROM 的地址(在本例中0xA0)
EEPROM 中存儲器位置的地址
數據字節(地址將自動增加)
在此示例中,我們嘗試從位置0x00開始將以下字節寫入EEPROM:0x12,0x34,0x56,0x78和0x90。
i2c_init_write(); // Sets the MAX2990 I2C Engine into write mode i2c_write(0x50); // 24C04 write (adr = 0b1010 000 0) = 0xA0 // The MAX2990 I2C engine shifts the I2C address by // 1 bit, because it will generate the R/W bit // automatically i2c_write(0x00); // word address location i2c_write(0x12); // data1 i2c_write(0x34); // data2 i2c_write(0x56); // data3 i2c_write(0x78); // data4 i2c_write(0x90); // data5 I2C_STOP; // Sends I2C stop-condition
閱讀模式
為了回讀數據,我們從EEPROM寫入。重要的是,我們要給 24C04 足夠的時間來編寫。這通常需要在“停止條件”后的幾毫秒內。請查閱IC的數據手冊,確保使用正確的時序。
i2c_init_write(); // Sets the MAX2990 I2C engine into write mode
i2c_write(0x50); // 24C04 write (adr = 0b1010 000 0) = 0xA0
// The MAX2990 I2C engine shifts the I2C address by
// 1 bit, because it will generate the R/W bit
// automatically
i2c_write(0x00); // word address location
i2c_init_read(); // Sets the MAX2990 I2C engine into read mode
i2c_write(0x50); // 24C04 read (adr = 0b1010 000 1) = 0xA1
// The MAX2990 I2C engine shifts the I2C address by
// 1 bit, because it will generate the R/W bit
// automatically
unsigned char data[5]; // Array to store the received data
i2c_read(data[0]); // Reads 1 byte from I2C and writes it to the array
i2c_read(data[1]); // Reads 1 byte from I2C and writes it to the array
i2c_read(data[2]); // Reads 1 byte from I2C and writes it to the array
i2c_read(data[3]); // Reads 1 byte from I2C and writes it to the array
i2c_read(data[4]); // Reads 1 byte from I2C and writes it to the array
I2C_STOP; // Sends I2C stop-condition
現在我們檢查以下用于讀取和寫入EEPROM的函數。
i2c_init_write(void) i2c_init_read(void) i2c_write(UINT8 data) i2c_read(UINT8 *data)
void i2c_init_write(void) { I2CCN_bit.I2CMODE = 0; // I2C transmit mode I2CCN_bit.I2CACK = 1; // Creates I2C NACK so that slave can create ACK I2C_START; // Generates I2C START condition while( I2CCN_bit.I2CSTART == 1 ); // Waits until the START condition // was put to the I2C bus I2CST_bit.I2CSRI = 0; // Resets the I2C interrupt flag } int i2c_init_read(void) { I2CCN_bit.I2CMODE = 1; // I2C read-mode I2CCN_bit.I2CACK = 0; // Creates I2C ACK after receive I2C_START; // Generates I2C START condition while( I2CCN_bit.I2CSTART == 1 ); // Waits until the START condition I2CST_bit.I2CSRI = 0; // Resets the I2C interrupt flag } void i2c_write(UINT8 data) { I2CBUF = data; // Puts the data on the I2C bus while( I2CST_bit.I2CTXI == 0 ); // Waits for transfer complete I2CST_bit.I2CTXI = 0; // Resets the I2C transmit complete // interrupt flag } void i2c_read(UINT8 *data) { I2CBUF = 0xff; // Puts "all ones" on the I2C bus so that slave can pull // the bus down to generate zeros while( !I2CST_bit.I2CRXI ); // Waits for receive complete I2CST_bit.I2CRXI=0; // Resets the I2C receive complete // interrupt flag *data = I2CBUF; // Writes the data to the pointer }
審核編輯:郭婷
-
調制解調器
+關注
關注
3文章
860瀏覽量
38940 -
總線
+關注
關注
10文章
2903瀏覽量
88397 -
代碼
+關注
關注
30文章
4827瀏覽量
69054
發布評論請先 登錄
相關推薦
LTC2990-I2C溫度、電壓和電流監視器
MAX2990 pdf
AN979使用手冊,I2C™串行EEPROM與P
MAX2990 Integrated Power-Line
使用硬件模塊實現8051 MCU與I2C?串行EEPROM的接口
I2C串行EEPROM與PIC18器件接口設計的詳細中文資料概述
![<b class='flag-5'>I2C</b>串行<b class='flag-5'>EEPROM</b>與PIC18器件<b class='flag-5'>接口</b>設計的詳細中文資料概述](https://file.elecfans.com/web1/M00/55/D2/o4YBAFs12f6AHWQcAAB_iC3bdYc771.png)
評論