//---初始化-------------------------------------------------//
void I2C_EEInit(void)
{
u8 Input_Clock = 0x0;
Input_Clock = CLK_GetClockFreq()/1000000;
I2C_Cmd(ENABLE);
I2C_Init(I2C_Speed, I2C1_SLAVE_ADDRESS7, I2C_DUTYCYCLE_2,I2C_ACK_CURR, I2C_ADDMODE_7BIT, Input_Clock);
}
//-----向器件指定地址写一个字节---------------------------------------//
void I2C_EE_ByteWrite(u16 WriteAddr,u8 Data)
{
I2C_GenerateSTART(ENABLE);while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(0xa0,I2C_DIRECTION_TX); while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ));
I2C_SendData(WriteAddr>>8);while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));
I2C_SendData(WriteAddr&0xff);while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));
I2C_GenerateSTART(ENABLE); while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(0xa0, I2C_DIRECTION_RX);while (!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);
I2C_SendData(Data); while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(ENABLE);
}
void I2C_EE_BufferWrite(u8 * pBuffer, u16 WriteAddr, u16 NumByteToWrite )
{
u16 i;
for(i=0;i<NumByteToWrite;i++)
{
I2C_EE_ByteWrite(WriteAddr+i,* (pBuffer+i));
}
}
//读回存放数组,器件ID,寄存器起始地址,数据数量
void I2C_EE_BufferRead(u8* pBuffer,u8 ReadAddr, u8 NumByteToRead)
{
while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));
I2C_GenerateSTART(ENABLE); while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT ));
I2C_Send7bitAddress(0xa0, I2C_DIRECTION_TX); while (!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
I2C_Cmd(ENABLE);
I2C_SendData(ReadAddr>>8);while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(ReadAddr&0xff);while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(ENABLE); while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(0xa0, I2C_DIRECTION_RX); while (!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
while(NumByteToRead)
{
if(NumByteToRead == 1)
{
I2C_AcknowledgeConfig(I2C_ACK_NONE);
I2C_GenerateSTOP(ENABLE);
}
if(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED))
{
*pBuffer = I2C_ReceiveData();
pBuffer++;
NumByteToRead--;
}
}
I2C_AcknowledgeConfig(I2C_ACK_CURR);
}
固件库 version V2.1.0
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
---------------------------------
原子哥,你这个代码的延时函数是怎样的?需要跟晶振对应吗?
一周热门 更多>