/*****RAS-32IO-V2 I2C Write Sample Program*****/ #include #include #include #include #include #include #include #include #include int fd; //char *i2cdev = "/dev/i2c-0"; char *i2cdev = "/dev/i2c-1"; unsigned char buf[2]; int i2cwrite(char *buf, int len) { if((write(fd, buf, len)) != len){ return(1); } return(0); } main() { unsigned char SLAVE_ADDRESS = 0x11; //SLAVE_ADDRESS unsigned char rddat; printf("**** I2C READ SAMPLE ****\n"); if((fd = open(i2cdev, O_RDWR)) < 0){ printf("Can't open i2c\n"); exit(1); } if(ioctl(fd,I2C_SLAVE, SLAVE_ADDRESS) < 0){ printf("Unable to get bus access to talk to slave\n"); exit(1); } //PORT 0 WRITE// buf[0] = 0x00; buf[1] = 0xaa; if(i2cwrite(buf,2)){ printf("write error SA = %02x\n",SLAVE_ADDRESS); exit(1); } if(ioctl(fd, I2C_SLAVE,SLAVE_ADDRESS) < 0) { printf("Unable to bus access to talk to slave\n"); exit(1); } //PORT 1 WRITE// buf[0] = 0x01; buf[1] = 0x55; if(i2cwrite(buf,2)){ printf("write error SA = %02x\n",SLAVE_ADDRESS); exit(1); } if(ioctl(fd, I2C_SLAVE,SLAVE_ADDRESS) < 0) { printf("Unable to bus access to talk to slave\n"); exit(1); } close(fd); return 0; }