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

📄 eeprom.c

📁 sfsf sfsf
💻 C
字号:
#include <stdio.h>
#include <fcntl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>

#define CHIP_ADDR 0x50  // 设备地址
#define PAGE_SIZE 8    //页写入大小
#define I2C_DEV   "/dev/i2c/0"

static int read_eeprom (int fd,char buff[],int addr,int count)
{
  int res;
  if (write(fd,&addr,1)!=1)  //写地址失败
     return -1;
  res=read(fd,buff,count);
  printf("read %d byte at 0x %x\n",res,addr);
  return res;
}
//缓冲区不能超过一页
static int write_eeprom (int fd, char buff[],int addr,int count)
{
  int res;
  int i;
  static sendbuffer[PAGE_SIZE];
  memcpy(sendbuffer + 1,buff,count);
  sendbuffer[0] = addr;
  res = write(fd,&addr,count + 1);
  printf("write %d byte at 0x%x\n",res,addr);
  
}

int main(void)
{
   int fd,n,res;
   unsigned char buf[PAGE_SIZE]={1,2,3,4,5,6,7,8};
   
   fd = open(I2C_DEV,O_RDWR);
   if(fd<0)
   	{
   		printf("####i2c test device open failed ####\n");
   		return(-1);
    }
  res = ioctl(fd,I2C_TENBIT,0	);  //不是10位模式
  res = ioctl(fd,I2C_SLAVE,CHIP_ADDR);//设置I2C从设备地址【6:0】
  
  write_eeprom(fd,buf,0,sizeof(buf));
  read_eeprom(fd,buf,0,sizeof(buf));
  
  for(n= 0;n<sizeof(buf);n++)
     printf(" 0x%x",buf[n]);
     close(fd);
     return(0);  	
}

⌨️ 快捷键说明

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