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

📄 24c64.c

📁 I2C存储芯片24C016大全
💻 C
字号:
//--------------------------
#include<reg51.h>
#include<intrins.h>
#include<absacc.h>
//-------------------------
#define uchar unsigned char
#define uint  unsigned int
//----------------------------
//----------------------------
sbit I2cSDA_24C64=P0^5;
sbit I2cSCL_24C64=P0^4;
///*************************************************
//延时子程序
//**************************************************/
void DelayX1m(uint count)
 {
  uint i;
  uchar j;
  for(i=0;i<count;i++)
     for(j=0;j<120;j++);
 }
/**************************************************/
void I2cWait_24C64(void)
 {
   _nop_();
   _nop_();
 }
///*************************************************
//初始化
//**************************************************/
void I2cInit_24C64(void)
 {
   I2cSDA_24C64=1;
   I2cSCL_24C64=1;
  }
///*******************************************
//启动器件子程序
//*******************************************/
void I2cStar_24C64(void)
 { 
    I2cSDA_24C64=1;
	I2cSCL_24C64=1;
    I2cWait_24C64();
	I2cSDA_24C64=0;
    I2cWait_24C64();
	I2cSCL_24C64=0;
  }
///*******************************************
//器件停止子程序
//*******************************************/
 void I2cStop_24C64(void)
 {  
    I2cSDA_24C64=0;
    I2cWait_24C64();
	I2cSCL_24C64=1;
	I2cWait_24C64();
	I2cSDA_24C64=1;
 }
///*******************************************
//主机发送一个字节数据至24C02子程序
//*******************************************/
 bit I2cSentByte_24C64(char bytedata)
 {
   
   uchar i;
   bit ack;
   for(i=0;i<8;i++)
      {
         if(bytedata & 0x80)
            I2cSDA_24C64=1;
		 else
	        I2cSDA_24C64=0;
		 bytedata<<=1;
         I2cWait_24C64();
	  	 I2cSCL_24C64=1;
 	  	 I2cWait_24C64();
	  	 I2cSCL_24C64=0;
 	  	 I2cWait_24C64();
	 }
   I2cSDA_24C64=1;
   I2cWait_24C64();
   I2cSCL_24C64=1;
   I2cWait_24C64();
   ack=I2cSDA_24C64;//从24C16中读取SDA的状态
   I2cSCL_24C64=0;
   I2cWait_24C64();
   return ack;
 }
///**************************************************
//主机接收一个字节数据子程序
//***************************************************/
 uchar I2cReceiveByte_24C64(void)
 {
   char i,bytedata=0;
   for(i=0;i<8;i++)
      {
	    I2cSCL_24C64=1;
        I2cWait_24C64();
		bytedata<<=1;
		if(I2cSDA_24C64)bytedata |= 0x01;
		I2cSCL_24C64=0;
        I2cWait_24C64();
		}   
   return bytedata;
   }
///*************************************************
//传送确认信号“1”或“0”电平到s
//**************************************************/
 void SendAcknowledge_24C64(bit ack)
 {
   I2cSDA_24C64=ack;
   I2cSCL_24C64=1;
   I2cWait_24C64();
   I2cSCL_24C64=0;
 }
///*************************************************
//将数据写入指定24C64地址内
//**************************************************/
 void I2cByteWrite_24C64(uchar device,uchar firstaddress,uchar bytedata)
 {
   uchar i;
   bit ack,FgTimeout=1;
   for(i=0;i<10;i++)
      {
       I2cStar_24C64();
       ack=I2cSentByte_24C64(device);
       if(ack==1)
		  {
		    I2cStop_24C64();
			continue;
		  }
	   ack=I2cSentByte_24C64(firstaddress);
	   if(ack==1)
		  {
		    I2cStop_24C64();
			continue;
		  }
//       ack=I2cSentByte_24C64(secondaddress);
	   if(ack==1)
		  {
		    I2cStop_24C64();
			continue;
		  }
	   ack=I2cSentByte_24C64(bytedata);
	   if(ack==1)
		  {
		    I2cStop_24C64();
			continue;
		  }
	    I2cStop_24C64();
		FgTimeout=0;
		if(ack==0) break;
		}
	DelayX1m(10);
}
//**************************************************
//连续写几个数据
//***************************************************
 //void I2cByteWrite_ndata_24C64(uchar device,uchar firstaddress,uchar *bytedata,uchar n)
//{
//	for(;n>0;n--)
//	{
//		 I2cByteWrite_24C64( device, firstaddress, *bytedata);
//		 firstaddress++;
//		 bytedata++;
//	}
//}
//****************************************************
//*************************************************
//将数据从指定24C64地址内读出
//**************************************************/
uchar I2cByteRead_24C64(uchar device,uchar firstaddress)//Random Read 随机读取,不是正规的读取 
 {
   uchar bytedata;
   I2cStar_24C64();
   I2cSentByte_24C64(device);
   I2cSentByte_24C64(firstaddress);//上面是发出一个写的模式
  // I2cSentByte_24C64(secondaddress);
   I2cStar_24C64();
   I2cSentByte_24C64(device|0x01);//发出读的信号
   bytedata=I2cReceiveByte_24C64();
   SendAcknowledge_24C64(1);
   I2cStop_24C64();
   return bytedata;
   }//=============================================================================

⌨️ 快捷键说明

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