#define LOW 0
#define HIGH 1
#define EXBUFF_LEN 100
unsigned char exbuff[EXBUFF_LEN]={0};
#define SDA_SET_AS_INPUT {PE_DDR2=0;}
#define SCL_SET_AS_INPUT {PE_DDR1=0;}
#define test_GET_SDA_VALUE PE_IDR2
#define test_GET_SCL_VALUE PE_IDR1
/*print from I2C Start to I2C End ,then return */
void PrintI2C(void)
{
unsigned char SDA_last,SDA_current;
unsigned char SCL_last,SCL_current;
unsigned char temp,stopState=0;
unsigned char inbyte=0;
unsigned int i,j,k;
static unsigned char ptr=0;
//PrintString("PrintI2C()...\n") ;
SDA_SET_AS_INPUT;
SCL_SET_AS_INPUT;
while(!test_GET_SDA_VALUE||!test_GET_SCL_VALUE);
//PrintString("two bus ready\n");
while(1)
{
//wait start
while(1)
{
if(test_GET_SCL_VALUE==HIGH && test_GET_SDA_VALUE==LOW)
{
//PrintString("st\n");
break;
}
}
//printf("wait two line low\n");
while(test_GET_SDA_VALUE||test_GET_SCL_VALUE);
while(1)
{
for(i=0;i<8;i++)
{
//printf("wait scl 1\n");
while(!test_GET_SCL_VALUE);//wait high
temp=test_GET_SDA_VALUE;
if(temp!=0)
{
inbyte |= (1<<(7-i));
}
//printf("wait scl 0\n");
while(test_GET_SCL_VALUE)//wait low
{
if(temp==0 && test_GET_SDA_VALUE==1)
{
//stop
return;
}
else if(temp==1 && test_GET_SDA_VALUE==0)
{
//restart
}
}
}
//PrintStringNumberHex(",",inbyte);
#if 1
exbuff[ptr++]=inbyte;
if(ptr==EXBUFF_LEN)
{
for(j=0;j<EXBUFF_LEN;j++)
{
PrintStringNumberHex(",",exbuff[j]);
}
while(1);
}
#endif
//check ACK or NACK
while(!test_GET_SCL_VALUE);//wait high
while(test_GET_SCL_VALUE); //wait low
//ack end
inbyte=0;
}
}
}