uchar idata buff[32]; //save the byte read out form iic device in test operation
uchar idata readbuff[32];//测试数组,暂存写入读出数据
//uchar writebuff[32];
//===============================
sbit SDA = P4^5; //THE SDA BIT IS PORT 4 BIT 2
sbit SCL = P4^4; //THE SCL BIT IS PORT 1 BIT 0
//===============================
//define a bit_operation byte to use in shift operation
//use this mode can achieve high operation speed
uchar bdata bbyte;//定义位操作用数组,采用此方法可提高位操作速度
sbit a0 = bbyte^0;
sbit a1 = bbyte^1;
sbit a2 = bbyte^2;
sbit a3 = bbyte^3;
sbit a4 = bbyte^4;
sbit a5 = bbyte^5;
sbit a6 = bbyte^6;
sbit a7 = bbyte^7;
//========================================
bit IFACK; //record the SDA state to confirm if ACK has happened
bit NO_ACK; //no ack flag
//FUNCTION:ROUTES TO PROVIDE A START SIGNAL
void start(void)
{
SCL=0;SDA=1;SCL=1;SDA=0;SCL=0;
}
//=======================================
//FUNCTION:ROUTES TO PROVIDE A STOP SIGNAL
void stop(void)
{
SCL=0;SDA=0;SCL=1;SDA=1;SCL=0;
}
//=====================================
//FUNCTION:ROUTES TO PROVIDE ACK SINGAL
void ack(void)
{
SCL=0;SDA=0;SCL=1;SCL=0;
}
//=====================================
//FUNCTION:ROUTES TO RELEASE THE SDA TO RECEIVE A ACK SIGNAL
// OR TO PROVIDE A NO_ACK SIGNAL
//type=1等待应答信号
//type=0 产生无应答信号
void nack(uchar type)
{
SCL=0;SDA=1;SCL=1;
IFACK=SDA; SCL=0;
if(type)
{
if(IFACK)//如果无应答信号,则置标志位NO_ACK,程序中止
{
NO_ACK=1;//用户可以加入自己的异常处理程序
//while(1);
}
else NO_ACK=0;
}
}
//=======================================================
//FUNCTION:THE IIC DEVICE SHIFT OUT A BYTE TO THE MASTER
unsigned char inbyte(void)
{ //从IIC器件中读出数据
unsigned char get_value,i;
get_value=0;
SCL=0;
for(i=0;i<8;i++)
{
SDA=1;
SCL = 1;
get_value <<= 1;//首先接收到的为最高位
if(SDA==1)get_value += 1;
SCL = 0;
}
return get_value;
}
//=======================================================
//FUNCTION:THE IIC DEVICE SHIFT IN A BYTE FROM THE MASTER
void outbyte(unsigned char outdata) {//将数据写入IIC器件