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

📄 24c04rw.h

📁 这是一个模拟I2C通信协议的C语言模块
💻 H
字号:
/*****************************************************************************/
/*                          AT24C16读写驱动程序                              */
/*****************************************************************************/
//模块调用:
//读数据:read_24C04(unsigned int address,unsigned int mun_data,unsigned char *fp_data)
//address:读曲数据的首地址;mun_data:要读取的字节个数;*fp_data:读取数据存放区指针
//写数据:write_24C04(unsigned int address,unsigned int mun_data,unsigned char *fp_data)
//address:写入数据的首地址;mun_data:要写入的字节个数;*fp_data:写入数据存放区指针
//sda和scl根据实际情况在此头文件前自行定义
//用户自定义读写缓冲区
////////////////////////////////////////////////////////////////////////////////
//                               延时函数                                     //
////////////////////////////////////////////////////////////////////////////////
void ys(int s)
{   
     int i;
     for(i=0;i<s;i++);
}

////////////////////////////////////////////////////////////////////////////////
//                               起始函数                                     //
////////////////////////////////////////////////////////////////////////////////
void s24(void)                 
{
    ys(1);
    scl=0; 
    sda=1;
    scl=1;
    ys(1);
    sda=0;
    ys(1);
    scl=0;
    ys(1);
    sda=1;
}

////////////////////////////////////////////////////////////////////////////////
//                                 停止函数                                   //
////////////////////////////////////////////////////////////////////////////////
void p24(void)                
{
    sda=0;
    scl=1;
    ys(1);
    sda=1;
}

////////////////////////////////////////////////////////////////////////////////
//                            从24c16读一字节数据                             //
////////////////////////////////////////////////////////////////////////////////
unsigned char rd24(void) 
{       
        bit sample;
        char r_data=0;
        int i;
        sda=1;
        for(i=0;i<8;i++)
        {
              char sample;
              scl=1;
              sample=sda;
              r_data*=2;
              if(sample==1)r_data++;
              ys(4);scl=0;
        }
sda=1;scl=1;ys(4);scl=0;                                   //24c16的一位回答位。
return(r_data);
}

////////////////////////////////////////////////////////////////////////////////
//                             向24c16写一字节数据                            //
////////////////////////////////////////////////////////////////////////////////
void wd24(unsigned char dd)
{    
     int i;
     unsigned char w_data;
     w_data=dd;
     sda=1;
     for(i=0;i<8;i++)
     {  
        sda = (bit)(w_data & 0x80);
        w_data = w_data << 1;
        scl=1;ys(4);scl=0;
        
     }
     sda=0;scl=1;;;scl=0;
}

////////////////////////////////////////////////////////////////////////////////
//                     启动24C04读取功能,写入地址                            //
////////////////////////////////////////////////////////////////////////////////
unsigned char read(unsigned int address)
{
unsigned char dd;
    s24();                                       //开始条件
    wd24(0xa0);                                  //写器件地址(写命令)
    ys(4);
    scl=0;                                      //接收器件地址确认信号
    wd24(address);                               // 写数据地址
    ys(4);
    scl=0;
    s24();                                       //开始条件
    wd24(0xa1);                                  //写器件地址(读命令)
    scl=0;
    dd=rd24();                                   //读 一字节
    p24();                                       //停止条件
    return(dd);
}

////////////////////////////////////////////////////////////////////////////////
//                                 写地址                                     //
////////////////////////////////////////////////////////////////////////////////
void write(unsigned int address)
{


    s24();                                       //开始条件
    wd24(0xa0);                                  //写器件地址;
    scl=0; 
    wd24(address);                               //写数据地址
    scl=0;
} 

////////////////////////////////////////////////////////////////////////////////
//                               写入数据                                     //
////////////////////////////////////////////////////////////////////////////////
void  write_24C04(unsigned int address,unsigned int mun_data,unsigned char *fp_data)
{
      int z,i;
      int j=0;
      z=address+mun_data;
      write(address);
      for(i=address;i<z;i++)
          {
               wd24(fp_data[j]);                 //写dd数据
               j++;
                
          }
      p24();                                     //停止条件
      ys(60);
}

////////////////////////////////////////////////////////////////////////////////
//                               读取数据                                     //
////////////////////////////////////////////////////////////////////////////////
void  read_24C04(unsigned int address,unsigned int mun_data,unsigned char *fp_data)
{
      int z,i;
      int j=0;
      z=address+mun_data;
      for(i=address;i<z;i++)
          {
               fp_data[j]=read(i);
               j++;
          }
      p24();
      ys(60);
}

⌨️ 快捷键说明

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