24c02的c51驱动
扫描二维码
随时随地手机看文章
#include
//#include
//#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit led=P1^0;
sbit led2=P1^1;
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 _nop_();
void x24c01_init();
void start();
void stop();
void writex(uchar j);
uchar readx();
void clock();
unsigned char code dispcode[]={0x3f,0x06, 0x5b, 0x4f, 0x66,
0x6d, 0x7d,0x07,0x7f,0x6f}; //显示段代码
void delay1(uchar x)
{
uint i;
for(i=0;i
}
/*void _nop_()
{
// uchar u;
DOG=~DOG;
}*/
void x24c02_init()
{
SCL=1;
_nop_();
SDA=1;
_nop_();
}
void start()
{
SDA=1;
_nop_();
SCL=1;
_nop_();
SDA=0;
_nop_();
SCL=0;
_nop_();
}
void stop()
{
SDA=0;
_nop_();
SCL=1;
_nop_();
SDA=1;
_nop_();
}
void writex(uchar j)
{
uchar i,temp;
temp=j;
for (i=0;i<8;i++)
{
temp=temp<<1;
SCL=0;
_nop_();
SDA=CY;
_nop_();
SCL=1;
_nop_();
}
SCL=0;
_nop_();
SDA=1;
_nop_();
}
uchar readx()
{
uchar i,j,k=0;
SCL=0; _nop_(); SDA=1;
for (i=0;i<8;i++)
{
_nop_();
SCL=1;
_nop_();
if (SDA==1)
j=1;
else j=0;
k=(k<<1)|j;
SCL=0;
}
_nop_();
return(k);
}
void clock()
{
uchar i=0;
SCL=1;
_nop_();
while ((SDA==1)&&(i<255))
i++;
SCL=0;
_nop_();
}
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);
}
void delay(int us)
{
int s;
for ( s=0; s
void disp(unsigned char data_X)
{
P3_1=0;
P2=~dispcode[data_X/10];
P3_0=1;
delay(200);
P3_0=0;
P2=~dispcode[data_X%10];
P3_1=1;
delay(200);
}
void main(void)
{
while(1)
{
// uchar data a=2,b=45,c=0;
uchar a=2,b=55,c=0;
x24c02_init();
x24c02_write(a,b);
delay(200);
c=x24c02_read(a);
if(c==b)led=0;
led2=0;
disp(c);
}
}
/*====================================================================//
main (){
uchar data a=2,b=10,c=0;
x24c02_init();
x24c02_write(a,b);
delay1(200);
c=x24c02_read(a);
if(c==b)led=0;
led2=0;
}
//==================================================================*/