Menu

Configuring ADS1115 ADC chip

Rikki
2024-06-24
2024-06-24
  • Rikki

    Rikki - 2024-06-24

    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

    HI2CMode  Master
    #define HI2C_BAUD_RATE 125   '
    #define HI2C_DATA  PORTB.4  ;18F13K22  family
    #define HI2C_CLOCK PORTB.6  ;18F13K22  family
    
    dir HI2C_DATA  in ;data line must be inputs
    dir 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  'i2c address for ADC write
    #define ADSr 0x91  '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 #1
    
    loop 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 mode
    
    loop while HI2CAckpollState ;wait for ADS1115 to respond
    
    do
    
      hi2csend 0xE4  ;LSB of the config register, 860 SPS, Ready Pin is active low, 
                                                 ;Latch Ready Pin enabled, assert latch after one conversion
    
    loop 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' pin low when a conversion has been completed. The pin goes high after the conversion register is read.
    
      ;Access & write to Config register 2 Lo_Threshhold register. Write 0x0000 to this register
    
     do
    
        hi2cstart         
        hi2csend(ADSw)  
    
    loop while HI2CAckpollState ;wait for ADS1115 to respond
    
    do
    
          hi2csend 0x02  ;select Lo_thresh register #2
    
    loop while HI2CAckpollState ;wait for ADS1115 to respond
    
    do
    
       hi2csend 0x00  ;MSB of the Lo_Thresh register
    
    loop while HI2CAckpollState ;wait for ADS1115 to respond
    
     do
    
           hi2csend 0x00  ;LSB of the Lo_Thresh register
    
    loop while HI2CAckpollState ;wait for ADS1115 to respond
    
    hi2CStop
    
    
     ;Access & write to Config register 3 Hi_Threshhold register. Write 0x8000 to this register to enable conversion ready function
    
     do
           hi2cstart         
           hi2csend(ADSw)   
    
    loop while HI2CAckpollState ;wait for ADS1115 to respond
    
     do
    
          hi2csend 0x03  ;select Hi_thresh register #3
    
    loop while HI2CAckpollState ;wait for ADS1115 to respond
    
    
     do
    
             hi2csend 0x80  ;MSB of the Lo_Thresh register. Set MSB to 1
    
     loop while HI2CAckpollState ;wait for ADS1115 to respond
    
     do
    
              hi2csend 0x00  ;LSB of the Lo_Thresh register
    
      loop while HI2CAckpollState ;wait for ADS1115 to respond
    
     hi2CStop
    
    ;NB  :now change the Address Pointer register to #0 (conversion register)
    ;NB : there is no need to repeatedly set the ADS1115 register pointer to 0
    ;NB : the register pointer will remain locked at 0 until power is cycled 
    ;NB : or a different register is accessed via coding.
    ;NB : power on default register index is 0 and the config register #1 needs to be setup ;NB : again after every ADS1115 power off-on cycle
    
     do
              hi2cstart
              hi2csend(ADSw) 
    
    loop while HI2CAckpollState ;wait for ADS1115 to respond
    
      do
    
           hi2csend 0x00  ;point to conversion register
    
      loop while HI2CAckpollState ;wait for ADS1115 to respond
    
      hi2cstop
    
    ;The ADS1115 chip is now continuously doing ADC conversions and loading the value into register #0, triggering the ready pin when every conversion is completed (1/860 second)
    
    ;To read a 16 bit conversion word from the ADS1115 use this code
    
      do
    
                hi2cstart
                hi2csend(ADSr)       
    
    loop while HI2CAckpollState ;wait for ADS1115 to respond
    
    
                HI2CReceive(gold)  ;ADS1115 response with the MSB of the Conversion register. conversion register already pointed to after config routine
    
               HI2CReceive(silver) ;ADS1115 response with the LSB of the Conversion register
    
                HI2CStop
    
               data = gold * 256 [word] 
    
               data +=silver
    
     

    Last edit: Rikki 2024-06-24
  • Anobium

    Anobium - 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

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.