#define uchar unsigned char
#define uint unsigned int
#include
#include
#include
sbit scl=P3^5; //24c02 SCL
sbit sda=P3^4; //24c02 SDA
sbit DOG=P1^7; //看门狗
uchar x24c02_read(uchar address); //从24c02的地址address中读取一个字节数据
void x24c02_write(uchar address,uchar info); //向24c02的address地址中写入一字节数据info
void x24c02_init(); //24c02初始化子程序
void delay1(uchar x);
void flash();
void x24c01_init();
void start();
void stop();
void writex(uchar j);
uchar readx();
void clock();
void delay1(uchar x)
{
uint i;
for(i=0;iDOG=~DOG;
}
void flash()
{
uchar u;
DOG=~DOG;
}
void x24c02_init()
{
scl=1; flash(); sda=1; flash();
}
void start()
{
sda=1; flash(); scl=1; flash(); sda=0; flash(); scl=0; flash();
}
void stop()
{
sda=0; flash(); scl=1; flash(); sda=1; flash();
}
void writex(uchar j)
{
uchar i,temp;
temp=j;
for (i=0;i<8;i++){
temp=temp<<1; scl=0; flash(); sda=CY; flash(); scl=1; flash();
}
scl=0; flash(); sda=1; flash();
}
uchar readx()
{
uchar 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()
{
uchar i=0;
scl=1; flash();
while ((sda==1)&&(i<255))i++;
scl=0; flash();
}
uchar x24c02_read(uchar address)
{
uchar i;
start(); writex(0xa0);
clock(); writex(address);
clock(); start();
writex(0xa1); clock();
i=readx(); stop();
delay1(10);
return(i);
}
void x24c02_write(uchar address,uchar info)
{
EA=0;
start(); writex(0xa0);
clock(); writex(address);
clock(); writex(info);
clock(); stop();
EA=1;
delay1(50);
}
引用地址:C语言编写的 24C02程序
#define uint unsigned int
#include
#include
#include
sbit scl=P3^5; //24c02 SCL
sbit sda=P3^4; //24c02 SDA
sbit DOG=P1^7; //看门狗
uchar x24c02_read(uchar address); //从24c02的地址address中读取一个字节数据
void x24c02_write(uchar address,uchar info); //向24c02的address地址中写入一字节数据info
void x24c02_init(); //24c02初始化子程序
void delay1(uchar x);
void flash();
void x24c01_init();
void start();
void stop();
void writex(uchar j);
uchar readx();
void clock();
void delay1(uchar x)
{
uint i;
for(i=0;i
}
void flash()
{
uchar u;
DOG=~DOG;
}
void x24c02_init()
{
scl=1; flash(); sda=1; flash();
}
void start()
{
sda=1; flash(); scl=1; flash(); sda=0; flash(); scl=0; flash();
}
void stop()
{
sda=0; flash(); scl=1; flash(); sda=1; flash();
}
void writex(uchar j)
{
uchar i,temp;
temp=j;
for (i=0;i<8;i++){
temp=temp<<1; scl=0; flash(); sda=CY; flash(); scl=1; flash();
}
scl=0; flash(); sda=1; flash();
}
uchar readx()
{
uchar 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()
{
uchar i=0;
scl=1; flash();
while ((sda==1)&&(i<255))i++;
scl=0; flash();
}
uchar x24c02_read(uchar address)
{
uchar i;
start(); writex(0xa0);
clock(); writex(address);
clock(); start();
writex(0xa1); clock();
i=readx(); stop();
delay1(10);
return(i);
}
void x24c02_write(uchar address,uchar info)
{
EA=0;
start(); writex(0xa0);
clock(); writex(address);
clock(); writex(info);
clock(); stop();
EA=1;
delay1(50);
}
小广播
热门活动
换一批
更多
设计资源 培训 开发板 精华推荐
最新单片机文章
更多精选电路图
更多热门文章
更多每日新闻
更多往期活动
11月23日历史上的今天
厂商技术中心