Here is some code that has been tested for configuring the ADS1115 ADC chip connected to an 18F13K22 running @ 64Mhz. It is assumed that the address pin on the ADS1115 chip is grounded. Refer to the datasheet for more details on what each of the 16 configuration bits in register #1 are used for.
The datasheet is a bit tricky to decipher but the configuration below works fine
HI2CModeMaster#defineHI2C_BAUD_RATE125'#define HI2C_DATA PORTB.4 ;18F13K22 family#define HI2C_CLOCK PORTB.6 ;18F13K22 familydir HI2C_DATA in ;data line must be inputsdir HI2C_CLOCK in ;clock line must be inputs;ADS1115 ADC setup;set portc.2 in , to detect ready pulse from ADS1115 (optional)dir portc.2 in;ADS1115 i2c address config (assumes the address pin on the ADS1115 is grounded)#define ADSw 0x90 'i2caddressforADCwrite#defineADSr0x91'i2c address for ADC read;configure 16 bit ADC for continuous Read ** NB page 25 of ADS1115 datasheet page 25 figure 31;NB if a 5v power supply is used the 32768 scale is only partially used and FSR needs to be set to 6.144v in the config register. See datasheet as the ADS1115 can be damaged by overvoltage on the input;Write to Config register do hi2cstart hi2csend(ADSw) loop while HI2CAckpollState ;wait for ADS1115 to respond do hi2csend 0x01 ;select config register #1loop while HI2CAckpollState ;wait for ADS1115 to respond do hi2csend 0x20 ;MSB of the config register. selects single ended conversion mode, ;6.144v FSR, continuous conversion modeloop while HI2CAckpollState ;wait for ADS1115 to responddo hi2csend 0xE4 ;LSB of the config register, 860 SPS, Ready Pin is active low, ;Latch Ready Pin enabled, assert latch after one conversionloop while HI2CAckpollState ;wait for ADS1115 to respond hi2CStop ; The next blocked of code enable the conversion-ready function of the ALERT/RDY pin by setting the Hi_thresh register to 0x8000 and the Lo_thresh register to 0x0000 ;The chip pulls the 'ready'pinlowwhenaconversionhasbeencompleted.Thepingoeshighaftertheconversionregisterisread.;Access&writetoConfigregister2Lo_Threshholdregister.Write0x0000tothisregisterdohi2cstarthi2csend(ADSw)loopwhileHI2CAckpollState;waitforADS1115toresponddohi2csend0x02;selectLo_threshregister#2loopwhileHI2CAckpollState;waitforADS1115toresponddohi2csend0x00;MSBoftheLo_ThreshregisterloopwhileHI2CAckpollState;waitforADS1115toresponddohi2csend0x00;LSBoftheLo_ThreshregisterloopwhileHI2CAckpollState;waitforADS1115torespondhi2CStop;Access&writetoConfigregister3Hi_Threshholdregister.Write0x8000tothisregistertoenableconversionreadyfunctiondohi2cstarthi2csend(ADSw)loopwhileHI2CAckpollState;waitforADS1115toresponddohi2csend0x03;selectHi_threshregister#3loopwhileHI2CAckpollState;waitforADS1115toresponddohi2csend0x80;MSBoftheLo_Threshregister.SetMSBto1loopwhileHI2CAckpollState;waitforADS1115toresponddohi2csend0x00;LSBoftheLo_ThreshregisterloopwhileHI2CAckpollState;waitforADS1115torespondhi2CStop;NB:nowchangetheAddressPointerregisterto#0(conversionregister);NB:thereisnoneedtorepeatedlysettheADS1115registerpointerto0;NB:theregisterpointerwillremainlockedat0untilpoweriscycled;NB:oradifferentregisterisaccessedviacoding.;NB:powerondefaultregisterindexis0andtheconfigregister#1needstobesetup;NB:againaftereveryADS1115poweroff-oncycledohi2cstarthi2csend(ADSw)loopwhileHI2CAckpollState;waitforADS1115toresponddohi2csend0x00;pointtoconversionregisterloopwhileHI2CAckpollState;waitforADS1115torespondhi2cstop;TheADS1115chipisnowcontinuouslydoingADCconversionsandloadingthevalueintoregister#0,triggeringthereadypinwheneveryconversioniscompleted(1/860second);Toreada16bitconversionwordfromtheADS1115usethiscodedohi2cstarthi2csend(ADSr)loopwhileHI2CAckpollState;waitforADS1115torespondHI2CReceive(gold);ADS1115responsewiththeMSBoftheConversionregister.conversionregisteralreadypointedtoafterconfigroutineHI2CReceive(silver);ADS1115responsewiththeLSBoftheConversionregisterHI2CStopdata=gold*256[word]data+=silver
Last edit: Rikki 2024-06-24
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Here is some code that has been tested for configuring the ADS1115 ADC chip connected to an 18F13K22 running @ 64Mhz. It is assumed that the address pin on the ADS1115 chip is grounded. Refer to the datasheet for more details on what each of the 16 configuration bits in register #1 are used for.
The datasheet is a bit tricky to decipher but the configuration below works fine
Last edit: Rikki 2024-06-24
Rikki. Nice.
Have a look at the attachment. I will add to the demos if you can sort.
The variables are not defined for gold, silver and data. And, data is reserved so I changed to data_value.
I guess gold and silver are some sort of constant?
If you can sort then this would be great to include within the demos
Evan