Greetings. For now, I have tested your code. It runs, but what I get from the I2C bus is the byte 0x80, so I think the PCF8591 is sending something back. At the moment I dont' use any pull up resistors, but get the same results with 10k on SCK and/or SDA. My Vref is 3,3V.
Please help. --- In [email protected], "kaffbeemer" <[EMAIL PROTECTED]> wrote: > > Hi John, > > here's the code and some new's: After inserting a sleep between > write and read I get plausible values (between 0x0 and 0x3 when > grounded). > I think the code is ok so far? > I try experimenting with the delay time. > > greez, Klaus > > > > #include "stdio.h" > #include "stdlib.h" > #include "unistd.h" > #include "sys/ioctl.h" > #include "fcntl.h" > #include "time.h" > #include "string.h" > #include <asm/etraxi2c.h> > > /* defintions for address byte */ > #define PCF8591_BASE_ADDRESS 0x90 > #define PCF8591_WRITE_MODE 0x00 > #define PCF8591_READ_MODE 0x01 > #define PCF8591_DEVICE_1 0x00 > #define PCF8591_DEVICE_2 0x02 > #define PCF8591_DEVICE_3 0x04 > #define PCF8591_DEVICE_4 0x06 > #define PCF8591_DEVICE_5 0x08 > #define PCF8591_DEVICE_6 0x0A > #define PCF8591_DEVICE_7 0x0C > #define PCF8591_DEVICE_8 0x0E > > /* defintions for control byte */ > #define PCF8591_BASE_CONTROL 0x00 > #define PCF8591_OUTPUT_ON 0x40 > #define PCF8591_OUTPUT_OFF 0x00 > #define PCF8591_4_SINGLE 0x00 > #define PCF8591_3_DIFF 0x10 > #define PCF8591_MIXED 0x20 > #define PCF8591_2_DIFF 0x30 > #define PCF8591_AUTO_INC 0x04 > #define PCF8591_CHANNEL_0 0x00 > #define PCF8591_CHANNEL_1 0x01 > #define PCF8591_CHANNEL_2 0x02 > #define PCF8591_CHANNEL_3 0x03 > > > // Global variables > int fd_i2c; > > > int I2C_Open(void) { > fd_i2c = open("/dev/i2c", O_RDWR); > if (fd_i2c <= 0) { > printf("I2C: open error on /dev/i2c\n"); > return(-1); > } > > return(0); > } > > void I2C_Close(void) { > close(fd_i2c); > } > > int main(int argc, char **argv) { > int retcode; > float vref=5.15f; > > retcode = I2C_Open(); > if (retcode != 0) { > exit(1); > } > > I2C_DATA i2cdata; > int i=0; > int ch=0; > for (ch=0; ch<4; ch++) { > printf ("Reading channel: %i\n", ch); > for (i=0; i<5; i++) { > // now use a register write 1 byte to register 1 and > read it back (pcf8591) > i2cdata.reg = 1; > i2cdata.length = 1; > i2cdata.slave = (PCF8591_BASE_ADDRESS | > PCF8591_DEVICE_1 | PCF8591_WRITE_MODE); > i2cdata.data[0] = (PCF8591_BASE_CONTROL | > PCF8591_OUTPUT_OFF | PCF8591_4_SINGLE | PCF8591_CHANNEL_0)+ch; > printf ("PCF8591 control byte: 0x%x\n", 0x00ff & > i2cdata.data[0]); > ioctl(fd_i2c, _IO(ETRAXI2C_IOCTYPE, I2C_WRITEREG_N), > &i2cdata); > sleep (1); > > i2cdata.reg = 1; > i2cdata.length = 1; > i2cdata.data[0] = 0; > ioctl(fd_i2c, _IO(ETRAXI2C_IOCTYPE, I2C_READREG_N), > &i2cdata); > > printf ("PCF8591 returned: 0x%x = %6.2fV\n", 0x00ff > & i2cdata.data[0], vref/255.0f*(float)i2cdata.data[0]); > sleep (1); > } > } > I2C_Close(); > > return(0); > } >
