⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 at24cxx.c

📁 I2C协议机制
💻 C
字号:
/*******************************************************************************
AT24512 Introduction:
65536bytes *8Bit=512 pages *128bytes/page 
word address=16bit
*******************************************************************************/
#include "App\Global.h"
#include "I2C.h"
#include "AT24CXX.h"
#include "App\time.h"
BOOLEAN AT24CXXReadByte(INT16U WordAdd,INT8U *pReadData)
{	
	if(I2C_ReadBlock(AT24CXX_I2CSlaveWriteAddress,AT24CXX_I2CSlaveReadAddress,pReadData,WordAdd,1)==NG)
	return NG;

	return OK;
}
BOOLEAN AT24CXXWriteByte(INT16U WordAddr,INT8U value)
{	
	if(I2C_WriteBlock(AT24CXX_I2CSlaveWriteAddress,&value,WordAddr,1)==NG)
	return NG;

	return OK;
}
BOOLEAN AT24CXXWriteBlock(INT8U *pWriteData,INT16U StartWordAdd,INT16U size)
{
    INT8U FirstPageNo,IntPageNos,LastPageNo;
	INT8U FirstPageBytes,LastPageBytes;
	INT8U i;
	FirstPageNo=StartWordAdd/PageBytes;
	FirstPageBytes=PageBytes-StartWordAdd%PageBytes;
	IntPageNos=(size-FirstPageBytes)/PageBytes;
	LastPageNo=FirstPageNo+IntPageNos+1;
	LastPageBytes=size-FirstPageBytes-IntPageNos*PageBytes;

	//首页零散字节
	if(I2C_WriteBlock(AT24CXX_I2CSlaveWriteAddress,pWriteData,StartWordAdd,FirstPageBytes)==NG)
	return NG;
	DelayNms(10);//临时增加延时
	
	//中间整数字节
	if(IntPageNos!=0)
	{
		for(i=0;i<IntPageNos;i++)
		{
			if(I2C_WriteBlock(AT24CXX_I2CSlaveWriteAddress,pWriteData+FirstPageBytes+i*PageBytes,(FirstPageNo+i+1)*PageBytes,PageBytes)==NG)
			break;
			DelayNms(10);
		}
	}
	DelayNms(10);//临时增加延时
	
	//尾页零散字节
	if(LastPageBytes!=0)
	{
		if(I2C_WriteBlock(AT24CXX_I2CSlaveWriteAddress,pWriteData+FirstPageBytes+IntPageNos*PageBytes,LastPageNo*PageBytes,LastPageBytes)==NG)
		return NG;
	}
	DelayNms(10);//临时增加延时
}
BOOLEAN AT24CXXReadBlock(INT8U *pReadData,INT16U StartWordAdd,INT16U size)
{
    INT8U FirstPageNo,IntPageNos,LastPageNo;
	INT8U FirstPageBytes,LastPageBytes;
	INT8U i;
	FirstPageNo=StartWordAdd/PageBytes;
	FirstPageBytes=PageBytes-StartWordAdd%PageBytes;
	IntPageNos=(size-FirstPageBytes)/PageBytes;
	LastPageNo=FirstPageNo+IntPageNos+1;
	LastPageBytes=size-FirstPageBytes-IntPageNos*PageBytes;	
	
	//首页零散字节
	if(I2C_ReadBlock(AT24CXX_I2CSlaveWriteAddress,AT24CXX_I2CSlaveReadAddress,pReadData,StartWordAdd,FirstPageBytes)==NG)
	return NG;
	DelayNms(10);//临时增加延时

	//中间整数字节
	if(IntPageNos!=0)
	{
		for(i=0;i<IntPageNos;i++)
		{
			if(I2C_ReadBlock(AT24CXX_I2CSlaveWriteAddress,AT24CXX_I2CSlaveReadAddress,pReadData+FirstPageBytes+i*PageBytes,(FirstPageNo+i+1)*PageBytes,PageBytes)==NG)
			break;
			DelayNms(10);
		}
	}
	DelayNms(10);//临时增加延时

	//尾页零散字节
	if(LastPageBytes!=0)
	{
		if(I2C_ReadBlock(AT24CXX_I2CSlaveWriteAddress,AT24CXX_I2CSlaveReadAddress,pReadData+FirstPageBytes+IntPageNos*PageBytes,LastPageNo*PageBytes,LastPageBytes)==NG)
		return NG;
	}
	DelayNms(10);//临时增加延时
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -