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

📄 at24cxx.c

📁 IIC总线协议源码(AT24CXX芯片)
💻 C
字号:

#include <REGLE52AD.H>
#include <At24Cxx.H>
//#include <HanJi.H>


//读指定长度(不能超过16字节且均在一页范围内!),通过Buffer传递,address可在一页中0--15变化
//void ReadEEpromPage(Uint address,Uchar Num)
////void ReadEEpromPage(void)
//{  
//    I2C_Start();
//    I2C_Send_Byte(0xa0|((address>>7)&0x0e));
//    I2C_Send_Byte(address);
//    I2C_Start();
//    I2C_Send_Byte(0xa1|((address>>7)&0x0e));
//    for(address=0;address<Num;address++)
//    {    
////        ClearDog();
//    	Buffer[address]=I2C_Receive_Byte();
//    	if(address==(Num-1))
//        I2C_Nack();
//        else
//        I2C_ack();
//    }
//    I2C_Stop();
//    Protect24C16();
//}
//---------------------------------------------------
//void ReadEEprom264(Uint Addr)//读264byte,通过Buffer传递,ByteAddr起始地址
//{  //用时860MS
//    Uint  i16;
//    Uchar  StartPage;//起始页
//    Uchar  AllPage;//总页数
//    Uchar  i;
//    Uchar  j;
//    Uchar  Data;
//    Uchar  DataBuffer[16];
//    
//    StartPage=(Addr/16)+1;
//    Data=StartPage*16-Addr;//起始页的几个字节不够成一页
//    AllPage=(264-Data)/16;
//    ReadEEpromPage(Addr,Data);//起始页的几个字节不够成一页
////    strGlobal.ReadEEPageAddr=Addr;
////    strGlobal.ReadEEPageNum=Data;
////    ReadEEpromPage();//起始页的几个字节不够成一页
//    i16=Data;
//    
//    for(i=0;i<AllPage;i++)
//    {
////        ClearDog();
//        ReadEEpromPage2(DataBuffer,StartPage+i);
//        for(j=0;j<16;j++,i16++)
//        Buffer[i16]=DataBuffer[j];
//    }
//    i=264-AllPage*16-Data;
//    if(i)
//    {
//        for(j=0;j<i;j++,i16++)
//        Buffer[i16]=ReadEEpromByte(Addr+Data+AllPage*16+j);
//    }    
//}
//---------------------------------------------------
void I2C_Nack(void)
{
    ShortDelay();
    btData24C16=1;
    ShortDelay();
    btClock24C16=1;
    ShortDelay();
    btClock24C16=0;
    ShortDelay();
}
//---------------------------------------------------
void I2C_ack(void)
{
    ShortDelay();
    btData24C16=0;
    ShortDelay();
    btClock24C16=1;
    ShortDelay();
    btClock24C16=0;
    ShortDelay();
}
//---------------------------------------------------
unsigned char I2C_Receive_Byte(void)
{
 unsigned char  i, d;
 
    i=8;
    ShortDelay();
    btData24C16=1;
    btData24C16=1;
    while ( i--)
    {
      d = d << 1;
       ShortDelay();
        btClock24C16 =1;
      if (btData24C16)d++;
       ShortDelay();
        btClock24C16 =0;
    }
    return d;
}
//---------------------------------------------------
Uchar ReadEEpromByte(Uint address)
{  
     Uchar  Temp1;
     
     I2C_Start();
     I2C_Send_Byte(0xa0|((address>>7)&0x0e));
     I2C_Send_Byte(address);
     I2C_Start();
     I2C_Send_Byte(0xa1|((address>>7)&0x0e));
	 Temp1=I2C_Receive_Byte();
     I2C_Nack();
     I2C_Stop();
    return(Temp1);
}
//----------------------------
void I2C_Send_Byte( unsigned char d)
{
    Uchar  Temp0;
    
	Temp0 = 8;
	while( Temp0-- )
	{
		 if ( d &0x80 )   
		 btData24C16 =1;
		 else             
		 btData24C16 =0;
		 btClock24C16 = 1;
		 ShortDelay();
		 d = d << 1;
		 btClock24C16 = 0;
		 ShortDelay();
	}
    btData24C16=1;
	btClock24C16=1;
	ShortDelay();
	btClock24C16=0;
	ShortDelay();
}
//----------------------------
void I2C_Start(void)
{
    Uchar  Temp1;
    
    Temp1=1;
    btData24C16=1;
	btClock24C16=1;
    ShortDelay();
   /*_pbc=0x07;	//in
   if( btData24C16 == 0)//读写不正确!退出
   {
   		return;
	}
   if( btClock24C16 == 0) //读写不正确!退出
   {
   		return;
	}
   _pbc=0x04;	//out*/
   btData24C16 = 0;//关键!,要延时
   do
   {++Temp1;}
   while(Temp1);
   btClock24C16 = 0;
   ShortDelay();
   ShortDelay();
   ShortDelay();
}
//--------epaddres在一页中的起始地址可以0--15不定,但不能超过一页的范围,number可以1--16不定
//void WriteEEpromPage(Uint epaddres,Uchar number)//写指定的长度(不能超过16字节!),通过Buffer传递,每页16字节
////void WriteEEpromPage(void)//写指定的长度(不能超过16字节!),通过Buffer传递,每页16字节
//{
//  NotProtect24C16();
////    ClearDog();
//  I2C_Start();
//  I2C_Send_Byte(0xa0|((epaddres>>7)&0x0e));
//  I2C_Send_Byte(epaddres);
//  for(epaddres=0;epaddres<number;epaddres++)
//  {
//    I2C_Send_Byte(Buffer[epaddres]);
////    ClearDog();
//  }
//  I2C_Stop();
//  Protect24C16();
//  Wait1Ms(6);
////  for(number=0;number<50;number++)//加延时,很重要!!
////  {
////    ShortDelay();
////  }
//}
//---------------------------------------------------
void WriteEEpromByte(Uint epaddres,unsigned char number)
{
  I2C_Start();
  I2C_Send_Byte(0xa0|((epaddres>>7)&0x0e));
  I2C_Send_Byte(epaddres);
  I2C_Send_Byte(number);
  I2C_Stop();
  for(number=0;number<50;number++)//加延时,很重要!!
  {
    ShortDelay();
  }
}
//---------------------------------------------------
void  I2C_Stop(void)
{
  	btData24C16 = 0;
  	ShortDelay();
  	btClock24C16 = 1;
    ShortDelay();
  	btData24C16 = 1;
  	ShortDelay();
}
//-------------------------------------
void ShortDelay(void)
{
//    Uchar data i=10;
    Uchar idata i=100;
    do
    {
        i--;
    }while(i);
//    ClearDog();
}
//-------------------------------------
//void ReadEEpromPage2(Uchar *Buf,Uchar Page)//通过Buf传递,读16字节,Page为指定的页(0--128)
//{  
//    Uchar  i;
//    Uint  address;
//    
//    NotProtect24C16();
//    address=Page<<4;
//    I2C_Start();
//    I2C_Send_Byte(0xa0|((address>>7)&0x0e));
//    I2C_Send_Byte(address);
//    I2C_Start();
//    I2C_Send_Byte(0xa1|((address>>7)&0x0e));
//    for(i=0;i<16;i++)
//    {    
////        ClearDog();
//    	Buf[i]=I2C_Receive_Byte();
//    	if(i==15)
//        I2C_Nack();
//        else
//        I2C_ack();
//    }
//    I2C_Stop();
//    Protect24C16();
//}
////---------------------------------------
//void ReadEEpromAll(Uchar *Buf)//把EEprom数据全部读出,Buf传递数组地址,
//{
//    Uchar  Page;
//    Uchar  i;
//    Uint  i16;
//    Uint  address;
//    
//    NotProtect24C16();
//    for(Page=0,i16=0;Page<128;Page++)
////    for(Page=0,i16=0;Page<64;Page++)
////    for(Page=0,i16=0;Page<32;Page++)
//    {
////        ClearDog();
//        address=Page<<4;
//        I2C_Start();
//        I2C_Send_Byte(0xa0|((address>>7)&0x0e));
//        I2C_Send_Byte(address);
//        I2C_Start();
//        I2C_Send_Byte(0xa1|((address>>7)&0x0e));
//        for(i=0;i<16;i++)
//        {    
////            ClearDog();
//        	Buf[i16]=I2C_Receive_Byte();
//        	if(i==15)
//            I2C_Nack();
//            else
//            I2C_ack();
//            i16++;
//        }
//        I2C_Stop();
//    }
//    Protect24C16();
////    for(i16=0;i16<2048;i16++)
////    for(i16=0;i16<512;i16++)
////    SendByte(Buf[i16]);
//}
//---------------------------------------

⌨️ 快捷键说明

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