📄 eeprom.h
字号:
sbit sda=P2^0; //IO口定义
sbit scl=P2^1;
/////////24C08读写驱动程序////////////////////
void delay1(unsigned int x)
{ unsigned int i;
for(i=0;i<x;i++);
;}
void flash()
{ ; ; }
void x24c08_init() //24c08初始化子程序
{scl=1; flash(); sda=1; flash();}
void start() //启动I2C总线
{sda=1; flash(); scl=1; flash(); sda=0; flash(); scl=0; flash();}
void stop() //停止I2C总线
{sda=0; flash(); scl=1; flash(); sda=1; flash();}
void writex(unsigned char j) //写一个字节
{ unsigned char i,wtemp;
wtemp=j;
for (i=0;i<8;i++)
{wtemp=wtemp<<1; scl=0; flash(); sda=CY; flash(); scl=1; flash();}
scl=0; flash(); sda=1; flash();
}
unsigned char readx() //读一个字节
{
unsigned char i,j,k=0;
scl=0; flash(); sda=1;
for (i=0;i<8;i++)
{ flash(); scl=1; flash();
if (sda==1) j=1;
else j=0;
k=(k<<1)|j;
scl=0;}
flash(); return(k);
}
void clock() //I2C总线时钟,获得24c64响应信号
{
unsigned char i=0;
scl=1; flash();
while ((sda==1)&&(i<255))i++;
scl=0; flash();
}
////////从24c02的地址address中读取一个字节数据/////
unsigned char x24c08_read(uint address)
{
unsigned char i,hig,low;
start(); writex(0xa0); //发送0xa0表示写数据
clock();
hig=(address&0xff00)>>8; //获得地址的高8位
low=address&0x00ff; //获得地址的低8位
writex(hig); //写地址高8位
clock();
writex(low); //写地址低8位
clock();
start();
writex(0xa1); clock(); //发送0xa1表示读数据
i=readx(); stop();
delay1(10);
return(i);
}
//////向24c02的address地址中写入一字节数据info/////
void x24c08_write(uint address,unsigned char info)
{ unsigned char hig,low;
start(); writex(0xa0);
clock();
hig=(address&0xff00)>>8;
low=address&0x00ff;
writex(hig);
clock();
writex(low);
clock(); writex(info);
clock(); stop();
delay1(50);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -