给你段程序看看吧,直接可以使用的
#include <reg52.H>
#include <INTRINS.H>
// ------下面的2个函数是本模块的对外开放的服务----------------------------------------------------------
// bit AT24CXX_ReadBuffer( unsigned int wAddress , unsigned char xdata* pBuffer , unsigned int uintSize )
// bit AT24CXX_WriteBuffer( unsigned int wAddress , unsigned char xdata* pBuffer , unsigned int uintSize )
// -----------------------------------------------------------------------------------------------------
#define AT24C64
#if defined( AT24C01 )
#define AT24CXX_PAGESIZE 8
#define AT24CXX_SIZE 128
#elif defined( AT24C02 )
#define AT24CXX_PAGESIZE 8
#define AT24CXX_SIZE 256
#elif defined( AT24C04 )
#define AT24CXX_PAGESIZE 16
#define AT24CXX_SIZE 512
#elif defined( AT24C08 )
#define AT24CXX_PAGESIZE 16
#define AT24CXX_SIZE 1024
#elif defined( AT24C16 )
#define AT24CXX_PAGESIZE 16
#define AT24CXX_SIZE 2048
#elif defined( AT24C32 )
#define AT24CXX_PAGESIZE 32
#define AT24CXX_SIZE 4096
#elif defined( AT24C64 )
#define AT24CXX_PAGESIZE 32
#define AT24CXX_SIZE 8192
#else
#error Must defined macro AT24C?? , please see < At24CXX.H > .
#endif
#define AT24CXX_Start() AT24CXX_SetState( 1 )
#define AT24CXX_Stop() AT24CXX_SetState( 0 )
#define nops() {_nop_();_nop_();_nop_();_nop_();_nop_();}
sbit AT24CXX_SCLK = P1^0;
sbit AT24CXX_IO = P1^1;
//启动I2C总线,SCL=1时,SDA上有下降沿
//停止I2C总线,SCL=1时,SDA上有上升沿
void AT24CXX_SetState( bit bBitLevel )
{
AT24CXX_SCLK = 0; _nop_();
AT24CXX_IO = bBitLevel; _nop_();
AT24CXX_SCLK = 1; nops();
AT24CXX_IO = ~bBitLevel; nops();
}
bit AT24CXX_Write( unsigned char ucData )
{
unsigned char data ucVar;
bit bitAcknowledge;
for ( ucVar = 0 ; ucVar < 8 ; ucVar++ ) {
AT24CXX_SCLK = 0; nops();
AT24CXX_IO = ( ( ucData & 0x80) == 0x80 ); nops();
AT24CXX_SCLK = 1; nops();
ucData < <= 1;
}
AT24CXX_SCLK = 0; nops();
AT24CXX_IO = 1; nops();
AT24CXX_SCLK = 1; nops();
bitAcknowledge = AT24CXX_IO;
AT24CXX_IO = 1; nops();
return(bitAcknowledge);
}
bit AT24CXX_Enabled(void)
{
unsigned char data tmpCnt;
tmpCnt=0xff;
while (tmpCnt != 0) { // 防止没有安装24CXX时,进入死循环
AT24CXX_Start();
if ( AT24CXX_Write(0xA0) == 0 ) return(1);
tmpCnt--;
}
return(0);
}
void AT24CXX_SetWriteAddress(unsigned int wAddress)
{
unsigned char data ucCtrl;
ucCtrl = 0xA0;
#if AT24CXX_PAGESIZE < 32
ucCtrl |= (unsigned char)( ( wAddress> > 7 ) & 0x0E );
AT24CXX_Write( ucCtrl );
AT24CXX_Write( (unsigned char)( wAddress & 0xff ) );
#else
AT24CXX_Write( ucCtrl );
AT24CXX_Write( (unsigned char)( wAddress > > 8 ) );
AT24CXX_Write( (unsigned char)( wAddress & 0xff ) );
#endif
}
void AT24CXX_SetReadAddress(unsigned int wAddress )
{
unsigned char data ucCtrl;
ucCtrl = 0xA1;
#if AT24CXX_PAGESIZE < 32
ucCtrl |= (unsigned char)( ( ( wAddress ) > > 7 ) & 0x0E );
#endif
AT24CXX_SetWriteAddress( wAddress );
AT24CXX_Start();
AT24CXX_Write( ucCtrl );
}
bit AT24CXX_WriteFullPage( unsigned int wAddress , unsigned char xdata* pBuffer )
{
unsigned char data ucVar;
if ( AT24CXX_Enabled() == 0 ) return 0;
AT24CXX_Start();
AT24CXX_SetWriteAddress( wAddress );
for ( ucVar = 0 ; ucVar < AT24CXX_PAGESIZE ; ucVar++ )
AT24CXX_Write( pBuffer[ucVar] );
AT24CXX_Stop();
return(1);
}
bit AT24CXX_WritePartPage( unsigned int wAddress , unsigned char xdata* pBuffer , unsigned char ucSize )
{
unsigned char data ucVar;
if ( AT24CXX_Enabled() == 0 ) return 0;
AT24CXX_Start();
AT24CXX_SetWriteAddress( wAddress );
for ( ucVar = 0 ; ucVar < ucSize ; ucVar++ )
AT24CXX_Write( pBuffer[ ucVar ] );
AT24CXX_Stop();
return(1);
}
bit AT24CXX_WriteBuffer( unsigned int wAddress , unsigned char xdata* pBuffer , unsigned int uintSize )
{
unsigned char data ucVar;
// AT24CXX_WP = 0;
ucVar = 0;
if ( ( wAddress % AT24CXX_PAGESIZE ) != 0 ) {
ucVar = AT24CXX_PAGESIZE - ( wAddress % AT24CXX_PAGESIZE );
if ( ucVar > uintSize ) ucVar = uintSize;
if (AT24CXX_WritePartPage( wAddress , pBuffer , ucVar )==0) return(0);
wAddress += ucVar;
pBuffer += ucVar;
uintSize -= ucVar;
}
ucVar = uintSize / AT24CXX_PAGESIZE;
while ( ucVar > 0 ) {
if (AT24CXX_WriteFullPage( wAddress , pBuffer )==0) return(0);
wAddress += AT24CXX_PAGESIZE;
pBuffer += AT24CXX_PAGESIZE;
uintSize -= AT24CXX_PAGESIZE;
ucVar -= 1;
}
if ( uintSize > 0 )
if (AT24CXX_WritePartPage( wAddress , pBuffer , uintSize )==0) return(0);
// AT24CXX_WP = 1;
return(1);
}
bit AT24CXX_ReadBuffer( unsigned int wAddress , unsigned char xdata* pBuffer , unsigned int uintSize )
{
unsigned int data pos;
unsigned char data k;
unsigned char data ucData;
if ( AT24CXX_Enabled() == 0 ) return(0);
AT24CXX_Start();
AT24CXX_SetReadAddress( wAddress );
for ( pos=0; pos <uintSize; ) {
ucData = 0;
for ( k = 0 ; k < 8 ; k++ ) {
AT24CXX_SCLK = 0; nops();
AT24CXX_IO = 1; nops();
ucData < <= 1;
AT24CXX_SCLK = 1; nops();
ucData |= AT24CXX_IO;
}
pBuffer[pos] = ucData;
pos++;
AT24CXX_SCLK = 0; nops();
AT24CXX_IO = ( pos == uintSize );
AT24CXX_SCLK = 1; nops();
}
AT24CXX_SCLK = 0; nops();
AT24CXX_SCLK = 1; nops();
AT24CXX_IO = 1; nops();
AT24CXX_Stop();
return(1);
}
发表时间:2008年1月4日20:59:24