void IIC_Start(void)
{
SDA_Output();
SDA=1;
NOP();
SCL=1;
NOP();
SDA=0;
NOP();
}
/***************停止信号**************/
void IIC_Stop(void)
{
SDA_Output();
SDA=0;
NOP();
SCL=1;
NOP();
SDA=1;
NOP();
}
/***********等待应答信号***********/
void IIC_Responsion(void)
{
unsigned char i;
SDA_Input();
SCL=1;
NOP();
while((SDA==1)&&(i<250))i++;
SCL=0;
NOP();
}
/************写一个字节************/
void IIC_Write_byte(unsigned char date)
{
unsigned char i,temp;
SDA_Output();
temp=date;
for(i=0;i<8;i++)
{
SCL=0;
NOP();
if(temp&0x80)
{
SDA = 1;
}
else
{
SDA = 0;
}
temp=temp<<1;
NOP();
SCL=1;
NOP();
}
SCL=0;
NOP();
SDA=1;
NOP();
}
/************读一个字节************/
unsigned char IIC_Read_byte(void)
{
unsigned char i,temp;
SDA=1;
SDA_Input();
delay_ms(2);
NOP();
for(i=0;i<8;i++)
{
SCL=0;
NOP();NOP();NOP();
temp=(temp<<1)|SDA;
SCL=1;
NOP();NOP();NOP();
}
SCL=0;
NOP();
return temp;
}
/*********对任意地址写数据*********/
void IIC_Write_add(unsigned int address,unsigned char date)
{
IIC_Start();
IIC_Write_byte(Device_Adress);
IIC_Responsion();
IIC_Write_byte((address>>8)&0xff);
IIC_Responsion();
IIC_Write_byte( address&0xff);
IIC_Responsion();
IIC_Write_byte(date);
IIC_Responsion();
IIC_Stop();
}
/*********对任意地址读数据*********/
unsigned char IIC_Read_add(unsigned int address)
{
unsigned char date;
// In_Put Mode
IIC_Start();
IIC_Write_byte(Device_Adress);
IIC_Responsion();
IIC_Write_byte((address>>8)&0xff);
IIC_Responsion();
IIC_Write_byte( address&0xff);
IIC_Responsion();
IIC_Start();
IIC_Write_byte(Device_Adress|Read_Single);
IIC_Responsion();
date=IIC_Read_byte();
IIC_Stop();
return date;
}
|