📄 at24c256.h
字号:
#ifndef AT24C256_H
#define AT24C256_H
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit SDA=P3^6;
sbit SCL=P3^5;
bit errorflag=1;
void delay(unsigned char count); //延时函数
void start_24c256(void); //启动
void stop_24c256(void); //结束
bit recack_24c256_24c256(void); //应答检查
void noack_24c256_24c256(void); //不应答
void ack_24c256(void); //应答
unsigned char receivebyte_24c(void); //读1字节
void sendbyte_24c(unsigned char sendbyte); //写1字节
void write_24c256(unsigned char *Data,unsigned char length,unsigned int add); //往地址add处写入长度为length的数据Data
void read_24c256(unsigned char *buffer,unsigned char length,unsigned int add); //往地址add处读取长度为length的数据放入buffer
void write_24c256(unsigned char *Data,unsigned char length,unsigned int add)
{
uchar i=10,j;
while(i--)
{
start_24c256();
sendbyte_24c(0xa0);
if(recack_24c256_24c256())continue;
sendbyte_24c((unsigned char)(add>>8));
if(recack_24c256_24c256())continue;
sendbyte_24c((unsigned char)add);
if(recack_24c256_24c256())continue;
j=length;
errorflag=0;
while(j--)
{
sendbyte_24c(*Data++);
if(!recack_24c256_24c256())continue;
errorflag=1;
break;
}
if(errorflag==1)
continue;
break;
}
stop_24c256();
delay(255);
delay(255);
delay(255);
delay(255);
}
void read_24c256(unsigned char *buffer,unsigned char length,unsigned int add)
{
uchar i=10,j,add_h,add_l; //分别存放地址add的高8位和低8位
add_h=(unsigned char)(add>>8);
add_l=(unsigned char)add;
while(i--)
{
j=length;
start_24c256();
sendbyte_24c(0xa0); //激活写功能
if(recack_24c256_24c256())continue;
sendbyte_24c(add_h); //写入高字节地址
if(recack_24c256_24c256())continue;
sendbyte_24c(add_l); //写入低字节地址
if(recack_24c256_24c256())continue;
start_24c256(); //初始化
sendbyte_24c(0xa1); //激活读功能
if(recack_24c256_24c256())continue;
while(--j)
{
*buffer++=receivebyte_24c();
ack_24c256();
}
*buffer=receivebyte_24c(); //读出一字节数据,放入Data
noack_24c256_24c256();
break;
}
stop_24c256(); //结束写操作
_nop_();_nop_();_nop_();_nop_();
}
/*==================================================*/
/* 启动函数 */
/*==================================================*/
void start_24c256()
{
SCL=0; //先拉低SCL以改变SDA
SDA=1; //SCL为高电平时,SDA的下降沿表示启动,故先拉高SDA
_nop_();//延时
_nop_();
_nop_();
SCL=1; // 当SCL为高电平时SDA的下降沿表示开始状态
_nop_();
_nop_();
SDA=0; //给SDA下降沿表示开始
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0; //恢复低电平以改变SDA的值
SDA=1;
}
/*==================================================*/
/* 停止函数 */
/*==================================================*/
void stop_24c256() //停止函数
{
SCL=0;
SDA=0; //SCL为高电平时,SDA的上升沿表示停止,故先拉低SDA
SCL=1; //拉高SCL
_nop_();//延时
_nop_();
_nop_();
_nop_();
SDA=1; //给SDA一个上升沿以停止
_nop_();
_nop_();
SCL=0; //返回0状态以等待
}
/*=======================================================================*/
/* 检查应答 */
/*=======================================================================*/
bit recack_24c256_24c256(void)
{
SCL=0; //在SCL为0的时候改变SDA的值
SDA=1; //将SDA置1以释放总线
SCL=1; //在SCL为1的时候等待SDA值的变化,在器件接受到数据后会把SDA拉低。
_nop_();
_nop_();
_nop_();
_nop_();
CY=SDA; // 因为返回值总是放在CY中的
SCL=0;
_nop_();
return(CY); //如果为CY为低则表示接受成功,如果为高,则表示接受失败。
}
/*===================================================*/
/* 产生应答 */
/*===================================================*/
void ack_24c256(void) //该函数用来读取应答信号
{
SDA=0;
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;
_nop_();
SDA=1;
}
/*===================================================*/
/* 不产生应答 */
/*===================================================*/
void noack_24c256_24c256(void)
{
SDA=1;
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;
}
/*===================================================*/
/* 写一字节 */
/*===================================================*/
void sendbyte_24c(unsigned char sendbyte)
{
uchar j=8;
for(;j>0;j--)
{
SCL=0; //拉低SCL准备给上升沿
_nop_(); _nop_(); _nop_(); _nop_();//延时
sendbyte<<= 1; //使CY=sendbyte^7;
SDA=CY; //移位sendbyte时,移出的部分会放进CY
SCL=1; //给上升沿,发出SDA的状态值
}
SCL=0;
}
/*===================================================*/
/* 读一字节 */
/*===================================================*/
unsigned char receivebyte_24c(void)
{
register receivebyte,i=8;
SCL=0;
while(i--)
{
SCL=1; //拉高SCL准备给下降沿
_nop_();
receivebyte=(receivebyte<<1)|SDA; //接受值左移一位把低位和SDA相或得到SDA的状态值
SCL=0; //给下降沿发出SDA的状态值
}
_nop_();
return(receivebyte);
}
/*==========================================*/
/* 延时 */
/*==========================================*/
void delay(unsigned char count)
{
while(count--);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -