1 //***************************************************
4 // Example to read A/D values from a
5 // 4 channel / 8 bit AD converter PCF8591
6 // through I2C using the I2C driver improved by
7 // Geert Vancompernolle
8 // http://www.acmesystems.it/?id=10
9 //***************************************************
14 #include "sys/ioctl.h"
19 #include "i2c_errno.h"
23 int main( int argc, char **argv ) {
29 printf("Reading from a PCF8591 (4 chanel A/D at 8 bits with I2C bus)\n");
31 fd_i2c = open( "/dev/i2c-0", O_RDWR );
33 printf( "Open error on /dev/i2c\n" );
37 // PCF8591 address scheme
38 // | 1 | 0 | 0 | 1 | A2 | A1 | A0 | R/W |
39 // i2c_d.slave =(0x09<<4)|(0x01<<1);
43 for (ch=0;ch<=3;ch++) {
44 // Select the A/D channel
47 if ((rtc=ioctl(fd_i2c,_IO( ETRAXI2C_IOCTYPE, I2C_WRITE), &i2c_d))!=EI2CNOERRORS) {
49 printf( "Error %d on line %d\n",rtc,__LINE__);
54 if ((rtc=ioctl(fd_i2c,_IO( ETRAXI2C_IOCTYPE, I2C_READ), &i2c_d))!=EI2CNOERRORS) {
56 printf( "Error %d on line %d\n",rtc,__LINE__);
60 // Show the voltage level
61 printf("Chanel %d = %.2fv (%02X hex)\n",ch,i2c_d.rbuf[2]*0.012941,i2c_d.rbuf[2]);