EEPROM at24c256读写

2019-04-15 15:49发布

/******************************************
*
* Description: EEPROM Read/Write.
* Author:LY
* Date: 2013-07-24 
*
******************************************/
#include
#include "at24c128.h"
//Write:0xA0,Read:0xA1 读写间须延时
#define  EEPROM_ADDR    0xA0 
#define  DELAY_TIME       1  
extern void delay(uint uiCnt);


unsigned char i2c_eeprom_byte_write(unsigned int uiAddr, unsigned char ucData)
{
  unsigned char ucTmp;
  unsigned int    uiTmp;
  unsigned char i;
  
   SDA = 1;
   SCL = 1;
   delay(DELAY_TIME);


   //start bit
   SDA = 0;
   delay(DELAY_TIME);
   
  //device addr
  ucTmp = 0xA0;
  for(i = 0; i<8;i++)
  {
      SCL = 0;


 delay(DELAY_TIME);
      if(ucTmp & 0x80)
      {
      SDA = 1;
      }
 else
   {
      SDA = 0;
   }
 delay(DELAY_TIME);
 ucTmp <<=  1;
 SCL = 1;
      delay(DELAY_TIME);
  }


  //ack read
  SCL = 0;
  SDA = 1;
   delay(DELAY_TIME);
   
   if(SDA == 0)  //ack ok
   {
      SCL = 1;  
      delay(DELAY_TIME);  
   }
   else //ack error
   {
      SCL = 1;  
      delay(DELAY_TIME);  
 
     //stop bit send 
     SCL = 0;
     SDA = 0;
      delay(DELAY_TIME);  
      SCL = 1;  
      delay(DELAY_TIME);  


      SDA = 1; 


      return 1;
   }


   //high address
   uiTmp = uiAddr;
   uiTmp >>= 8;
   ucTmp = uiTmp;
  for(i = 0; i<8;i++)
  {
      SCL = 0;
 delay(DELAY_TIME);
      if(ucTmp & 0x80)
      {
      SDA = 1;
      }
 else
   {
      SDA = 0;
   }


 delay(DELAY_TIME);


 ucTmp <<=  1;


 SCL = 1;
      delay(DELAY_TIME);


  }


  //ack read
  SCL = 0;
  SDA = 1;
   delay(DELAY_TIME);
   
   if(SDA == 0)  //ack ok
   {
      SCL = 1;  
      delay(DELAY_TIME);  
   }
   else //ack error
   {
      SCL = 1;  
      delay(DELAY_TIME);  
 
     //stop bit send 
     SCL = 0;
     SDA = 0;
      delay(DELAY_TIME);  
      SCL = 1;  
      delay(DELAY_TIME);  


      SDA = 1; 


      return 1;
   }
  
   //low address
   ucTmp = uiAddr;
  for(i = 0; i<8;i++)
  {
      SCL = 0;
 delay(DELAY_TIME);
      if(ucTmp & 0x80)
      {
      SDA = 1;
      }
 else
   {
      SDA = 0;
   }


 delay(DELAY_TIME);


 ucTmp <<=  1;


 SCL = 1;
      delay(DELAY_TIME);


  }


  //ack read
  SCL = 0;
  SDA = 1;
   delay(DELAY_TIME);
   
   if(SDA == 0)  //ack ok
   {
      SCL = 1;  
      delay(DELAY_TIME);  
   }
   else //ack error
   {
      SCL = 1;  
      delay(DELAY_TIME);  
 
     //stop bit send 
     SCL = 0;
     SDA = 0;
      delay(DELAY_TIME);  
      SCL = 1;  
      delay(DELAY_TIME);  


      SDA = 1; 


      return 1;
   }


    //data send
   ucTmp = ucData;
  for(i = 0; i<8;i++)
  {
      SCL = 0;
 delay(DELAY_TIME);
      if(ucTmp & 0x80)
      {
      SDA = 1;
      }
 else
   {
      SDA = 0;
   }


 delay(DELAY_TIME);


 ucTmp <<=  1;


 SCL = 1;
      delay(DELAY_TIME);


  }


  //ack read
  SCL = 0;
  SDA = 1;
   delay(DELAY_TIME);
   
   if(SDA == 0)  //ack ok
   {
      SCL = 1;  
      delay(DELAY_TIME);  
   }
   else //ack error
   {
      SCL = 1;  
      delay(DELAY_TIME);  
 
     //stop bit send 
     SCL = 0;
     SDA = 0;
      delay(DELAY_TIME);  
      SCL = 1;  
      delay(DELAY_TIME);  


      SDA = 1; 


      return 1;
   }


     //stop bit send 
     SCL = 0;
     SDA = 0;
      delay(DELAY_TIME);  
      SCL = 1;  
      delay(DELAY_TIME);  


      SDA = 1; 


     return 0;


}




