From: Roman Bazalevskiy Date: Wed, 12 Apr 2017 22:09:20 +0000 (+0300) Subject: ads1115 analog-to-digital 4-channel added X-Git-Url: https://git.rvb.name/i2c-telemetry.git/commitdiff_plain/4424731ea41a70e9cfbdea74098fbebccf5f19f8?ds=inline;hp=5347abf61b167f3014ec7e0d21c95a9284a58689 ads1115 analog-to-digital 4-channel added --- diff --git a/ads1115.c b/ads1115.c new file mode 100644 index 0000000..f1fa0de --- /dev/null +++ b/ads1115.c @@ -0,0 +1,83 @@ + +#include +#include // open +#include // open +#include // open +#include // read/write usleep +#include // exit +#include // uint8_t, etc +#include // I2C bus definitions + +float getVin(int pin) { + + int fd; + int ads_address = 0x48; + + uint8_t buf[10]; + int16_t val; + uint8_t pinCodes[4]; + + pinCodes[0]=0xc3; + pinCodes[1]=0xd3; + pinCodes[2]=0xe3; + pinCodes[3]=0xf3; + + // open device on /dev/i2c-0 + if ((fd = open("/dev/i2c-0", O_RDWR)) < 0) { + printf("Error: Couldn't open device! %d\n", fd); + return 1; + } + + // connect to ads1115 as i2c slave + if (ioctl(fd, I2C_SLAVE, ads_address) < 0) { + printf("Error: Couldn't find device on address!\n"); + return 1; + } + + // AIN0 and GND, 4.096v, 128s/s + + buf[0] = 1; // config register is 1 + buf[1] = pinCodes[pin]; + buf[2] = 0x85; + if (write(fd, buf, 3) != 3) { + perror("Write to register 1"); + exit(-1); + } + + usleep(10000); + + // wait for conversion complete + do { + if (read(fd, buf, 2) != 2) { + perror("Read conversion"); + exit(-1); + } + } while (buf[0] & 0x80 == 0); + + // read conversion register + buf[0] = 0; // conversion register is 0 + if (write(fd, buf, 1) != 1) { + perror("Write register select"); + exit(-1); + } + if (read(fd, buf, 2) != 2) { + perror("Read conversion"); + exit(-1); + } + + // convert output and display results + val = (int16_t)buf[0]*256 + (uint16_t)buf[1]; + + close(fd); + + return (float)val*4.096/32768.0; +} + +main() { + + int i; + for ( i=0; i<=3; i++) { + printf("Vin%1d (V) %5.3f\n",i,getVin(i)); + } + +}