#include
#include
#define AddWr 0xae //Write Address
#define AddRd 0xaf //Read Address
//全局变量
sbit Sda=P1^2; //界说总线衔接端口
sbit Scl=P1^1;
sbit WP=P1^0;//No connect here
void mDelay(unsigned char j)//A normal delay
{
unsigned int i;
for(;j>0;j–)
{
for(i=0;i<125;i++)
{;}
}
}
//发动 IIC总线
void Start(void)
{
Sda=1;
_nop_();_nop_();
Scl=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
Sda=0;
_nop_();_nop_();_nop_();_nop_();_nop_();
Scl=0;
}
//中止 IIC总线
void Stop(void)
{
Sda=0;
_nop_();
Scl=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
Sda=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
Scl=0;
}
//应对 IIC总线
void Ack(void)
{
Sda=0;
_nop_();_nop_();_nop_();
Scl=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
Scl=0;
_nop_();_nop_();
}
//非应对 IIC总线
void NoAck(void)
{
Sda=1;
_nop_();_nop_();_nop_();
Scl=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
Scl=0;
_nop_();_nop_();
}
//发送一个字节
void Send(unsigned char Da
{
unsigned char BitCounter=8;
unsigned char temp;
do
{
temp=Da
Scl=0;
_nop_();_nop_();_nop_();_nop_();_nop_();
if((temp&0x80)==0x80)
Sda=1;
else
Sda=0;
Scl=1;
temp=Da
Da
BitCounter–;
}
while(BitCounter);
Scl=0;
}
//读一个字节并回来变量
unsigned char Read(void)
{
unsigned char temp=0;
unsigned char temp1=0;
unsigned char BitCounter=8;
Sda=1;
do
{
Scl=0;
_nop_();_nop_();_nop_();_nop_();_nop_();
Scl=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
if(Sda)
temp=temp|0x01;
else
temp=temp&0xfe;
if(BitCounter-1)
{
temp1=temp<<1;
temp=temp1;
}
BitCounter–;
}
while(BitCounter);
return(temp);
}
//write to ROM
void WrToROM(unsigned char Da
{
unsigned char i;
unsigned char *PData;
PData=Da
for(i=0;i
Start();
Send(AddWr);//Write Address
Ack();
Send(Address+i);//Write sub Address
Ack();
Send(*(PData+i));//Write Da
Ack();
Stop();
mDelay(20);
}
}
//read from ROM
void RdFromROM(unsigned char Da
{
unsigned char i;
unsigned char *PData;
PData=Da
for(i=0;i
Start();
Send(AddWr);//Write Address
Ack();
Send(Address+i);//Write sub Address
Ack();
Start();
Send(AddRd);//Read Address
Ack();
*(PData+i)=Read();//Read Da
Scl=0;
NoAck();
Stop();
}
}
void main()
{
unsigned char Number[4]={0x06,0x5b,0x4f,0x66};// 显现码值 1234
unsigned char i;
WP=0;//写保护关掉
P2=0x00;
WrToROM(Number,4,4);//写入24c02
mDelay(200);
Number[0]=0;//铲除当时数据
Number[1]=0;
Number[2]=0;
Number[3]=0;
RdFromROM(Number,4,4);//调用存储数据
while(1)
{
P0=Number[i];//显现存储数据
mDelay(200);
mDelay(200);
i++;
if(i==4)
i=0;
}
}