unsigned char i2c_eeprom_byte_read(unsigned int uiAddr)
{
  unsigned char ucTmp;
  unsigned int  uiTmp;
  unsigned char i;
  unsigned char ucData;
   F0=0;
   SDA = 1;
   SCL = 1;
   delay(DELAY_TIME);


   //start bit
   SDA = 0;
   delay(DELAY_TIME);
   
  //device addr
  ucTmp = 0xA0;
  for(i = 0; i<8;i++)
  {
      SCL = 0;
 delay(DELAY_TIME);
      if(ucTmp & 0x80)
      {
      SDA = 1;
      }
 else
   {
      SDA = 0;
   }


 delay(DELAY_TIME);


 ucTmp <<=  1;


 SCL = 1;
      delay(DELAY_TIME);


  }


  //ack read
  SCL = 0;
  SDA = 1;
   delay(100);
   
   if(SDA == 0)  //ack ok
   {
      SCL = 1;  
      delay(DELAY_TIME);  
   }
   else //ack error
   {
    F0=1;
   /*   SCL = 1;   //111
      delay(DELAY_TIME);  
 
     //stop bit send 
     SCL = 0;
     SDA = 0;
      delay(DELAY_TIME);  
      SCL = 1;  
      delay(DELAY_TIME);  


      SDA = 1; 


      return 1; //*/
   }


   //high address
   uiTmp = uiAddr;
   uiTmp >>= 8;
   ucTmp = uiTmp;
  for(i = 0; i<8;i++)
  {
      SCL = 0;
 delay(DELAY_TIME);
      if(ucTmp & 0x80)
      {
      SDA = 1;
      }
 else
   {
      SDA = 0;
   }


 delay(DELAY_TIME);


 ucTmp <<=  1;


 SCL = 1;
      delay(DELAY_TIME);


  }


  //ack read
  SCL = 0;
  SDA = 1;
   delay(DELAY_TIME);
   
   if(SDA == 0)  //ack ok
   {
      SCL = 1;  
      delay(DELAY_TIME);  
   }
   else //ack error
   {  F0=1;
  /*    SCL = 1;    //111
      delay(DELAY_TIME);  
 
     //stop bit send 
     SCL = 0;
     SDA = 0;
      delay(DELAY_TIME);  
      SCL = 1;  
      delay(DELAY_TIME);  


      SDA = 1; 


      return 1; //*/
   }
  
   //low address
   ucTmp = uiAddr;
  for(i = 0; i<8;i++)
  {
      SCL = 0;
 delay(DELAY_TIME);
      if(ucTmp & 0x80)
      {
      SDA = 1;
      }
 else
   {
      SDA = 0;
   }


 delay(DELAY_TIME);


 ucTmp <<=  1;


 SCL = 1;
      delay(DELAY_TIME);


  }


  //ack read
  SCL = 0;
  SDA = 1;
   delay(DELAY_TIME);
   
   if(SDA == 0)  //ack ok
   {
      SCL = 1;  
      delay(DELAY_TIME);  
   }
   else //ack error
   {   F0=1;
/*      SCL = 1;    //111
      delay(DELAY_TIME);  
 
     //stop bit send 
     SCL = 0;
     SDA = 0;
      delay(DELAY_TIME);  
      SCL = 1;  
      delay(DELAY_TIME);  


      SDA = 1; 


      return 1; //*/
   }


   //start bit
   SCL = 0;
   SDA = 1;
   delay(DELAY_TIME);  
   SCL = 1;
   delay(DELAY_TIME);  
   SDA = 0;
   delay(DELAY_TIME);  


   //device address, read
  ucTmp = 0xA1;
  for(i = 0; i<8;i++)
  {
      SCL = 0;
 delay(DELAY_TIME);
      if(ucTmp & 0x80)
      {
      SDA = 1;
      }
 else
   {
      SDA = 0;
   }


 delay(DELAY_TIME);


 ucTmp <<=  1;


 SCL = 1;
      delay(DELAY_TIME);


  }


  //ack read
  SCL = 0;
  SDA = 1;
   delay(DELAY_TIME);
   
   if(SDA == 0)  //ack ok
   {
      SCL = 1;  
      delay(DELAY_TIME);  
   }
   else //ack error
   {   F0=1;
 /*     SCL = 1;      //111
      delay(DELAY_TIME);  
 
     //stop bit send 
     SCL = 0;
     SDA = 0;
      delay(DELAY_TIME);  
      SCL = 1;  
      delay(DELAY_TIME);  


      SDA = 1; 


      return 1; //*/
   }


   //read data
  ucData = 0;
  for(i = 0; i<8;i++)
  {
      SCL = 0;
      delay(DELAY_TIME);
      if(SDA)
      {
      ucData |= 0x01;
      }
else
  {
      ucData &= (~0x01);
  }


       if(i<7)
      {
            ucData <<= 1;
       }


SCL = 1;
delay(DELAY_TIME);
  }
   
  //ack read, no ack----
  SCL = 0;
  SDA = 1;
   delay(DELAY_TIME);
   if(SDA == 1)  //ack ok
   {
      SCL = 1;  
      delay(DELAY_TIME);  
   }
   else //ack error
   {
      SCL = 1;  
      delay(DELAY_TIME);  
 


   }
   
     //stop bit send 
     SCL = 0;
     SDA = 0;
      delay(DELAY_TIME);  
      SCL = 1;  
      delay(DELAY_TIME);  


      SDA = 1;
 
 return   ucData;


}