📄 24c64.c
字号:
/*****************************************
AT24C64C语言程序
copyright@lance
******************************************/
#define AT24C64_GLOBALS
#include"AT24C64.h"
void Delay16(unsigned int i)
{
while(i--);
}
/*****************************************
i2c project
******************************************/
void I2cInit(void);
void I2cStart(void);
void I2cStop(void);
void I2cWait(void);
bit I2cSentByte(uchar bytedata);
void SendAcknowledge(bit ack);
/*****************************************
i2c start
condition SDA 1-0 while SCL=1
******************************************/
void I2cStart(void)
{
ISDA=1;
ISCL=1;
I2cWait();
ISDA=0;
I2cWait();
ISCL=0;
}
/*****************************************
I2c sotp
condition SDA=0-1 while SCL=1
******************************************/
void I2cStop(void)
{
ISDA=0;
I2cWait();
ISCL=1;
I2cWait();
ISDA=1;
}
/*****************************************
I2c Wait
Wait for some time to get proper I2C timing
******************************************/
void I2cWait(void)
{
_nop_();
_nop_();
}
/*****************************************
I2c Init
Initialize I2C interface
Release I2c BUS
******************************************/
void I2cInit(void)
{
ISDA=1;
ISCL=1;
}
/*****************************************
I2c SentByte
master transfer data to slave and return acknowledge bit
don't include<intrins.h>
******************************************/
bit I2cSentByte(uchar bytedata)
{
uchar i;
bit ack;
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();
return ack;
}
/*****************************************
I2c ReceiveByte
slave trransfer data to master
******************************************/
uchar I2cReceiveByte(void)
{
uchar i;
uchar bytedata=0;
for(i=0;i<8;i++)
{
ISCL=1;
I2cWait();
bytedata<<=1;
if(ISDA)
bytedata|=0x01;
I2cWait();
ISCL=0;
}
return bytedata;
}
/*****************************************
I2c SendAcknowledge
Master send acknowledge bit to slave
acknowledge="0",non-acknowledge="1"
******************************************/
void SendAcknowledge(bit ack)
{
ISDA=ack;
ISCL=1;
I2cWait();
ISCL=0;
}
/*****************************************
24c64 project
******************************************/
/*****************************************
24c64 WriteByte
addr:0-8192
value:数据
******************************************/
void _24c64WriteByte(uint addr,uchar value)
{
I2cStart();
I2cSentByte(0xA0);
I2cSentByte(addr>>8); //送高位地址
I2cSentByte(addr&0xff); //送地位地址
I2cSentByte(value);
I2cStop();
Delay16(2000);
}
/*****************************************
24c64 WriteMulti
page:0-255
count:要写入的数个数
******************************************/
void _24c64WriteMulti(uchar page,uchar count,uchar *SenBuf)
{
uchar i ;
uint addr=page*32;
I2cStart();
I2cSentByte(0xa0);
I2cSentByte(addr>>8); //送高位地址
I2cSentByte(addr&0xff); //送地位地址
for(i=0;i<count;i++)
{
I2cSentByte(SenBuf[i]);
}
I2cStop();
Delay16(2000);
}
/*****************************************
24c64 ReadByte
addr:0-8192
******************************************/
uchar _24c64ReadByte(uint addr)
{
uchar temp;
I2cStart();
I2cSentByte(0xa0);
I2cSentByte(addr>>8); //送高位地址
I2cSentByte(addr&0xff); //送地位地址
I2cStart();
I2cSentByte(0xa1);
temp=I2cReceiveByte();
SendAcknowledge(1);
I2cStop();
return temp;
}
/*****************************************
24c64 ReadMulti
page:0-256,count0-32
count:要读出的数的个数
******************************************/
void _24c64ReadMulti(uchar page,uchar count,uchar *RedBuf)
{ uint addr=page*32;
uchar i;
for(i=0;i<count;i++)
{
RedBuf[i]=_24c64ReadByte(addr);
addr++;
}
SendAcknowledge(1);
I2cStop();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -