文章和固件代码示例介绍了如何使用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文章
850浏览量
38787 -
总线
+关注
关注
10文章
2871浏览量
88010 -
代码
+关注
关注
30文章
4762浏览量
68408
发布评论请先 登录
相关推荐
评论