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

📄 cat1025.c

📁 周立功magic2410实验箱源码 第6章Linux高级实验(part1) 6.1 Linux内核编译实验 6.2 Linux根文件系统实验 6.3 CAT1025读/写实验. 6.4 ZL
💻 C
字号:
#include <stdio.h>#include <unistd.h>#include <fcntl.h>                                /* 文件操作 */#include <sys/ioctl.h>				  /* ioctl()函数 */#include "i2c.h"                                  /* I2C总线控制器驱动命令 */#define	 I2C_ADDR 	0xA0                          /* CAT1025 从机地址 */int main(){    int fd,ret,i;    unsigned char suba;    char buf[9] = {0,1,2,3,4,5,6,7,8};   	fd = open("/dev/i2c/0", O_RDWR);              /* 打开设备 */    if(fd == -1)    {        printf("\nCan't open I2C device!\n");        exit(-1);    }     ret = ioctl(fd, I2C_TENBIT, 0);               /* 指定从机地址为7bit */    if (ret != 0)    {    	printf("\nCan't set I2C address bit number.\n");    	close(fd);    	exit(-1);    }     ret = ioctl(fd, I2C_SLAVE, I2C_ADDR >> 1);    /* 设置从机地址,7位地址,须右移1位*/    if (ret != 0)    {    	printf("\nCan't set I2C slave device address.\n");    	close(fd);    	exit(-1);    }             printf("Write CAT1025:\n"); 	for(i = 1; i < 9; i++)		printf("%d ", buf[i]);       	     ret = write(fd, buf, 9);					  /* 写8个字节到CAT1025,第1字节为子地址 */    if (ret != 9)    {    	printf("Write CAT1025 failed.\n");    	close(fd);    	exit(-1);    }        usleep(10000);                                /* 等待10ms写入完成 */        printf("\nRead CAT1025:\n");	    suba = 0;    ret = write(fd, &suba, 1);					  /* 发送从机子地址 */    if (ret != 1)    {    	printf("send slave address fail.\n");    	close(fd);    	exit(-1);    }   	    		 	 		ret = read(fd, buf, 8);       			 	  /* 从CAT1025读8个字节 */   	if (ret == 8)   	{   		for (i = 0; i < 8; i++)   			printf("%d ", buf[i]);   	}   	else   	{    	printf("Read CAT1025 failed.\n");    	close(fd);    	exit(-1);   		   	}    	printf("\nRead and Write CAT1025 sucessfully!\n");    return 0;}/****************************************************************************                            End Of File**************************************************************************/

⌨️ 快捷键说明

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