/********************************************************************************************************
**函数信息 :I2CTXEmptyCheck(I2C_TypeDef *I2Cx)
**功能描述 :检查发送中断标志位
**输入参数 :I2C_TypeDef *I2Cx,选择I2C1,I2C2
**输出参数 :无
********************************************************************************************************/
void I2CTXEmptyCheck(I2C_TypeDef *I2Cx)
{
while(1)
{
if(I2C_GetFlagStatus(I2Cx, I2C_FLAG_TX_EMPTY))
{
break;
}
}
}
/********************************************************************************************************
**函数信息 :I2CTXEmptyCheck(I2C_TypeDef *I2Cx)
**功能描述 :检查接收中断标志位
**输入参数 :I2C_TypeDef *I2Cx,选择I2C1,I2C2
**输出参数 :无
********************************************************************************************************/
void I2CRXFullCheck(I2C_TypeDef *I2Cx)
{
while(1)
{
if(I2C_GetFlagStatus(I2Cx, I2C_FLAG_RX_FULL))
{
break;
}
}
}
/********************************************************************************************************
**函数信息 :I2CTXByte(I2C_TypeDef *I2Cx,unsigned short cmd,unsigned char temp)
**功能描述 :I2C发送数据
**输入参数 :I2C_TypeDef *I2Cx,选择I2C1,I2C2;cmd;temp
**输出参数 :无
********************************************************************************************************/
void I2CTXByte(I2C_TypeDef *I2Cx,unsigned short cmd,unsigned char temp)
{
I2C_SendData(I2Cx, temp);
I2CTXEmptyCheck(I2Cx);
}
/********************************************************************************************************
**函数信息 :I2CRXByte(I2C_TypeDef *I2Cx)
**功能描述 :I2C接收数据
**输入参数 :I2C_TypeDef *I2Cx,选择I2C1,I2C2
**输出参数 :temp
********************************************************************************************************/
unsigned char I2CRXByte(I2C_TypeDef *I2Cx)
{
unsigned short temp;
I2CRXFullCheck(I2Cx);
temp = I2C_ReceiveData(I2Cx);
return (unsigned char)temp;
}
//============================================================================
//
//
//
//============================================================================
void I2C_WriteByte(unsigned char dev_addr, unsigned int addr,unsigned char Byte)
{
I2C_PageWrite(dev_addr,addr,&Byte,1);
}
unsigned char I2C_ReadByte(unsigned char dev_addr, unsigned int addr)
{
unsigned char Byte;
I2C_PageRead(dev_addr,addr,&Byte,1);
return Byte;
}
uint32_t I2C_PageWrite(unsigned char dev_addr,unsigned int addr,unsigned char *str,unsigned int num)
{
unsigned short temp;
unsigned short i;
unsigned char *p;
p = str;
I2C_Cmd(I2C1, DISABLE);
I2C_Send7bitAddress(I2C1, dev_addr<<1, I2C_Direction_Transmitter);
I2C_Cmd(I2C1, ENABLE);
temp = ((addr) & 0xff);
I2CTXByte(I2C1,CMD_WRITE,temp); //tx memory addr
for(i=0;i<num;i++)
{
temp = *p;
p++;
if(i==(num-1))
I2C1->IC_DATA_CMD =temp| 0x200; // //muaul set stop flag
else
I2CTXByte(I2C1,CMD_WRITE,temp); //tx data
}
return 0;
}
uint32_t I2C_PageRead(unsigned char dev_addr,unsigned int addr,unsigned char *str,unsigned int num)
{
unsigned char temp=0;
unsigned short i;
I2C_Cmd(I2C1, DISABLE);
I2C_Send7bitAddress(I2C1, dev_addr<<1, I2C_Direction_Transmitter);
I2C_Cmd(I2C1, ENABLE);
temp = ((addr) & 0xff);
I2CTXByte(I2C1,CMD_WRITE,temp); //tx memory addr
I2C_Send7bitAddress(I2C1, dev_addr<<1, I2C_Direction_Receiver);
for(i=0;i<num;i++)
{
str[i] = I2CRXByte(I2C1);
}
I2C1->IC_DATA_CMD = 0x200; // //set stop flag
return 0;
}