Diese Programmzeilen wurden mir dankenswerterweise von einem Kunden geschickt. Das Programm horter_adc5 liest alle 11 Byte und berechnet gleich die Spannung in V.
#include <unistd.h> #include <fcntl.h> #include <sys/ioctl.h> #include <linux/i2c-dev.h> #include <stdio.h> int main(void) { float adc[5]; int r, i; char buffer[11]; int fd = open("/dev/i2c-2", O_RDWR); if (fd < 0) { return 1; } if (ioctl(fd, I2C_SLAVE, 0x08) < 0) { close(fd); return 2; } r = write(fd, (char[]){ 0 }, 1); if (r < 0) { close(fd); return 3; } r = read(fd, buffer, sizeof(buffer)); close(fd); printf("read: %d\n", r); if (r < 0) { return 4; } for(i = 1; i < sizeof(buffer); ++i) printf("%02x ", buffer[i]); putchar('\n'); putchar('\n'); for(i = 0; i < 5; ++i) { adc[i] = buffer[(i*2)+2]*256+buffer[(i*2)+1]; printf("ADC%d = %.3fV\n", i, adc[i]*10/999.0234375 ); //printf("ADC%d = %4d\n", i, buffer[(i*2)+2]*256+buffer[(i*2)+1] ); } putchar('\n'); return 0; }