引言
本文介紹了如何通過MAX2990電力線通信調(diào)制解調(diào)器的I2C接口與外部EEPROM 24C04連接,并給出了相應(yīng)的固件例程。I²C總線受控于MAX2990 (主機),24C04 EEPROM為從機器件。以下框圖給出了本文示例的硬件配置。
固件說明
I²C接口初始化
一旦使能I²C模塊,SCL和SDA必須配置成漏極開路狀態(tài),以確保I²C總線通信正常。由于I²C是GPIO端口的一個替代功能,固件必須確保SCL和SDA輸入在初始化期間禁止上拉(通過對端口控制器的輸出位寫零實現(xiàn))。
示例中,時鐘頻率為250kHz。首先需要配置MAX2990的I²C接口:
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時,必須通過I²C接口寫入以下字節(jié):
EEPROM的I²C總線地址(這里為0xA0)
EEPROM存儲器的地址
數(shù)據(jù)字節(jié)(地址將自動遞增)
示例中試圖寫入以下字節(jié),從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