#include "ads1115.h" //initial address of ads1115 is 1001000(7 bit).So in order to write to ads1115,we have to send address+0 i.e 0b10010000=144(d).In order to read from it,we have to send address+1 i.e 0b10010001=145(d) uint16_t read_ads1115_single_ended(uint8_t ch)//in this function,we will read the ads1115's desired channel with all default values { uint8_t read_MSB=0;//two variable to save read results uint8_t read_LSB=0; uint8_t MSB_config,LSB_config; uint16_t data; LSB_config=0b000011;//LSB of config resistor(D7 to D0),D7D6D5=000(8 samples/sec),D4=0(comparator mode,no use for us),D3=0(comparator mode,no use for us),D2=0(Latching of comparator,non latching here),D1D0=11(disable comparator) switch(ch) { case 0: MSB_config=0b11000011;//MSB of config resistor(D15 to D8),D15=1(single ended conv),D14D13D12=100(Ain(0) and GND as inputs),D11D10D9=001(gain of 1,D8=1(power down single shot mode)) break; case 1: MSB_config=0b11010011;//D14D13D12=101(Ain(1) and GND as inputs),rest are same break; case 2: MSB_config=0b11100011;//D14D13D12=110(Ain(2) and GND as inputs),rest are same break; case 3: MSB_config=0b11110011;//D14D13D12=111(Ain(3) and GND as inputs),rest are same break; } twi_init(); twi_start();//send a start condition twi_send(0x90);//send 10010000 to write into ads1115 twi_send(0x01);//This value is getting saved in pointer register.write 01 to pointer register to point to config resistor in order to write the configs into it. twi_send(MSB_config);//config bits are stated above in details twi_send(LSB_config); twi_stop(); //now configurations are uploaded,we will read it back from the ADC by putting it into transmitter mode.In order to know if the conversion is complete,we will poll the D15 bit of the MSB of Config resistor.It will become 1 at the end of one conversion while((read_MSB & 0b10000000)==0)//stay in the loop and read agin & again until D15 becomes 1 to indicate end of conversion { twi_start(); twi_send(0x91);//send 10010001 to read from ads1115 read_MSB=twi_receive(1);//read MSB from config resistor.No need to point to config resistor as last time we pointed to confog resister and the pointer resister is still holding the value read_LSB=twi_receive(0);//read LSB from config resistor. twi_stop(); } //now as the conversion is completed,we will read the result.So for this we have to point to the conversion register twi_start(); twi_send(0x90);//send address+0 to write into twi_send(0x00);//write 0 into pointer resister in order to point to conversion register as we will read the conversion result from the conversion result twi_stop(); //now read twi_start(); twi_send(0x91);//send address+1 to read from the conversion register read_MSB=twi_receive(1);//read MSB with ACK read_LSB=twi_receive(0);//read LSB with NACK. twi_stop();//release the bus data =((read_MSB<<8)|(read_LSB));//combine the 16 bit data if(data>32768)//if data is greater than 32768,then it is false because we are using single ended mode here return 0; else return data; }