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

📄 at24c64.c

📁 这是一个c51写的AT24C64芯片的驱动
💻 C
字号:
//------------------------------------------------------------------------------
#include    <intrins.h>
#include    "TypeDef.h"
#include    "AT78E58BP.h"
#include    "AT24C64.h"
//------------------------------------------------------------------------------
#define   I2C_SDA  P2_7
#define   I2C_SCK  P2_6
#define   I2C_WP   P2_1
//#define   DOG_WDI  P2_2
 
//------------------------------------------------------------------------------
  //Comm Private:私有函数和成员
//------------------------------------------------------------------------------

bit   I2C_Start(void);
void  I2C_Stop(void);
void  I2C_Ack(void);
void  I2C_Nack(void);
bit   AT24C64_Clock(void);
bit   I2C_Send_Byte(uchar);
uchar I2C_Receive_Byte(void);
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
void  Delay_10_uS(void)
{
//char i=20;
// while(i--);
_nop_();
}
//------------------------------------------------------------------------------
void Delay_N_mS( uint n_milisecond)  /* n mS delay */
{
uchar i;
 while(n_milisecond--)
 {
  i=57;
  while(i--);
 }
}
//------------------------------------------------------------------------------
bit I2C_Start(void)
{
 Delay_10_uS();
 I2C_SDA =1;
 Delay_10_uS();
 I2C_SCK =1;
 Delay_10_uS();
 if ( I2C_SDA == 0) return 0;
 if ( I2C_SCK == 0) return 0;
 I2C_SDA = 0;
 Delay_10_uS();
 I2C_SCK = 0;
 Delay_10_uS();
 return 1;
}
//------------------------------------------------------------------------------
void  I2C_Stop(void)
{
 Delay_10_uS();
 I2C_SDA = 0;
 Delay_10_uS();
 I2C_SCK = 1;
 Delay_10_uS();
 I2C_SDA = 1;
 Delay_10_uS();
 I2C_SCK = 0;
 Delay_10_uS();
}
//------------------------------------------------------------------------------
/*void I2C_Ack(void)
{
 Delay_10_uS();
 I2C_SDA=0;
 Delay_10_uS();
 I2C_SCK=1;
 Delay_10_uS();
 I2C_SCK=0;
 Delay_10_uS();
}*/
//------------------------------------------------------------------------------
void I2C_Nack(void)
{
 Delay_10_uS();
 I2C_SDA=1;
 Delay_10_uS();
 I2C_SCK=1;
 Delay_10_uS();
 I2C_SCK=0;
 Delay_10_uS();
}
//------------------------------------------------------------------------------
bit I2C_Send_Byte( uchar d)
{
 uchar i = 8;
 bit bit_ack;
 while( i-- )
 {
  Delay_10_uS();
  if ( d &0x80 )   I2C_SDA =1;
  else             I2C_SDA =0;
  Delay_10_uS();
  I2C_SCK = 1;
  Delay_10_uS();
  I2C_SCK = 0;
  d = d << 1;
 }
 Delay_10_uS();
 I2C_SDA = 1;
 Delay_10_uS();
 I2C_SCK = 1;
 Delay_10_uS();
 bit_ack = I2C_SDA;
 I2C_SCK =0;
 Delay_10_uS();
 return ~bit_ack;
}
//------------------------------------------------------------------------------
bit I2C_Send_Addr(uint Address)
{
I2C_Start();
/*I2C_Send_Byte( 0xa0 + AT24C64_address / 256 *2 );*/   /* 24C16 USE */
if(I2C_Send_Byte( 0xa0 ))
  {
  return ((I2C_Send_Byte(Address/256))
	     &(I2C_Send_Byte(Address%256)));
  }
  else
  { 
    return false;
  };//end if
}
//------------------------------------------------------------------------------
uchar I2C_Receive_Byte(void)
{
 uchar i = 8, d;
 Delay_10_uS();
 I2C_SDA = 1;
 while ( i--)
 {
  d = d << 1;
  Delay_10_uS();
  I2C_SCK =1;
  if ( I2C_SDA ) d++;
  Delay_10_uS();
  I2C_SCK =0;
 }
 return d;
}
//------------------------------------------------------------------------------
void  AT24C64_Init(void)
{
 I2C_SCK =0;
 I2C_Stop();
 I2C_WP=0;
}
//------------------------------------------------------------------------------
/*bit  AT24C64_Clock(void)
{
 bit Result;

 I2C_SCK =1;
 Delay_10_uS();
 Result = I2C_SDA;
 I2C_SCK =1;
 return Result;
}*/
//------------------------------------------------------------------------------
void AT24C64_Write(uchar *mcu_address,uint AT24C64_address,uint count)
{
 //DOG_WDI=!DOG_WDI;
 //DOGTIME=0;
 while(count)
 {
   I2C_Start();
   /*I2C_Send_Byte( 0xa0 + AT24C64_address /256 *2);*/  /* 24C16  USE */
   if(  I2C_Send_Addr(AT24C64_address)
      &&I2C_Send_Byte( *mcu_address ))
	 {
       I2C_Stop();
       Delay_N_mS(2);       /* waiting for write cycle to be completed */
       mcu_address++;
       AT24C64_address++;
	   count--;
 	 }else I2C_Stop(); //end if
 };//end while
}
//------------------------------------------------------------------------------
void AT24C64_Read(uchar *mcu_address,uint AT24C64_address,uint count)
{
 //DOG_WDI=!DOG_WDI;
 //DOGTIME=0;
 while(count--)
 {
  I2C_Start();
  /*I2C_Send_Byte( 0xa0 + AT24C64_address / 256 *2 );*/   /* 24C16 USE */
  I2C_Send_Byte( 0xa0 );
  I2C_Send_Byte( AT24C64_address/256 );
  I2C_Send_Byte( AT24C64_address % 256 );
  I2C_Start();
  /*I2C_Send_Byte( 0xa1 + AT24C64_address /256 *2 );*/
  I2C_Send_Byte( 0xa1 );
  *(uchar*)mcu_address = I2C_Receive_Byte();
  I2C_Nack();
  I2C_Stop();
  ((uchar*)mcu_address)++;
  AT24C64_address++;
 }

 //DOG_WDI=!DOG_WDI;
 //DOGTIME=0;
/* while(count)
 {
  I2C_Start();
  if(I2C_Send_Addr(AT24C64_address))
    {
      I2C_Start();
	  /*I2C_Send_Byte( 0xa1 + AT24C64_address /256 *2 );*/
/*      if(I2C_Send_Byte(0xa1))
	    {
          *mcu_address = I2C_Receive_Byte();
           I2C_Nack();
           I2C_Stop();
           mcu_address++;
           AT24C64_address++;
           count--;
		}else I2C_Stop();//end if
    } else I2C_Stop();//end if
 };//end while*/
}
//------------------------------------------------------------------------------

⌨️ 快捷键说明

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