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

📄 fm24cl64.c

📁 FM24CL64驱动C语言程序
💻 C
📖 第 1 页 / 共 2 页
字号:
******************************************************************************
******************************************************************************
******************************************************************************
*Description :向IIC总线发出应答信号
*Arguments   :none  
*Returns     :none 
*Nvotes      :主机产生应答位,用于通知从机主机成功接收从机数据
*************  主机在收到每一个字节后都要产生应答
******************************************************************************
*/
void FM_ACK(void)
{  
  FMSDA=0;   
  nNop(20); 
  FMSCL=1; 
  nNop(20); 
  FMSCL=0;
  nNop(20); 
}
/*
******************************************************************************
******************************************************************************
******************************************************************************
*Description :向IIC总线发撤泅应答信号
*Arguments   :none 
*Returns     :none 
*Nvotes      :主机产生非应答位,用于通知从机主机没有成功接收从机数据
*************  主机在收到最后一个字节后应当产生非应答
******************************************************************************
*/
void FM_NoACK(void)
{  
  FMSDA=1;  
  nNop(20); 
  FMSCL=1; 
  nNop(20);  
  FMSCL=0;
  nNop(50);  
}

/*
******************************************************************************
******************************************************************************
******************************************************************************
*Description :向IIC总线写一个字节数据
*Arguments   :参数WriteData为数据  
*Returns     :none
*Nvotes      :
******************************************************************************
*/


void FM_WriteByte(uint8 WriteData)
{  
   uint8 i;
   FMSDA_out;	  //改变FMSDA为输出口
  for (i=0; i<8; i++)
  {  
     FMSCL=0;       
    if (((WriteData >> 7) & 0x01) == 0x01)  
      FMSDA=1;
    else    
      FMSDA=0; 
     FMSCL=1;  
    nNop(20); 
    WriteData = WriteData << 1;         
  }
  FMSCL=0;
  nNop(20);  
  FMSDA=1;
  nNop(50); 
  }


/*
******************************************************************************
******************************************************************************
******************************************************************************
*Description :向IIC总线上的从机(FM24CL64)某一地址写入数据
*Arguments   :address:要写入的地址  val:要写入的数据 
*Returns     :none 
*Nvotes      :单字节写操作
******************************************************************************
*/
void FM_WriteSingleData(uint16 address,uint8 val)
{
  uint8 i,high5address,low8address;
  
  low8address = (address&0x0FF);       //取出低八位地址
  
  high5address = ((address&0x700)>>8); //取出高五位地址
  
  FM_Start();                          //开始条件
  FM_WriteByte(0xA0);                  //发从机地址     1 0 1 0 A2 A1 A0 R=1/W=0 
                                       //               1 0 1 0 0  0  0  0
  FM_GetACK();    
  
  FM_WriteByte(high5address);          //发送高5位地址
  FM_GetACK();

  FM_WriteByte(low8address);           //发送低8位地址
  FM_GetACK();
  FM_WriteByte(val);
  FM_GetACK();
  FM_Stop();
  //for(i=0;i<0x1fff;i++){}   ///写后的延时,否则导致读错误
  for(i=0;i<0x2fff;i++){}
}



/*
******************************************************************************
******************************************************************************
******************************************************************************
*Description :从IIC总线读一字节数据
*Arguments   :none  
*Returns     :返回一字节数据ReadData
*Nvotes      :
******************************************************************************
*/
uint8 FM_ReadByte(void)
{  
  uint8 i;  
  uint8 ReadBit  = 0;  
  uint8 ReadData = 0;
    
  FMSCL=0;  
  nNop(20); 
  FMSDA_in;  //改FMSDA为输入口
  for (i=0; i<8; i++)  
  {      
    nNop(20); 
    FMSCL=1;
    FMSDA_in;           
    if (FMSDA_ReadOneBit == 0x01)      
      ReadBit = 1;   
    else    
      ReadBit = 0;        
    ReadData = (ReadData << 1) | ReadBit;    
    FMSCL=0; 
   } 
   FMSDA_out;
   nNop(50);  
   return(ReadData);
}



/*
******************************************************************************
******************************************************************************
******************************************************************************
*Description :从FM24CL64某一地址读取数据
*Arguments   :address:要读数据的地址 
*Returns     :data:从指定地址读出的数据
*Nvotes      :选择性(随机)读操作
******************************************************************************
*/
uint8 FM_ReadSingleData(uint16 address)
{ 
  uint8 data;
  uint8 high5address,low8address;
  
  low8address = (address&0x0FF);       //取出低八位地址
  high5address = ((address&0x700)>>8); //取出高五位地址 
  
  FM_Start();
  FM_WriteByte(0xA0);                  //发从机地址字节 1 0 1 0 A2 A1 A0 R=1/W=0
                                       //               1 0 1 0 0  0  0  0写
  
  FM_GetACK(); 
    
  FM_WriteByte(high5address);
  FM_GetACK(); 
  FM_WriteByte(low8address );
  FM_GetACk(); 
  
  FM_Start();
  FM_WriteByte(0xA1);                  //发从机地址字节 1 0 1 0 A2 A1 A0 R=1/W=0
                                       //               1 0 1 0 0  0  0  1读
  FM_GetACK(); 
  data = FM_ReadByte(); 
  FM_NoACK();  
  FM_Stop();  
  nNop(50); 
  return(data);  
}

/*
******************************************************************************
******************************************************************************
******************************************************************************
*Description :
*Arguments   : 
*Returns     :none 
*Nvotes      :
******************************************************************************
*/
void  nNop(uint16 i)
{
  for(;i>0;i--);

}


/*
******************************************************************************
******************************************************************************
******************************************************************************
*Description :
*Arguments   : 
*Returns     :none 
*Nvotes      :
******************************************************************************
*/
void   LongDelay(uint16 i)
{
   uint16 j;
   
   for(;i>0;i--){
      for(j=1000;j>0;j--);
   }
}

/*********************************************************************************************************
**                            End Of File
********************************************************************************************************/

⌨️ 快捷键说明

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