AT24C256完整程序.docx
《AT24C256完整程序.docx》由会员分享,可在线阅读,更多相关《AT24C256完整程序.docx(9页珍藏版)》请在冰豆网上搜索。
AT24C256完整程序
AT24C256完整程序
/******************************************
AT24C64C语言程序
copyright@lance
******************************************/
#include"c8051F020.h"
#include"24C256.h"
#include"LCD_display.h"
sbitISCL=P1^4;
sbitISDA=P1^3;
/*****************************************
I2cWait
WaitforsometimetogetproperI2Ctiming
******************************************/
voidI2cWait(void)
{
/*****************************************
I2csotp
conditionSDA=0-1whileSCL=1
******************************************/
voidI2cStop(void)
{
ISDA=0;
I2cWait();
ISCL=1;
I2cWait();
ISDA=1;
I2cWait();
}
/*****************************************
I2cInit
InitializeI2Cinterface
ReleaseI2cBUS
****************************************
voidI2cInit(void)
{
ISDA=1;
ISCL=1;
I2cWait();
}
**/
/*****************************************
I2cSentByte
mastertransferdatatoslaveandreturnacknowledgebit
don'tinclude
******************************************/
bitI2cSentByte(unsignedcharbytedata)
{
unsignedchari;
bitack;
for(i=0;i<8;i++)
{
if(bytedata&0x80)
ISDA=1;
else
ISDA=0;
bytedata<<=1;
I2cWait();
ISCL=1;
I2cWait();
ISCL=0;
I2cWait();
}
ISDA=1;
I2cWait();
ISCL=1;
I2cWait();
ack=ISDA;
ISCL=0;
I2cWait();
returnack;
}
/*****************************************
I2cReceiveByte
slavetrransferdatatomaster
******************************************/
unsignedcharI2cReceiveByte(void)
{
unsignedchari;
unsignedcharbytedata=0;
ISCL=0;
I2cWait();
for(i=0;i<8;i++)
{
ISCL=1;
I2cWait();
bytedata=bytedata<<1;
if(ISDA==1)
{
bytedata=bytedata|0x01;
}
I2cWait();
ISCL=0;
I2cWait();
}
returnbytedata;
}
/*****************************************
I2cSendAcknowledge
Mastersendacknowledgebittoslave
acknowledge="0",non-acknowledge="1"
******************************************/
voidSendAcknowledge(bitack)
{
ISDA=ack;
ISCL=1;
I2cWait();
ISCL=0;
}
/*****************************************
24c64WriteByte
addr:
0-8192
value:
数据
******************************************/
void_24c64WriteByte(unsignedintaddr,unsignedcharvalue)
{
I2cStart();
I2cSentByte(0xA0);
I2cSentByte(addr>>8);//送高位地址
I2cSentByte(addr&0xff);//送低位地址
I2cSentByte(value);
I2cStop();
delay1ms(15);
}
/*****************************************
24c64WriteMulti
page:
0-255
count:
要写入的数个数
******************************************/
void_24c64WriteMulti(unsignedintpage,unsignedcharcount,unsignedchar*SenBuf)
{
unsignedchari;
unsignedintaddr=page*32;
I2cStart();
I2cSentByte(0xa0);
I2cSentByte(addr>>8);//送高位地址
I2cSentByte(addr&0x00ff);//送低位地址
for(i=0;i{
I2cSentByte(SenBuf[i]);
}
I2cStop();
delay1ms(15);
}
/*****************************************
24c64ReadByte
addr:
0-8192
******************************************/
unsignedchar_24c64ReadByte(unsignedintaddr)
{
unsignedchartemp;
I2cStart();
I2cSentByte(0xa0);
I2cSentByte(addr>>8);//送高位地址
I2cSentByte(addr&0x00ff);//送低位地址
I2cStart();
I2cSentByte(0xa1);
temp=I2cReceiveByte();
SendAcknowledge
(1);
I2cStop();
returntemp;
}
/*****************************************
24c64ReadMulti
page:
0-256,count0-32
count:
要读出的数的个数
******************************************/
void_24c64ReadMulti(unsignedintpage,unsignedcharcount,unsignedchar*RedBuf)
{unsignedintaddr=page*32;
unsignedchari;
for(i=0;i{
RedBuf[i]=_24c64ReadByte(addr);
addr++;
}
SendAcknowledge
(1);
I2cStop();
}