《電子技術應用》
您所在的位置:首頁 > 模擬設計 > 設計應用 > 如何通過MAX2990 I2C接口向標準EEPROM (24C04)連接
如何通過MAX2990 I2C接口向標準EEPROM (24C04)連接
摘要: 本文介紹了如何通過MAX2990電力線通信調(diào)制解調(diào)器的I2C接口與外部EEPROM24C04連接,并給出了相應的固件例程。I²C總線受控于MAX2990(主機),24C04EEPROM為從機器件。以下框圖給出了本文示例的硬件配置。
Abstract:
Key words :

  引言

  本文介紹了如何通過MAX2990電力線通信調(diào)制解調(diào)器的I2C接口與外部EEPROM 24C04連接,并給出了相應的固件例程。I²C總線受控于MAX2990 (主機),24C04 EEPROM為從機器件。以下框圖給出了本文示例的硬件配置。

  

硬件配置 www.elecfans.com

 

  固件說明

  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é):

  1. EEPROM的I²C總線地址(這里為0xA0)
  2. EEPROM存儲器的地址
  3. 數(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

讀模式

讀取我們寫入EEPROM的數(shù)據(jù)時,為24C04留出足夠的寫時間非常關鍵。通常在“停止條件”后留出幾個毫秒的時間,請參考數(shù)據(jù)資料,確認您的時間設置符合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

現(xiàn)在,我們可以驗證一下用于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
}
此內(nèi)容為AET網(wǎng)站原創(chuàng),未經(jīng)授權(quán)禁止轉(zhuǎn)載。
主站蜘蛛池模板: 天天躁日日躁aaaaxxxx| 男女一进一出抽搐免费视频| 在线观看国产日本| 久久99精品九九九久久婷婷| 狼人香蕉香蕉在线28-百度| 国产在线精品国自产拍影院午夜| 99久久免费中文字幕精品| 无码任你躁久久久久久久| 亚洲三级在线免费观看| 粉色视频免费试看| 国产乱码一区二区三区| 中文字幕在线色| 在线观看黄色毛片| 中文字幕の友人北条麻妃| 日韩色视频一区二区三区亚洲| 亚洲精品成人久久| 精品欧美一区二区精品久久| 国产日产欧产精品精品电影| 91精品国产高清| 无码高潮少妇毛多水多水免费| 亚洲最大视频网| 男女一级爽爽快视频| 国产一级一国产一级毛片| 69堂午夜精品视频在线| 国内色综合精品视频在线| 一个人看的www在线观看免费| 日本欧美在线观看| 亚洲区精选网址| 看久久久久久A级毛片| 国产三级小视频在线观看| 亚洲欧美日韩国产vr在线观| 天堂在线www| 久9re热这里精品首页| 日韩欧美在线观看| 亚洲人成人一区二区三区| 精品一区二区三区AV天堂| 国产伦精品一区二区三区视频小说| 你懂的网址免费国产| 大屁股熟女一区二区三区| 一级毛片私人影院| 日产精品一二三四区国产|