【文件属性】:
文件名称:基于I2C总线的EEPROM AT24C02A
文件大小:2KB
文件格式:C
更新时间:2012-10-20 04:50:47
I2C总线协议 EEPROM
#include
#define uchar unsigned char
sbit sda=P1^0;
sbit scl=P1^1;
uchar a;
void delay()
{ ;; }
void start() //开始信号
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop() //停止
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void respons() //应答
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<250))i++;
scl=0;
delay();
}
void init()
{
sda=1;
delay();
scl=1;
delay();
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
// scl=0;
// delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte()
{
uchar i,k;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
return k;
}
void delay1(uchar x)
{
uchar a,b;
for(a=x;a>0;a--)
for(b=100;b>0;b--);
}
void write_add(uchar address,uchar date)//指定地址写一数据
{
start();
write_byte(0xa0);
respons();
write_byte(address);// AT24C02:256字节(256×8位);
respons();
write_byte(date);
respons();
stop();
}
uchar read_add(uchar address)//指定地址读一数据
{
uchar date;
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
start();
write_byte(0xa1);
respons();
date=read_byte();
stop();
return date;
}
void main()
{
init();
write_add(23,0xA5);//在EEPROM内部地址23处写数据0XA5。内部地址范围为0-0xff(0-255)。
delay1(100);
P2=read_add(23);//把在23地址处写的数据读回来并在发光二极管上显示出来。
while(1);
}