void StartIIC(void)
{
SDA = 1;
IICDelay();
SCL = 1;
IICDelay();
SDA = 0;
IICDelay();
SCL = 0;
}
void EndIIC(void)
{
SDA = 0;
IICDelay();
SCL = 1;
IICDelay();
SDA = 1;
IICDelay();
SCL = 1;
}
unsigned char RecvIIC(void)
{
unsigned char Count,DataR;
SDA = 1;
for (Count = 0;Count<8;Count++)
{
SCL = 1;
DataR = DataR << 1;
if (SDA)
{
DataR |= B00000001;
}
SCL = 0;
IICDelay();
}
return DataR;
}
unsigned char SendIIC(unsigned char idata DataS)
{
unsigned char Count;
for (Count = 0;Count<8;Count++)
{
SDA = DataS & 0x80;
IICDelay();
SCL = 1;
IICDelay();
SCL = 0;
DataS = DataS << 1;
}
IICDelay();
SDA = 1;
IICDelay();
SCL = 1;
IICDelay();
if (SDA)
{
SCL =0;
return FALSE;
}
SCL =0;
return TRUE;
}
void IICDelay(void)
{
}
unsigned char ReadIIC(unsigned char* DataIn,unsigned char Address,unsigned char Length)
{
if (Length == 0)
{
return TRUE;
}
StartIIC();
if (!SendIIC(EEP_SLAVEADDR_WRITE))
{
EndIIC();
return FALSE;
}
SendIIC(Address);
StartIIC();
SendIIC(EEP_SLAVEADDR_READ);
while (Length > 1)
{
*DataIn = RecvIIC();
SDA = 0;
IICDelay();
SCL = 1;
IICDelay();
SCL = 0;
Length--;
DataIn++;
}
*DataIn = RecvIIC();
SDA = 1;
IICDelay();
SCL = 1;
IICDelay();
SCL = 0;
EndIIC();
return TRUE;
}
unsigned char WriteIIC(unsigned char* DataIn,unsigned char Address,unsigned char Length)
{
unsigned char idata Count;
if (Length == 0)
{
return TRUE;
}
StartIIC();
SendIIC(EEP_SLAVEADDR_WRITE);
SendIIC(Address);
while(1)
{
if (!SendIIC(*DataIn))
{
EndIIC();
return FALSE;
}
Address++;
DataIn++;
if (Count++ == Length)
{
EndIIC();
if (WaitPoll())
{
return TRUE;
}
else
{
return FALSE;
}
}
if ((Address & B00000111) == 0)
{
EndIIC();
WaitPoll();
StartIIC();
SendIIC(EEP_SLAVEADDR_WRITE);
SendIIC(Address);
}
}
}
unsigned char WaitPoll(void)
{
unsigned char Retry;
Retry = 0xff;
while(Retry--)
{
StartIIC();
if (SendIIC(EEP_SLAVEADDR_WRITE))
{
EndIIC();
return TRUE;
}
}
EndIIC();
return FALSE;
} |