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

📄 i2c_test.c

📁 嵌入式LINUX 的驱动程序。采用2410的开发板全部可以通用(如使用引脚不同只要重新改脚定义)
💻 C
字号:
/*************************************  *GEC-2410-BOX*  *eeprom at24c02 test program************************************/#include <stdio.h>#include <fcntl.h>#include <linux/i2c.h>#include <linux/i2c-dev.h>#define CHIP_ADDR 	0x50#define PAGE_SIZE	0x08#define I2C_DEV_PATH   "/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%.2x\n", res, addr);	return res;}static int write_eeprom(int fd, char  buff[], int addr, int count){	int res;	int i;	char sendbuffer[PAGE_SIZE+1];	memcpy(sendbuffer+1, buff, count);	sendbuffer[0]=addr;	 res= write(fd, sendbuffer, count+1);	 printf("write %d byte at 0x%.2x\n", res ,addr);}int main(void){	int fd, n, res;	unsigned char buf[PAGE_SIZE];		fd = open(I2C_DEV_PATH,  O_RDWR);	if(fd < 0)	{		printf("####i2c test device open fail####\n");		return (-1);	}	printf("success, i2c open file device %d \n",fd);		res = ioctl(fd, I2C_TENBIT,0);							//do not use 10bit address mode	res = ioctl(fd, I2C_SLAVE_FORCE, CHIP_ADDR);			//set slave device address	printf("ioctl I2C_SLAVE_FORCE msg NO:%d\n",res);	for(n=0; n<PAGE_SIZE; n++)		buf[n]=n;	write_eeprom(fd, buf, 0, sizeof(buf));	read_eeprom(fd, buf, 0, sizeof(buf));	for(n=0; n<sizeof(buf); n++) {		printf("0x%.2x, ", buf[n]);	}	printf("\n");		close(fd);	return(0);}

⌨️ 快捷键说明

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