Linux下iic(i2c)读写AT24C02(2)

for(retries=5; retries; retries--)
                if(write(fd, &reg, 1)==1)
                        if(read(fd, val, 1)==1)
                                return 0;
        return -1;
}

//输出数组
static uint8 printarray(uint8 Array[], uint8 Num)
{
  uint8 i;
 
  for(i=0;i<Num;i++)
  {
    printf("Data [%d] is %d \n", i ,Array[i]);
  }
 
  return(1);
}

//主函数
int main(int argc, char *argv[])
{
  int i;
 
  AT24C02_Init();
  usleep(1000*100);
    for(i=0; i<sizeof(rbuf); i++)
            if(i2c_read(fd, i, &rbuf[i]))
                    break;
  printarray(rbuf ,8);
  printf("Before Write Data \n");
  sleep(1);
    for(i=0; i<sizeof(rbuf); i++)
            if(i2c_write(fd, i, wbuf[i]))
                    break;
  printarray(wbuf ,8);
  printf("Writing Data \n");
  sleep(1);
    for(i=0; i<sizeof(rbuf); i++)
            if(i2c_read(fd, i, &rbuf[i]))
                    break;
  printarray(rbuf ,8);
  printf("After Write Data \n");
 
  close(fd);
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwfddw.html