Home > database >  I2c (c51)
I2c (c51)

Time:09-26

#include//define all monolithic SFR
#include
# define AT24C02_ADDR 0 xa0//AT24C02 address 0 xa0 give AT24C02
# define unit unsigned int
# define uchar unsigned char
Sbit SDA=P2 ^ 0;
Sbit SCL=P2 ^ 1;
Void delay_5us ()
{
_nop_();
}
///////////////////////////////////////////////////
Void I2C_init ()//IIC initialization
{
SDA=1;
_nop_();
SCL=1;
_nop_();
}
Void I2C_Start ()//start signal
{
SCL=1;
_nop_();
SDA=1;
Delay_5us ();
SDA=0;
Delay_5us ();
}
Void I2C_Stop ()//stop
{
SCL=1;
_nop_();
SDA=0;
Delay_5us ();
SDA=1;
Delay_5us ();
}
////////////////////////////////////////////////////////
Void Master_ACK bit (I)//Master_ACK to send response signal
{//1 to send reply
SCL=0;//only for low, the clock bus SDA change of cable are valid
_nop_();
If (I)//if I=1
{
SDA=0;//send a response signal
}
The else
{
SDA=1;
}
_nop_();
SCL=1;//up clock bus SDA data can be read away
_nop_();
SCL=0;
_nop_();
SDA=1;
_nop_();
}

Bit Text_ACK ()//testing from machine reply
{
SCL=1;
Delay_5us ();
If (SDA)//host to send response signal then cease lower SCL sends a signal to the end of the data transmission
{//host sends a frame data to each data from the machine to test for each frame of the ninth reply
//the SDA line low from machine representative reply
SCL=0;
I2C_Stop ();
Return (0);
}

The else
{
SCL=0;
_nop_();
The return (1);
}
}
Void I2C_send_byte (uchar byte)//send data
{
uchar i;
for(i=0; i<8; I++)
{
SCL=0;
_nop_();
If (byte& 0 x80)/0000/1000
{
SDA=1;
_nop_();
}
The else
{
SDA=0;
_nop_();
}
SCL=1;
Byte<=1;
}
SCL=0;
SDA=1;
}
//IIC read a byte
Uchar I2C_rend_byte ()
{

Uchar I, dat.
SCL=0;
_nop_();
SDA=1;
_nop_();
for(i=0; i<8; I++)
{
SCL=1;
_nop_();
i<=1;
If (SDA)
{
Dat=dat | 0 x01;

}
The else
{
Dat=dat& 0 xfe;
}
_nop_();
SCL=0;
_nop_();
}
Return (dat);

}

//send data
Bit I2C_TransmitData (uchar ADDR, uchar DAT)
{
I2C_Start ();
I2C_send_byte (AT24C02_ADDR + 0);//read/write a
if(! Text_ACK ())
{
Return (0);
}
I2C_send_byte (ADDR);//write the 255th unit address
if(! Text_ACK ())
{
Return (0);
}
I2C_send_byte (DAT);
if(! Text_ACK ())
{
Return (0);
}
I2C_Stop ();
The return (1);
}
//receiving data
Uchar I2C_ReceiveData (uchar ADDR)
{
Uchar DAT.
I2C_Start ();//start signal
_nop_();
I2C_send_byte (AT24C02_ADDR + 0);//from the machine address
if(! Text_ACK)//measuring response
{
Return (0);
}
I2C_send_byte (ADDR);//read unit data
if(! Text_ACK)
{
Return (0);
}
Master_ACK (0);//send the reply
I2C_Start ();
I2C_send_byte (AT24C02_ADDR + 1);
if(! Text_ACK)
{
Return (0);
}
DAT=I2C_rend_byte ();
Master_ACK (0);
I2C_Stop ();
Return (DAT);
}
Void main ()
{
A first address +//s writing, speaking, reading and writing direction from machine response to the address of the device from the machine response to write data to send the response
I2C_init ();
I2C_TransmitData (255, 0 xF0);//to the inside of the I2C 255 unit sends a 0 xfe
_nop_();
///////////////////read process of data
//s from the machine address +, speaking, reading and writing direction a wait reply read unit class data sending response or no response from the machine address + 1 waiting for reply
The P2=I2C_ReceiveData (255);//out of the 255 units receive
while(1);
} I don't feel any problem?
  • Related