#ifndef OS_MASTER_FILE
#include "includes.h"
#endif
void i2c_start(void)reentrant
{
scl = 0;
_nop_();
sda = 1;
scl = 1;
_nop_();
_nop_();
sda = 0;
_nop_();
_nop_();
scl = 0;
}
void i2c_stop(void)reentrant
{
scl = 0;
sda = 0;
_nop_();
scl = 1;
_nop_();
sda = 1;
_nop_();
}
void i2c_write(INT8U temp)reentrant
{
INT8U i;
for(i=0;i<8;i++)
{
if(0x80 == ((temp<<i)&0x80))
sda = 1;
else
sda = 0;
scl = 1;
_nop_();
_nop_();
scl = 0;
}
sda = 1;
scl = 1;
_nop_();
_nop_();
scl = 0;
}
INT8U i2c_read(INT8U ack)reentrant
{
INT8U i,temp;
sda = 1;
for(i=0;i<8;i++)
{
temp <<= 1;
scl = 1;
_nop_();
_nop_();
if(1 == sda)
temp |= 0x01;
else
temp &= 0xfe;
scl = 0;
}
if(1 == ack)
sda = 0;
else
sda = 1;
scl = 1;
_nop_();
_nop_();
scl = 0;
_nop_();
sda = 1;
return temp;
}
INT8U i2c_read_char(INT8U addr)reentrant
{
INT8U temp;
i2c_start();
i2c_write(0x64); //Rx8025设备地址(写)
i2c_write(addr);
i2c_start();
i2c_write(0x65); //Rx8025设备地址(读)
temp = i2c_read(1);
i2c_stop();
return temp;
}
void i2c_write_char(INT8U addr,INT8U data_)reentrant
{
i2c_start();
i2c_write(0x64);
i2c_write(addr);
i2c_write(data_);
i2c_stop();
}
void i2c_read_multi(INT8U addr,INT8U len,INT8U temp[])reentrant
{
INT8U i;
i2c_start();
i2c_write(0x64);
i2c_write(addr);
i2c_start();
i2c_write(0x65);
for(i=0;i<len-1;i++)
temp[i] = i2c_read(1);
i = len-1;
temp[i] = i2c_read(0);
i2c_stop();
}
void i2c_write_multi(INT8U addr,INT8U len,INT8U temp[])reentrant
{
INT8U i;
i2c_start();
i2c_write(0x64);
i2c_write(addr);
for(i=0;i<len;i++)
i2c_write(temp[i]);
i2c_stop();
}