#include "macrodriver.h"
#include "main.h"
uchar ack=0;
void DlyNop(void);
void Start_I2c()
{ TRISE_SDA=0;
SDA=1;
DlyNop();
SCL=1;
DlyNop();
SDA=0;
DlyNop();
}
void Stop_I2c()
{TRISE_SDA=0;
SDA=0;
DlyNop();
SCL=1;
DlyNop();
SDA=1;
DlyNop();
}
void SendByte(uchar Data)
{
uchar BitCnt=8;
uchar temp;
do{
temp=Data;
SCL=0;
DlyNop();
if((temp&0x80)==0x80)
SDA=1;
else SDA=0;
DlyNop();
SCL=1;
temp=Data<<1; /*RLC*/
Data=temp;
BitCnt--;
}while(BitCnt);
DlyNop();
SCL=0;
}
uchar RcvByte()
{
uchar retc=0;
uchar BitCnt=8;
uchar temp1=0;
SDA=1;
TRISE_SDA=1;
do{
SCL=0;
DlyNop();
SCL=1;
DlyNop();
if(SDA) retc=retc|0x01;
else retc=retc&0xfe;
if(BitCnt-1) { temp1=retc<<1; retc=temp1; }
BitCnt--;
}while(BitCnt);
TRISE_SDA=0;
DlyNop();
return(retc);
}
void Ack_I2c(uchar a)
{
TRISE_SDA=0;
if(a==0)SDA=0;
else SDA=1;
DlyNop();
SCL=1;
DlyNop();
SCL=0;
DlyNop();
}
uchar Read_eeprom(uchar sla,uchar suba,uchar *s,uchar no)
{
uchar i;
for(i=0;i<no;i++)
{ Start_I2c();
SendByte(sla-1);
Ack_I2c(0);
SendByte(suba+i);
Ack_I2c(0);
Start_I2c();
SendByte(sla);
Ack_I2c(0);
*s=RcvByte();
SCL=0;
s++;
Ack_I2c(1);
Stop_I2c();
}
return(1);
}
uchar Write_eeprom(uchar sla,uchar suba,uchar *s,uchar no)
{
uchar i;
Start_I2c();
SendByte(sla);
Ack_I2c(0);
SendByte(suba);
Ack_I2c(0);
for(i=0;i<no;i++)
{
SendByte(*s);
s++;
Ack_I2c(0);
DlyNop();DlyNop();DlyNop();DlyNop();
}
Stop_I2c();
return(1);
}
void DlyNop(void)
{
NOP();NOP();NOP();NOP();
NOP();NOP();NOP();NOP();
}