仅供参考~~
#define _I2C_C_
#include "include_all.h"
sbit SCL = P3^4; //pin16
sbit SDA = P3^5; //pin17
sbit TLED = P3^6;
unsigned char SystemError;
void Delay(int TIME)
{
while(TIME--);
}
//---------------------------------
// Function: void I2C_Start(void)
// Discrption:
//----------------------------------
void I2C_Start(void)
{
SDA = 1;
SCL = 1;
NOPS(); //Initial
SDA = 0;
NOPS(); //START
SCL = 0;
}
//---------------------------------
// Function: void I2C_Stop(void)
// Discrption:
//----------------------------------
void I2C_Stop(void)
{
SCL = 0;
SDA = 0;
NOPS(); //Initial
SCL = 1;
NOPS(); //STOP
SDA = 1;
}
//---------------------------------
// Function: void I2C_Start(void)
// Discrption:
//----------------------------------
WaitAck(void)
{
unsigned char errtime = 255;
SDA = 1;
NOPS();
SCL = 1;
NOPS();
SystemError = 0x10;
while(SDA)
{
errtime--;
if(!errtime)
{
I2C_Stop();
SystemError = 0x11;
return;
}
}
SCL = 0;
}
//---------------------------------
// Function: void SendAck(void)
// Discrption:
//----------------------------------
void SendAck(void)
{
SDA =0 ;
NOPS();
SCL = 1;
NOPS();
SCL = 0;
}
//---------------------------------
// Function: void SendNotAck(void)
// Discrption:
//----------------------------------
void SendNotAck(void)
{
SDA =1 ;
NOPS();
SCL = 1;
NOPS();
SCL = 0;
}
//-----------------------------------------------------
// Function: void I2CSendByte(unsigned char send_data)
// Discrption:
//-----------------------------------------------------
void I2CSendByte(unsigned char send_data)
{
unsigned char count = 8;
for(count = 0;count<8;count++)
{
SCL = 0;
_nop_();
SDA = (bit)(send_data & 0x80);
send_data <<= 1;
NOPS();
SCL = 1;
NOPS();
}
SCL = 0;
}
//-----------------------------------------------------
// Function: unsigned char I2CReceiveByte(void)
// Discrption:
//-----------------------------------------------------
unsigned char I2CReceiveByte(void)
{
unsigned char count = 8;
unsigned char dat = 0;
SDA = 1;
for(count = 0;count<8;count++)
{
dat <<= 1;
SCL = 0;
NOPS();
SCL = 1;
NOPS();
dat |= SDA;
}
SCL = 0;
return(dat);
}
//--------------------------------------------------------------------------
// Function: void WriteCharToChip(unsigned int address,unsigned char dat)
// Discrption:
//--------------------------------------------------------------------------
void WriteCharToChip(unsigned int address,unsigned char dat)
{
I2C_Start();
I2CSendByte(0xA0);
WaitAck();
I2CSendByte((char)(address)>>8); //????AT24C02,AT24C64,AT24C128,AT24C256??,??????????
Delay(300);
WaitAck();
I2CSendByte((char)(address));
WaitAck();
I2CSendByte(dat);
Delay(300);
WaitAck();
I2C_Stop();
}
//--------------------------------------------------------------------------
// Function: unsigned char ReadCharFromChip(unsigned int Address)
// Discrption:
//--------------------------------------------------------------------------
unsigned char ReadCharFromChip(unsigned int Address)
{
unsigned char recv_data;
I2C_Start();
I2CSendByte(0xA0);
WaitAck();
I2CSendByte((char)(Address>>8));//????AT24C02,AT24C64,AT24C128,AT24C256??,??????????
WaitAck();
I2CSendByte((char)(Address));
WaitAck();
I2C_Start();
I2CSendByte(0xA1);
WaitAck();
recv_data = I2CReceiveByte();
SendNotAck();
I2C_Stop();
return(recv_data);
}
main()
{
unsigned char Read_data;
for(;;)
{
WriteCharToChip(0x10,0xA3);
Read_data = ReadCharFromChip(0x10);
}
}