at24cxx.c文件中的程序#include "pbdata.h"
void I2C_EE_Init(void)
{
I2C_Init(I2C_Speed,0,I2C_DUTYCYCLE_2,I2C_ACK_CURR,I2C_ADDMODE_7BIT,16);//I2C初始化
I2C_Cmd(ENABLE);//使能I2C功能
}
void I2C_EE_BufferWrite(u8* pBuffer,u16 WriteAddr,u8 NumByte)//I2C写函数
{
while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));//获取指定的标志状态(判断总线状态)
I2C_GenerateSTART(ENABLE); //I2C起始信号
while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));//检测指定信号状态(EV5事件)如果没有发送完成在这里等待
if(EE_TYPE>AT24C16)
{
I2C_Send7bitAddress(EEPROM_ADDRESS,I2C_DIRECTION_TX);
while(!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
I2C_SendData((u8)(WriteAddr>>8));
while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));
}
else
{ //1010 000 0 I2C_Send7bitAddress(EEPROM_ADDRESS+((WriteAddr/256)<<1),I2C_DIRECTION_TX);
while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
}
I2C_SendData((u8)(WriteAddr));
while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));
while(NumByte--)
{
I2C_SendData(*pBuffer);
pBuffer++;
if(NumByte==0)
{
while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
else
{
while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));
}
}
I2C_GenerateSTOP(ENABLE);
}
void I2C_EE_BufferRead(u8* pBuffer,u16 ReadAddr,u8 NumByte)
{
while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));
I2C_GenerateSTART(ENABLE);
while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
if(EE_TYPE>AT24C16)
{
I2C_Send7bitAddress(EEPROM_ADDRESS,I2C_DIRECTION_TX);
while(!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
I2C_SendData((u8)(ReadAddr>>8));
while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));
}
else
{ //1010 000 0
I2C_Send7bitAddress(EEPROM_ADDRESS+((ReadAddr/256)<<1),I2C_DIRECTION_TX);
while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
}
I2C_SendData((u8)(ReadAddr));
while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));
I2C_GenerateSTART(ENABLE);
while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(EEPROM_ADDRESS,I2C_DIRECTION_RX);
while(!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
while(NumByte)
{
if(NumByte==1)
{
I2C_AcknowledgeConfig(I2C_ACK_NONE);
I2C_GenerateSTOP(ENABLE);
}
if(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED))
{
*pBuffer=I2C_ReceiveData();
pBuffer++;
NumByte--;
}
}
I2C_AcknowledgeConfig(I2C_ACK_CURR);
}
void AT24Cxx_Demo1(void)
{
u8 TX_buf[]="LY-STM8";//测试数据
u8 RX_buf[8];
delay_ms(2);
I2C_EE_BufferWrite(TX_buf,0x20,7);
delay_ms(20);
I2C_EE_BufferRead(RX_buf,0x20,7);
RX_buf[7]='\0';//加上'\0',数组才能正确打印出来
printf("%s\r\n",RX_buf);
}
|