#ifndef __AT24C02_H
#define __AT24C02_H
#include "sys.h"
//AT24C02初始化
void AT24C02_Init(void);
//AT24C02的字节写入 写一个字节
void AT24C02_WordWrite(u8 Address,u8 Data);
u8 AT24C02_WordRead(u8 Address );
void AT24C02_pageWrite(u8 page_Address,u8 *buf,u8 Datalen);
void AT24C02_RandomRead(u8 Address , u8 *RecvBuf ,u8 Dataleng);
#endif
上面是at24c02.h文件 下面是at24c02.c文件
#include "at24c02.h"
#include "at24c02iic.h"
#include "usart.h"
//AT24C02初始化
void AT24C02_Init(void)
{
AT24C02_IIC_Init();
}
//AT24C02的字节写入 写一个字节
//id AT24C02_WordWrite(u ddress,uint8_t Data)
void AT24C02_WordWrite(u8 Address,u8 Data)
{
//1。主机发送开始信号
AT24C02_IIC_Start();
//2.主机发送器件地址 写操作
AT24C02_IIC_Send_Byte(0xA0);
//3.主机等侍从机应答
if(AT24C02_IIC_Wait_Ack()==1) //如果没有应答
{
printf("AT24C02 Ack Device Address Error\n");
AT24C02_IIC_Stop();
}
printf("AT24C02 Ack Device Address OK\n");
//4.主机发送存储地址 写操作
AT24C02_IIC_Send_Byte(Address);
if(AT24C02_IIC_Wait_Ack()==1) //如果没有应答
{
printf("AT24C02 Ack Data Address Error\n");
AT24C02_IIC_Stop();
}
printf(" AT24C02 Ack Data Address OK\n");
//5主机发送存储数据 写操作
AT24C02_IIC_Send_Byte(Data);
if(AT24C02_IIC_Wait_Ack()==1) //如果没有应答
{
printf("AT24C02 Ack Write Data Error\n");
AT24C02_IIC_Stop();
}
printf(" AT24C02 Ack Write Date OK\n");
//6主机发送停止信号
AT24C02_IIC_Stop();
}
//AT24C02读取一个字节 随机读
u8 AT24C02_WordRead(u8 Address )
{
u8 data=0;
//1。主机发送开始信号
AT24C02_IIC_Start();
//2.主机发送器件地址 写操作
AT24C02_IIC_Send_Byte(0xA0);
//3.主机等侍从机应答
if(AT24C02_IIC_Wait_Ack()==1) //如果没有应答
{
printf("AT24C02 Ack Device Write Address Error\n");
AT24C02_IIC_Stop();
}
printf(" AT24C02 Ack Device Write Address OK\n");
//4.主机发送存储地址 打算读取数据的地址
AT24C02_IIC_Send_Byte(Address);
//5主机等待应答
if(AT24C02_IIC_Wait_Ack()==1) //如果没有应答
{
printf("AT24C02 Ack Data Device Address Error\n");
AT24C02_IIC_Stop();
}
printf(" AT24C02 Ack Data Device Address OK\n");
//6.主机再次发送开始信号
AT24C02_IIC_Start();
//7.主机发送器件地址 读操作
AT24C02_IIC_Send_Byte(0xA1);
//8主机等待应答
if(AT24C02_IIC_Wait_Ack()==1) //如果没有应答
{
printf("AT24C02 Ack Device Read Address Error\n");
AT24C02_IIC_Stop();
}
printf(" AT24C02 Ack Device Read Address OK\n");
//9主机读取1个字节数据
data=AT24C02_IIC_Read_Byte(1); //当前读
//10主机发送应答信号
// IIC_MasterAck(1);
//11主机发送停止信号
AT24C02_IIC_Stop();
return data;
}
//at24c02页写入 一次最多写入8字节
void AT24C02_pageWrite(u8 page_Address,u8 *buf,u8 Datalen)
{
//1。主机发送开始信号
AT24C02_IIC_Start();
//2.主机发送器件地址 写操作
AT24C02_IIC_Send_Byte(0xA0);
//3.主机等侍从机应答
if(AT24C02_IIC_Wait_Ack()==1) //如果没有应答
{
printf("AT24C02 Ack Device Address Error\n");
AT24C02_IIC_Stop();
}
printf("AT24C02 Ack Device Address OK\n");
//4.主机发送页地址 写操作
AT24C02_IIC_Send_Byte(page_Address);
if(AT24C02_IIC_Wait_Ack()==1) //如果没有应答
{
printf("AT24C02 Ack page_Address Error\n");
AT24C02_IIC_Stop();
}
printf("AT24C02 Ack page_Address OK\n");
//5主机循环发送字节
while(Datalen--)
{
AT24C02_IIC_Send_Byte(*buf++);
if(AT24C02_IIC_Wait_Ack()==1) //如果没有应答
{
printf("AT24C02 write data Error\n");
AT24C02_IIC_Stop();
}
}
//6.主机再次发送停止信号
AT24C02_IIC_Stop();
}
//读取n字节数据
void AT24C02_RandomRead(u8 Address , u8 *RecvBuf ,u8 Dataleng)
{
//1。主机发送开始信号
AT24C02_IIC_Start();
//2.主机发送器件地址 写操作
AT24C02_IIC_Send_Byte(0xA0);
//3.主机等侍从机应答
if(AT24C02_IIC_Wait_Ack()==1) //如果没有应答
{
printf("AT24C02 Ack Device Write Address Error\n");
AT24C02_IIC_Stop();
}
printf(" AT24C02 Ack Device Write Address OK\n");
//4.主机发送存储地址 打算读取数据的地址
AT24C02_IIC_Send_Byte(Address);
//5主机等待应答
if(AT24C02_IIC_Wait_Ack()==1) //如果没有应答
{
printf("AT24C02 Ack Data Device Address Error\n");
AT24C02_IIC_Stop();
}
printf(" AT24C02 Ack Data Device Address OK\n");
//6.主机再次发送开始信号
AT24C02_IIC_Start();
//7.主机发送器件地址 读操作
AT24C02_IIC_Send_Byte(0xA1);
//8主机等待应答
if(AT24C02_IIC_Wait_Ack()==1) //如果没有应答
{
printf("AT24C02 Ack Device Read Address Error\n");
AT24C02_IIC_Stop();
}
printf(" AT24C02 Ack Device Read Address OK\n");
//9主机读取1个字节数据
Dataleng=Dataleng-1;
while(Dataleng--)
{
*RecvBuf++=AT24C02_IIC_Read_Byte(1);
// IIC_MasterAck(0); //表示主机收到
}
*RecvBuf=AT24C02_IIC_Read_Byte(1); //读最后一个字节
//10主机发送应答信号
// IIC_MasterAck(1);
//11主机发送停止信号
AT24C02_IIC_Stop();
//return *RecvBuf;
}