#include <INTRINS.H>
#include <stdio.h>
#include <reg51.h>
sbit SCK=0x91;//时钟线端口
sbit SDA=0x90;//数据线端口
#define
#define
#define
//开始信号
//在时钟线为高电平时,数据线由高向低跳变.
void StartI2C(void)
{
}
//停止信号
//在时钟线为高电平时,数据线由低向高跳变.
void StopI2C(void)
{
}
//发送一个字节.
bit SendChar(unsigned char SendData)
{
//发送一个字节后,读取来自I2C芯片的响应.
//即在发送完8个字节后,在第九个时钟内,SDA总线应为低电平,表示I2C芯片已经读取了数据.
//否则为高电平.即发送收败.
}
//在读取完来自I2C的8位数据后,发出读取正确位.
//即发送一个低电平,表示已经读取到来自I2C的数据,并允许I2芯片发送下一字节.
void ACK(void)
{
}
//在读取完来自I2C的8位数据后,发送不响应位.
//即发送一个高电平,表示已经读取完来自I2C的数据,并且不允许I2C发送下一字节.
void NAK(void)
{
}
//读入一个字节的数据.
unsigned char ReadChar(void)
{
}
//以下所有为测试使用...
//应注意,在读I2C芯片时,在发送完I2C片内地址后,
//一定要发送一个重开始信号,然后再发送读命令.
bit WriteI2C(unsigned char Address,unsigned char WriteData)
{
}
unsigned char ReadI2C(unsigned char Address)
{
}
void Delay(unsigned int ms)
{
}
void main(void)
{
}
I2C 程序(已经验证 )
#define _I2C_C_
#include "include_all.h"
/**************************
AT24C02????
//Author: Yuki
//Last modified : 2008-6-8
**************************/
#define NOPS();{_nop_();_nop_();_nop_();_nop_();_nop_();}
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);
}
}