头文件:
#ifndef _AT24C_EEPROM_Driver_H
#define _AT24C_EEPROM_Driver_H
#include <reg52.h>
#include <intrins.h>
#include "IIC_BUS_Driver.h"//Depended I2C Bus Driver
#define uchar unsigned char
#define ushort unsigned short
#define uint unsigned int
//#define AT24C_EEPROM_ADDRESS 0x00 /*定义IC地址*/
typedef struct _tag_iic_at24c_info_hdr
{
uchar ic_address;//器件地址
ushort size;//空间大小
ushort data_address;//当前指针位置
}IIC_AT24C_HEADER, *LPIIC_AT24C_HEADER;
enum AT24C_SEEK_TYPE
{
SEEK_BEGIN= 0,
SEEK_CUR= 1,
SEEK_END= 2,
};
bit AT24C_Init(LPIIC_AT24C_HEADER lpHdr, uchar ic_address, ushort size);
bit AT24C_Write(LPIIC_AT24C_HEADER lpHdr, uchar *buffer, uchar count);
bit AT24C_WriteByte(LPIIC_AT24C_HEADER lpHdr, uchar buf);
bit AT24C_Read(LPIIC_AT24C_HEADER lpHdr, uchar *buffer, uchar count);
uchar AT24C_ReadByte(LPIIC_AT24C_HEADER lpHdr);
bit AT24C_Seek(LPIIC_AT24C_HEADER lpHdr, short address, uchar seek_type);
#endif
源文件:
#include "AT24C_EEPROM_Driver.h"
#include "IIC_BUS_Driver.h"
bit AT24C_Init(LPIIC_AT24C_HEADER lpHdr, uchar ic_address, ushort size)
{
if (lpHdr == 0)
return 0;
lpHdr->ic_address = ic_address;
lpHdr->size = size;
lpHdr->data_address = 1;
return 1;
}
bit AT24C_Write(LPIIC_AT24C_HEADER lpHdr, uchar *buffer, uchar count)
{
if (lpHdr == 0)
return 0;
if (lpHdr->data_address + count >= lpHdr->size)
return 0;
if (ISendStr(lpHdr->ic_address, lpHdr->data_address, buffer, count) == 0)
return 0;
lpHdr->data_address += count;
return 1;
}
bit AT24C_WriteByte(LPIIC_AT24C_HEADER lpHdr, uchar buf)
{
if (lpHdr == 0)
return 0;
if (lpHdr->data_address + 1 >= lpHdr->size)
return 0;
if (ISendByte(lpHdr->ic_address, lpHdr->data_address, buf) == 0)
return 0;
lpHdr->data_address++;
return 1;
}
bit AT24C_Read(LPIIC_AT24C_HEADER lpHdr, uchar *buffer, uchar count)
{
if (lpHdr == 0)
return 0;
if (lpHdr->data_address + count >= lpHdr->size)
return 0;
if (IRecvStr(lpHdr->ic_address, lpHdr->data_address, buffer, count) == 0)
return 0;
lpHdr->data_address += count;
return 1;
}
uchar AT24C_ReadByte(LPIIC_AT24C_HEADER lpHdr)
{
uchar bk;
if (lpHdr == 0)
return 0;
if (lpHdr->data_address + 1 >= lpHdr->size)
return 0;
if (IRecvByte(lpHdr->ic_address, lpHdr->data_address, &bk) == 0)
return 0;
lpHdr->data_address++;
return bk;
}
bit AT24C_Seek(LPIIC_AT24C_HEADER lpHdr, short address, uchar seek_type)
{
if (lpHdr == 0)
return 0;
if (seek_type == SEEK_BEGIN)
{
if (address < 0)
lpHdr->data_address = 0x01;
else
{
lpHdr->data_address = address;
}
}
else if (seek_type == SEEK_END)
{
if (address < 0)
return 0;
else if (address >= lpHdr->size)
return 0;
lpHdr->data_address = lpHdr->size - address;
}
else if (seek_type == SEEK_CUR)
{
if (address < 0)
{
if ((-address) > lpHdr->data_address)
return 0;
}
else
{
if (lpHdr->data_address + address >= lpHdr->size)
return 0;
}
lpHdr->data_address += address;
}
return 1;
}