当前位置:首页 > 电源 > 数字电源
[导读]  引言  本文介绍了如何通过MAX2990电力线通信调制解调器的I2C接口与外部EEPROM 24C04连接,并给出了相应的固件例程。I²C总线受控于MAX2990 (主机),24C04 EEPROM为从机器件。以下框图给出了本文示例的硬件

  引言

  本文介绍了如何通过MAX2990电力线通信调制解调器的I2C接口与外部EEPROM 24C04连接,并给出了相应的固件例程。I²C总线受控于MAX2990 (主机),24C04 EEPROM为从机器件。以下框图给出了本文示例的硬件配置。

  


 

  固件说明

  I²C接口初始化

  一旦使能I²C模块,SCL和SDA必须配置成漏极开路状态,以确保I²C总线通信正常。由于I²C是GPIO端口的一个替代功能,固件必须确保SCL和SDA输入在初始化期间禁止上拉(通过对端口控制器的输出位写零实现)。

示例中,时钟频率为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接口写入以下字节:

  1. EEPROM的I²C总线地址(这里为0xA0)
  2. EEPROM存储器的地址
  3. 数据字节(地址将自动递增)

示例中试图写入以下字节,从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[!--empirenews.page--]

现在,我们可以验证一下用于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
}
本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

2022 年 6 月 30 日,中国—— 依托在串行EEPROM技术领域的积累和沉淀,意法半导体率先业界推出了串行页EEPROM (Serial Page EEPROM)。这款全新类别的EEPROM 是一种SPI串行接口...

关键字: 意法半导体 EEPROM 非易失性存储器

包含中国在内,共计全球20个国家及地区可于线上收看 上海2022年6月21日 /美通社/ -- 万代南梦宫娱乐股份有限公司(总公司:东京都港区,社长:宫河恭夫)宣布将于2022年7月9日(周六)、7月10日(周日)两日...

关键字: TE CST LM AI

(全球TMT2022年6月14日讯)6月14日-7月12日,2022年中国国际半导体技术大会(CSTIC2022)将以云分享的方式在线上举办。本届会议将有九场专题讨论会,超400场专业演讲,4周网络会议延续与讲师的问答...

关键字: 半导体技术 安集科技 TI CST

单片机的存储器可分为程序存储器(ROM)和数据存储器(RAM)。

关键字: EEPROM 记忆设备

▼点击下方名片,关注公众号▼存储器分为两大类:RAM和ROM。RAM就不讲了,主要讨论ROM。ROM最初不能编程,出厂什么内容就永远什么内容,不灵活。后来出现了PROM,可以自己写入一次,要是写错了,只能换一片,自认倒霉...

关键字: EEPROM Flash

摘要:英飞凌公司推出的TLE4997是一款全新的可编程线性霍尔传感器。该传感器经过专门设计,可满足需要精确角度和位置检测的汽车级产品的苛刻要求。文中简要介绍了该传感器的主要特性,然后论述了其编程方法,最后给出了该传感器编...

关键字: TLE4997 霍尔传感器 Modbus EEPROM 微控制器

单片机在掉电时,电压低过一定的值。执行程序代码出错或是程序指针跑飞。刚好执行EEPORM 写入操作,才会出现所说的EEPROM 数据丢失!就算是外置EEPROM 在低于正常工作电压,进行写入操作,也会出现这种情况!

关键字: EEPROM 数据丢失

存储器分为两大类: ram 和 rom ,r am就不讲了,主要讨论rom。

关键字: 存储器 EEPROM Flash

EEPROM的全称是“电可擦除可编程只读存储器”,即Electrically Erasable Programmable Read-Only Memory。是相对于紫外擦除的ROM来讲的。但是今天已经存在多种EEPROM...

关键字: EEPROM Flash 存储器

今天给小伙伴们整理了USB接口的类型,带图片说明,好辨识!

关键字: USB EEPROM

数字电源

15504 篇文章

关注

发布文章

编辑精选

技术子站

关闭