F28069芯片本身的I2C模块对eeprom芯片AT24C32进行读写操作

         F28069芯片与eeprom芯片连接框图

1、F28069芯片的I2C模块初始化设置

芯片系统时钟为90mhz, I2C的传输速率为400Khz;

static void sI2c_Init(void)
{
    //    // Initialize I2C    //    
    I2caRegs.I2CSAR = 0x0050;				// Slave address - EEPROM control code  
    
	I2caRegs.I2CPSC.all = 8;				// 10mhz  // Prescaler - need 7-12 Mhz on module clk    
	
	I2caRegs.I2CCLKL = 10;					// NOTE: must be non zero   
	I2caRegs.I2CCLKH = 5;					// NOTE: must be non zero      // speed  400khz
	
//	I2caRegs.I2CIER.all = 0x24;				// Enable SCD & ARDY interrupts   
	
	//    // Take I2C out of reset. Stop I2C when suspended    //   
	I2caRegs.I2CMDR.all = 0x0020;	   
	
	//I2caRegs.I2CFFTX.all = 0x6000;	// Enable FIFO mode and TXFIFO   
	//I2caRegs.I2CFFRX.all = 0x2040;	// Enable RXFIFO, clear RXFFINT,
}

2、eeprom芯片AT24C32的写操作

        AT24C32的写操作时序如下:

        

        F28069芯片的写函数代码如下:

/*****************************************************************************
Function name : I2C_EepromDataWrite
Description   :  Eeprom data write max size 32   for AT24C32
*****************************************************************************/
uint16_t I2C_EepromDataWrite(uint16_t uwMemoryAddr, uint16_t uwLenth, uint16_t* pData)
{
	uint16_t i;
	
	// Wait until the STP bit is cleared from any previous master communication.   
    // Clearing of this bit by the module is delayed until after the SCD bit is    
    // set. If this bit is not checked prior to initiating a new message, the   
    // I2C could get confused.     
    if (I2caRegs.I2CMDR.bit.STP == 1)   
	{        
		return 1;    
	} 
	
	I2caRegs.I2CSAR = 0x0050;	// Slave address - EEPROM control code  1010000   
	
	// Check if bus busy   
	if (I2caRegs.I2CSTR.bit.BB == 1)   
	{        
		return 2;    
	}

	I2caRegs.I2CCNT = uwLenth + 2;
    I2caRegs.I2CMDR.all = 0x6E20;	
	for(i=0;i<2;i++)
	{
		while(!I2caRegs.I2CSTR.bit.XRDY);
		if(0==i)
		{
			I2caRegs.I2CDXR = (uwMemoryAddr>>8) & 0xFF;   //MemoryHighAddr
		}
		else
		{
			I2caRegs.I2CDXR = uwMemoryAddr & 0xFF;   //MemoryLowAddr
		}
	}
	
	for(i=0;i<uwLenth;i++)
	{
		while(!I2caRegs.I2CSTR.bit.XRDY);
		
		I2caRegs.I2CDXR = *(pData + i);   		
	}

	while(I2caRegs.I2CSTR.bit.BB);

	return 0;    
	

}

 3、eeprom芯片AT24C32的读操作

        AT24C32读指定地址的操作时序如下:

 F28069芯片的读函数代码如下:

/*****************************************************************************
Function name : PowerOnReadEepromData
Description   : PowerOn Read Eeprom data
*****************************************************************************/
void PowerOnReadEepromData(uint16_t uwMemoryAddr, uint16_t uwLenth, uint16_t* pScr)
{
	uint16_t i;
	uint16_t j = 0;
	uint16_t I2CRecBuf[50] = {0};

	//step 1	
	I2caRegs.I2CSAR = 0x0050;		// Slave address - EEPROM control code  1010000
	I2caRegs.I2CCNT = 2;
    I2caRegs.I2CMDR.all = 0x6620;	
	for(i=0;i<2;i++)
	{
		while(!I2caRegs.I2CSTR.bit.XRDY);
		if(0==i)
		{
			I2caRegs.I2CDXR = (uwMemoryAddr>>8) & 0xFF;   //MemoryHighAddr
		}
		else
		{
			I2caRegs.I2CDXR = uwMemoryAddr & 0xFF;   //MemoryLowAddr
		}
	}
	sDelay1Us(10);
	
	I2caRegs.I2CCNT = uwLenth;
    I2caRegs.I2CMDR.all = 0x6C20;	
	for(i=0;i<uwLenth;i++)
	{
		while(!I2caRegs.I2CSTR.bit.RRDY);
		I2CRecBuf[i] = I2caRegs.I2CDRR; 
	}
	
	for(i=0;i<uwLenth;i=i+2)
	{
		*(pScr + j)  = (I2CRecBuf[i]<<8)+I2CRecBuf[i+1];
		j++;
	}
	j = 0;
		
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值