📄 at24cxx.c
字号:
/*=======================================================================*/
/*-------------------------------------------------------------------------*/
/*=========AT24Cxx==========*/
/* at24cxx卡开始函数 */
void at24cxx_start(void)
{
ATSDA=1;
ATSCL=1;
_nop_();
_nop_();
ATSDA=0;
delay_12us();
ATSCL=0;
_nop_();
_nop_();
}
/*-------------------------------------------------------------------------*/
/* at24cxx卡停止函数 */
void at24cxx_stop(void)
{
ATSCL=0;
ATSDA=0;
_nop_();
_nop_();
ATSCL=1;
delay_12us();
ATSDA=1;
_nop_();
_nop_();
}
/*-------------------------------------------------------------------------*/
/* at24cxx卡串行输入函数 */
void at24cxx_si(char si)
{
uchar i=8;
while(i--)
{
ATSDA=(bit)(si&0x80);
ATSCL=1;
delay12us();
ATSCL=0;
delay12us();
si<<=1;
}
ATSDA=0; /* acknowledge */
delay_12us();
ATSCL=1;
delay_12us();
ATSCL=0;
delay_12us();
ATSDA=1;
}
/*--------------------------------------------------------------------------*/
/* at24cxx卡串行输出函数 */
char at24cxx_so(void)
{
uchar so,i=8;
ATSDA=1;
do {
so <<= 1;
if (ATSDA) so |=0x01;
ATSCL=1;
delay_12us();
ATSCL=0;
delay_12us();
} while(--i);
return(so);
}
/*-------------------------------------------------------------------------*/
/* 读at24c32(/64)卡一个字节函数 */
char at24c32_rb(uint addr) /* random read */
{
uchar f_addr,s_addr;
f_addr=addr/256;
s_addr=addr%256;
at24cxx_start();
at24cxx_si(0xa0);
at24cxx_si(f_addr);
at24cxx_si(s_addr);
at24cxx_start();
at24cxx_si(0xa1);
f_addr=at24cxx_so();
at24cxx_stop();
return(f_addr);
}
/*-------------------------------------------------------------------------*/
/* 向at24c32(/64)卡写一个字节函数 */
char at24c32_wb(uint addr, uchar dat) /* write a byte */
{
uchar f_addr,s_addr;
f_addr=addr/256;
s_addr=addr%256;
at24cxx_start();
at24cxx_si(0xa0);
at24cxx_si(f_addr);
at24cxx_si(s_addr);
at24cxx_si(dat);
at24cxx_stop();
delay_5ms();
return(at24c32_rb(addr));
}
/*-------------------------------------------------------------------------*/
/* 从at24c32(/64)卡存贮器读一个数据块函数 */
/* 输入形参:存数据块指针,块大小,块个数和卡的起始地址 */
uint at24c32_rd(void *buff,uint size,uint n,uint addr)
{
uint i,j=0;
uchar *buf;
if (!check_card(60)) return(0); // 无卡返回0
buf=buff;
while(n--)
{
for(i=0;i<size;i++)
{
if (ATIN) return(j); // 若抽卡,返回
*buf++=at24c32_rb(addr++);
}
j++;
}
return(j);
}
/*-------------------------------------------------------------------------*/
/* 向at24c32(/64)卡存贮器写一个数据块函数 */
/* 输入形参:存数据块指针,块大小,块个数和卡的起始地址 */
uint at24c32_wr(void *buff,uint size,uint n,uint addr)
{
uint i,j=0;
uchar *buf;
if (!check_card(60)) return(0); // 无卡返回0
buf=buff;
while(n--) // 循环读块
{
for(i=0;i<size;i++)
{
if (at24c32_wb(addr,*buf)!=*buf) // 写错误返回
return(j);
buf++; // 数据指针加1
addr++; // 卡地址加1
}
j++;
}
return(j); // 返回已写块数(j=n,正确,否则错)
}
/*-------------------------------------------------------------------------*/
/* 读at24c16(/01A/02/04/08)卡一个字节函数 */
char at24c16_rb(uint addr) /* random read a byte */
{
uchar d_addr,w_addr;
w_addr=addr%256;
d_addr=addr/256;
d_addr<<= 1;
d_addr&=0x0e;
d_addr|=0xa1;
at24cxx_start();
at24cxx_si(d_addr&0xfe);
at24cxx_si(w_addr);
at24cxx_start();
at24cxx_si(d_addr);
d_addr=at24cxx_so();
at24cxx_stop();
return(d_addr);
}
/*-------------------------------------------------------------------------*/
/* 向at24c16(/01A/02/04/08)卡写一个字节函数 */
char at24c16_wb(uint addr,uchar dat) /* write a byte */
{
uchar d_addr,w_addr;
w_addr=addr%256;
d_addr=addr/256;
d_addr<<=1;
d_addr &=0x0e;
d_addr |=0xa0;
at24cxx_start();
at24cxx_si(d_addr);
at24cxx_si(w_addr);
at24cxx_si(dat);
at24cxx_stop();
delay_5ms();
return(at24c16_rb(addr));
}
/*-------------------------------------------------------------------------*/
/* 从at24c16(/01A/02/04/08)卡存贮器读一个数据块函数 */
/* 输入形参:存数据块指针,块大小,块个数和卡的起始地址 */
uint at24c16_rd(void *buff,uint size,uint n,uint addr)
{
uint i,j=0;
uchar *buf;
if (!check_card(60)) return(0); // 无卡返回0
buf=buff;
while(n--)
{
for(i=0;i<size;i++)
{
if (ATIN) return(j); // 若抽卡,返回
*buf++=at24c16_rb(addr++);
}
j++;
}
return(j);
}
/*-------------------------------------------------------------------------*/
/* 向at24c16(01A/02/04/08)卡存贮器写一个数据块函数 */
/* 输入形参:存数据块指针,块大小,块个数和卡的起始地址 */
uint at24c16_wr(void *buff,uint size,uint n,uint addr)
{
uint i,j=0;
uchar *buf;
if (!check_card(60)) return 0; // 无卡返回0
buf=buff;
while(n--) // 循环读块
{
for(i=0;i<size;i++)
{
if (at24c16_wb(addr,*buf)!=*buf) // 写错误返回
return(j);
buf++; // 想据借针加1
addr++; // 卡地址加1
}
j++;
}
return(j); // 返回已写块数(j=n,正确,否则错)
}
/*=======================================================================*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -