News:

Let's find out together what makes a PIC Tick!

Main Menu

ADS1115

Started by DymDym, Jun 11, 2024, 01:16 PM

Previous topic - Next topic

DymDym

hello!
Does anyone have a working code for ads1115 ?
(Proton PicBasic)...

Pepe

#1
Demo with Proteus for ADS 1015(12bits)

Pepe

New version in post #2

Dolci

Hi Pepe,
 do you have a later version of Proteus for the demo file? Mine is V8.15. Thanks

Pepe

#4
Proteus v15

Pepe

Quote from: Dolci on Jun 12, 2024, 10:25 AMHi Pepe,
 do you have a later version of Proteus for the demo file? Mine is V8.15. Thanks

look post #5

DymDym

I once had a working code like this:

' ADS1115
' A0 - "+"; A1- "-"

Device = 18F452
Xtal = 20

@Config_REQ
@__Config Config1H, OSCS_OFF_1 & HS_OSC_1
@__Config Config2L, BOR_OFF_2 & BORV_20_2 & PWRT_ON_2
@__Config Config2H, WDT_OFF_2 & WDTPS_128_2
@__Config Config3H, CCP2MX_OFF_3
@__Config Config4L, STVR_OFF_4 & LVP_OFF_4 & DEBUG_OFF_4

'-----------------------------------------------------------
All_Digital = TRUE
'-----------------------------------------------------------
Declare LCD_Type ALPHA           
Declare LCD_DTPin PORTB.4         
Declare LCD_ENPin PORTB.3         
Declare LCD_RSPin PORTB.2         
Declare LCD_Interface 4 
Declare LCD_CommandUs 2000
Declare LCD_DataUs 50 
Declare LCD_Lines 2 
Clear
 
Symbol ADS_SDA = PORTD.2  ; ADS1115
Symbol ADS_SLC = PORTD.3  ; ADS1115


;        76543210
PORTD = %00000000 
TRISB = %00000000
 
Dim Dat15       As Word
Dim Resultaat_0 As Float
Dim Resultaat_1 As Float
 
Dim Data2       As Word
Dim Resultaat_2 As Float
Dim Resultaat_3 As Float
 
Cls
DelayMS 100
 
Lees_ADC:
    While 1 = 1
                                            ;5432109876543210
        I2COut ADS_SDA, ADS_SLC, $90, $01, [%1000000100000011]  ; configuratie
                                            ; |||
                                            ; 100 = AIN0 ingang en met ADDR naar aarde.
                                            ; 101 = AIN1 ingang en met ADDR naar aarde.
                                            ; 110 = AIN2 ingang en met ADDR naar aarde.
                                            ; 111 = AIN3 ingang en met ADDR naar aarde.
        DelayMS 15                         
        I2CIn ADS_SDA, ADS_SLC, $90, $48, [ Dat15 ]  ; uitlezen
        DelayMS 5                   
        If Dat15 > 32767 Then  Resultaat_0 = (65535-Dat15) * 0.0001869:  Resultaat_0=Resultaat_0*-1
        If Dat15 <= 32767 Then Resultaat_0 = Dat15 * 0.0001869                                           
       
        If Dat15 > 32767 Then  Resultaat_1 = (65535-Dat15) * 0.1869:  Resultaat_1=Resultaat_1*-1
        If Dat15 <= 32767 Then Resultaat_1 = Dat15 * 0.1869                                         
               
        'Resultaat_1 = Dat15 * 0.1869
        Print At 1,1, Dec3 Resultaat_0, " Volt"
        Print At 2,1, Dec3 Resultaat_1, " mvolt"
        DelayMS 15
        '                             ;5432109876543210
       ' I2COut ADS_SDA, ADS_SLC, $90, $01, [%1101000100000011]  ; configuratie
                                            ; |||
                                            ; 100 = AIN0 ingang en met ADDR naar aarde.
                                            ; 101 = AIN1 ingang en met ADDR naar aarde.
                                            ; 110 = AIN2 ingang en met ADDR naar aarde.
                                            ; 111 = AIN3 ingang en met ADDR naar aarde.
      '  DelayMS 15                         
      '  I2CIn ADS_SDA, ADS_SLC, $90, $48, [ Data2 ]  ; uitlezen
      '  DelayMS 5                   
      '  Resultaat_2 = Data2 * 0.0001869                                           
     '   Resultaat_3 = Data2 * 0.1869
      '  Print At 1,10, Dec3 Resultaat_2, "V"
      '  Print At 2,10, Dec3 Resultaat_3, "mv"
      '  DelayMS 15                                   
    Wend
 End


But recently bought new ADS1115 does not want to work with it. I have a suspicion that I have ADS1015 instead of ADS1115.

Thanks for the code for ADS1015, I will try it!

Dolci

Quote from: Pepe on Jun 12, 2024, 06:04 PM
Quote from: Dolci on Jun 12, 2024, 10:25 AMHi Pepe,
 do you have a later version of Proteus for the demo file? Mine is V8.15. Thanks

look post #5

thank you Pepe

Pepe

Diferencial mode

Pepe

Quote from: DymDym on Jun 13, 2024, 09:33 AMI once had a working code like this:

' ADS1115
' A0 - "+"; A1- "-"

Device = 18F452
Xtal = 20

@Config_REQ
@__Config Config1H, OSCS_OFF_1 & HS_OSC_1
@__Config Config2L, BOR_OFF_2 & BORV_20_2 & PWRT_ON_2
@__Config Config2H, WDT_OFF_2 & WDTPS_128_2
@__Config Config3H, CCP2MX_OFF_3
@__Config Config4L, STVR_OFF_4 & LVP_OFF_4 & DEBUG_OFF_4

'-----------------------------------------------------------
All_Digital = TRUE
'-----------------------------------------------------------
Declare LCD_Type ALPHA           
Declare LCD_DTPin PORTB.4         
Declare LCD_ENPin PORTB.3         
Declare LCD_RSPin PORTB.2         
Declare LCD_Interface 4 
Declare LCD_CommandUs 2000
Declare LCD_DataUs 50 
Declare LCD_Lines 2 
Clear
 
Symbol ADS_SDA = PORTD.2  ; ADS1115
Symbol ADS_SLC = PORTD.3  ; ADS1115


;        76543210
PORTD = %00000000 
TRISB = %00000000
 
Dim Dat15       As Word
Dim Resultaat_0 As Float
Dim Resultaat_1 As Float
 
Dim Data2       As Word
Dim Resultaat_2 As Float
Dim Resultaat_3 As Float
 
Cls
DelayMS 100
 
Lees_ADC:
    While 1 = 1
                                            ;5432109876543210
        I2COut ADS_SDA, ADS_SLC, $90, $01, [%1000000100000011]  ; configuratie
                                            ; |||
                                            ; 100 = AIN0 ingang en met ADDR naar aarde.
                                            ; 101 = AIN1 ingang en met ADDR naar aarde.
                                            ; 110 = AIN2 ingang en met ADDR naar aarde.
                                            ; 111 = AIN3 ingang en met ADDR naar aarde.
        DelayMS 15                         
        I2CIn ADS_SDA, ADS_SLC, $90, $48, [ Dat15 ]  ; uitlezen
        DelayMS 5                   
        If Dat15 > 32767 Then  Resultaat_0 = (65535-Dat15) * 0.0001869:  Resultaat_0=Resultaat_0*-1
        If Dat15 <= 32767 Then Resultaat_0 = Dat15 * 0.0001869                                           
       
        If Dat15 > 32767 Then  Resultaat_1 = (65535-Dat15) * 0.1869:  Resultaat_1=Resultaat_1*-1
        If Dat15 <= 32767 Then Resultaat_1 = Dat15 * 0.1869                                         
               
        'Resultaat_1 = Dat15 * 0.1869
        Print At 1,1, Dec3 Resultaat_0, " Volt"
        Print At 2,1, Dec3 Resultaat_1, " mvolt"
        DelayMS 15
        '                             ;5432109876543210
       ' I2COut ADS_SDA, ADS_SLC, $90, $01, [%1101000100000011]  ; configuratie
                                            ; |||
                                            ; 100 = AIN0 ingang en met ADDR naar aarde.
                                            ; 101 = AIN1 ingang en met ADDR naar aarde.
                                            ; 110 = AIN2 ingang en met ADDR naar aarde.
                                            ; 111 = AIN3 ingang en met ADDR naar aarde.
      '  DelayMS 15                         
      '  I2CIn ADS_SDA, ADS_SLC, $90, $48, [ Data2 ]  ; uitlezen
      '  DelayMS 5                   
      '  Resultaat_2 = Data2 * 0.0001869                                           
     '   Resultaat_3 = Data2 * 0.1869
      '  Print At 1,10, Dec3 Resultaat_2, "V"
      '  Print At 2,10, Dec3 Resultaat_3, "mv"
      '  DelayMS 15                                   
    Wend
 End


But recently bought new ADS1115 does not want to work with it. I have a suspicion that I have ADS1015 instead of ADS1115.

Thanks for the code for ADS1015, I will try it!

If it is not in differential mode it makes no sense to ask if it is greater than 32767 to convert it to negative because in that mode it only gives positive values, also the $48 is wrong, there goes 0 if it is to read the conversion record or 1 if it is to read or write the configuration register

Yves

Quote from: Pepe on Jun 12, 2024, 11:22 AMProteus v15
Hello all,
 I have a ADS1115 mbreak board module. according to the spread sheet the address is $90 when address is connected to ground. I'm using Pepe code and looking at the oscilloscope getting the ack = 0. what I'm missing is how you change the gain of the internal amplifier by manipulating the register. Is anyone has a full code for it? many thanks in advance.
Cheers
Yves
Yves

Pepe

example with variable gain

flosigud

Les made a very flexible library for ADS1115 some time ago. Isn't that available somewhere on this web?

Yves

Thank you Pepe nice code and it works perfectly.

cheers,

Yves
Yves

Pepe

I'm glad my contribution was useful.

Yves

#15
Quote from: flosigud on Feb 04, 2025, 12:40 PMLes made a very flexible library for ADS1115 some time ago. Isn't that available somewhere on this web?
Hi Flossigud. Any Idea where Les flexible library is? Maybe Les may help. I don't need differential mode as I will have it tied up to ground

Cheers

Yves
Yves

flosigud

It was probably on the old group.

Yves

Quote from: flosigud on Feb 04, 2025, 12:40 PMLes made a very flexible library for ADS1115 some time ago. Isn't that available somewhere on this web?
Hi, I looked in the old files, but it is not there. Maybe someone in the forum, has it? I will appreciate if I could get it. Basically, on the ADS1115 there is the possibility to have two channels as diff and 2 channels as normal(signal to GND). I just can't get it right I did modify the register and we through the pdf again and again the only think I achieved is when I read the  diff AN0-AN1 is an is an inverse response when I change the polarity of the signal. The result variable is signed but it gives me integer only. Please anyone can help in this matter.
Cheers

Yves
Yves

top204

I never created a library for the ADS1115 device, but I did create a simple, 'unfinished', demo code listing for it, many years ago, and it is listed below, if it helps. It has the config values and setups in it, so it should help you re-configure the settings:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Untested code to read an ADS1015 or ADS1115 16-bit ADC.
' Written by Les Johnson for the Positron8 or Positron16 compilers.
'
    Device = 18F25K20                                       ' Tell the compiler what device to compile for
    Declare Xtal = 16                                       ' Tell the compiler what frequency the device is operating at (in MHz)
'
' USART1 declares
'
    Declare Hserial_Baud = 9600                             ' UART1 Baud rate
    Declare Hrsout1_Pin = PORTB.6                           ' Select which pin is to be used for TX with USART1
'
' Declare the Pins used by the Busin/Busout commands
'
    Declare SDA_Pin = PORTB.0                               ' SDA connected to this pin
    Declare SCL_Pin = PORTB.1                               ' SCL connected to this pin
    Declare Slow_Bus = On                                   ' Slow down the interface (if required)
'
' I2C Address/Bits
'
$define cADS_ADDRESS             $48                        ' %1001000 (ADDR = GND)
'
' Conversion Delay (in mS)
'
$define cADS1015_CONVERSIONDELAY 1
$define cADS1115_CONVERSIONDELAY 8
'
' Pointer Register
'
$define cADS_POINTER_MASK        $03
$define cADS_POINTER_CONVERT     $00
$define cADS_POINTER_CONFIG      $01
$define cADS_POINTER_LOWTHRESH   $02
$define cADS_POINTER_HITHRESH    $03
'
' Config Register
'
$define cADS_OS_MASK      $8000
$define cADS_OS_SINGLE    $8000                             ' Write: Set to start a single-conversion
$define cADS_OS_BUSY      $0000                             ' Read: Bit = 0 when conversion is in progress
$define cADS_OS_NOTBUSY   $8000                             ' Read: Bit = 1 when device is not performing a conversion

$define cADS_MUX_MASK     $7000
$define cADS_MUX_DIFF_0_1 $0000                             ' Differential P = AIN0, N = AIN1 (default)
$define cADS_MUX_DIFF_0_3 $1000                             ' Differential P = AIN0, N = AIN3
$define cADS_MUX_DIFF_1_3 $2000                             ' Differential P = AIN1, N = AIN3
$define cADS_MUX_DIFF_2_3 $3000                             ' Differential P = AIN2, N = AIN3
$define cADS_MUX_SINGLE_0 $4000                             ' Single-ended AIN0
$define cADS_MUX_SINGLE_1 $5000                             ' Single-ended AIN1
$define cADS_MUX_SINGLE_2 $6000                             ' Single-ended AIN2
$define cADS_MUX_SINGLE_3 $7000                             ' Single-ended AIN3

$define cADS_PGA_MASK     $0E00
$define cADS_PGA_6_144V   $0000                             ' +/-6.144V range = Gain 2/3
$define cADS_PGA_4_096V   $0200                             ' +/-4.096V range = Gain 1
$define cADS_PGA_2_048V   $0400                             ' +/-2.048V range = Gain 2 (default)
$define cADS_PGA_1_024V   $0600                             ' +/-1.024V range = Gain 4
$define cADS_PGA_0_512V   $0800                             ' +/-0.512V range = Gain 8
$define cADS_PGA_0_256V   $0A00                             ' +/-0.256V range = Gain 16

$define cADS_MODE_MASK    $0100
$define cADS_MODE_CONTIN  $0000                             ' Continuous conversion mode
$define cADS_MODE_SINGLE  $0100                             ' Power-down single-shot mode (default)

$define cADS_DR_MASK      $00E0
$define cADS_DR_128SPS    $0000                             ' 128 samples per second
$define cADS_DR_250SPS    $0020                             ' 250 samples per second
$define cADS_DR_490SPS    $0040                             ' 490 samples per second
$define cADS_DR_920SPS    $0060                             ' 920 samples per second
$define cADS_DR_1600SPS   $0080                             ' 1600 samples per second (default)
$define cADS_DR_2400SPS   $00A0                             ' 2400 samples per second
$define cADS_DR_3300SPS   $00C0                             ' 3300 samples per second

$define cADS_CMODE_MASK   $0010
$define cADS_CMODE_TRAD   $0000                             ' Traditional comparator with hysteresis (default)
$define cADS_CMODE_WINDOW $0010                             ' Window comparator

$define cADS_CPOL_MASK    $0008
$define cADS_CPOL_ACTVLOW $0000                             ' ALERT/RDY pin is low when active (default)
$define cADS_CPOL_ACTVHI  $0008                             ' ALERT/RDY pin is high when active

$define cADS_CLAT_MASK    $0004                             ' Determines if ALERT/RDY pin latches once asserted
$define cADS_CLAT_NONLAT  $0000                             ' Non-latching comparator (default)
$define cADS_CLAT_LATCH   $0004                             ' Latching comparator

$define cADS_CQUE_MASK    $0003
$define cADS_CQUE_1CONV   $0000                             ' Assert ALERT/RDY after one conversions
$define cADS_CQUE_2CONV   $0001                             ' Assert ALERT/RDY after two conversions
$define cADS_CQUE_4CONV   $0002                             ' Assert ALERT/RDY after four conversions
$define cADS_CQUE_NONE    $0003                             ' Disable the comparator and put ALERT/RDY in high state (default)
'
' Gain values
'
$define cGAIN_TWOTHIRDS    cADS_PGA_6_144V
$define cGAIN_ONE          cADS_PGA_4_096V
$define cGAIN_TWO          cADS_PGA_2_048V
$define cGAIN_FOUR         cADS_PGA_1_024V
$define cGAIN_EIGHT        cADS_PGA_0_512V
$define cGAIN_SIXTEEN      cADS_PGA_0_256V
'
' Create global variables here
'
    Dim ADS_bAddress  As Byte
    Dim ADS_bDelay    As Byte
    Dim ADS_bBitShift As Byte
    Dim ADS_wGain     As Word


    Dim MyResult As Word

'-------------------------------------------------------------------------------------------------------------
' The main program loop starts here
'
Main:
    ADS1115_Setup(cADS_ADDRESS)                                     ' Set up for an ADS1115 with a default I2C address of 0

    Do
        MyResult = ADS_ReadADC_SE(0)
        HRSOutLn "Single_Result = ", Dec MyResult
        MyResult = ADS_ReadADC_Diff_0_1()
        HRSOutLn "Mult_0_1_Result = ", SDec MyResult
        MyResult = ADS_ReadADC_Diff_2_3()
        HRSOutLn "Mult_2_3_Result = ", Dec MyResult
        DelayMs 255
    Loop

'---------------------------------------------------------------------------------------
' Write 16-bits to the specified destination register
'
Proc ADS_WriteReg(pI2CAddress As Byte, pReg As Byte, pValue As Word)
    BusOut pI2CAddress, pReg, [pValue.Byte1, pValue.Byte0]
EndProc

'---------------------------------------------------------------------------------------
' Write 16-bits to the specified destination register
'
Proc ADS_ReadReg(pI2CAddress As Byte, pReg As Byte), Word
    BusOut pI2CAddress,[cADS_POINTER_CONVERT]
    BusIn pI2CAddress, pReg, [Result.Byte1, Result.Byte0]
EndProc

'---------------------------------------------------------------------------------------
' Set up for an ADS1015 ADC
'
Proc ADS1015_Setup(pI2CAddress As ADS_bAddress)
    ADS_bDelay = cADS1015_CONVERSIONDELAY
    ADS_bBitShift = 4
    ADS_wGain = cGAIN_TWOTHIRDS      ' +/- 6.144V range (limited to VDD +0.3V max!)
EndProc

'---------------------------------------------------------------------------------------
' Set up for an ADS1115 ADC
'
Proc ADS1115_Setup(pI2CAddress As ADS_bAddress)
    ADS_bDelay = cADS1115_CONVERSIONDELAY
    ADS_bBitShift = 0
    ADS_wGain = cGAIN_TWOTHIRDS      ' +/- 6.144V range (limited to VDD +0.3V max!) *)
EndProc

'---------------------------------------------------------------------------------------
' Sets the gain and input voltage range
'
Proc ADS_SetGain(pGain As ADS_wGain)
EndProc

'---------------------------------------------------------------------------------------
' Get a gain and input voltage range
'
Proc ADS_GetGain(), ADS_wGain
EndProc

'---------------------------------------------------------------------------------------
' Get a single-ended ADC reading from the specified channel
'
Proc ADS_ReadADC_SE(pChannel As Byte), Word
    Dim wConfig As Word
'
' Start with default values
'

    wConfig = cADS_CQUE_NONE | cADS_CLAT_NONLAT | cADS_CPOL_ACTVLOW | cADS_CMODE_TRAD | cADS_DR_1600SPS | cADS_MODE_SINGLE  ' Disable the comparator (default)
                                                                                                                            ' Non-latching (default)
                                                                                                                            ' Alert/Rdy active low (default)
                                                                                                                            ' Traditional comparator (default)
                                                                                                                            ' 1600 samples per second (default)
                                                                                                                            ' Single-shot mode (default)
' Set PGA/voltage range
'
    wConfig = wConfig | ADS_wGain
'
' Set single-ended input channel
'
    If pChannel = 0 Then
        wConfig = wConfig | cADS_MUX_SINGLE_0
    ElseIf pChannel = 1 Then
        wConfig = wConfig | cADS_MUX_SINGLE_1
    ElseIf pChannel = 2 Then
        wConfig = wConfig | cADS_MUX_SINGLE_2
    ElseIf pChannel = 3 Then
        wConfig = wConfig | cADS_MUX_SINGLE_3
    Else
        Result = 0
        ExitProc
    EndIf
'
' Set start single-conversion bit
'
    wConfig = wConfig | cADS_OS_SINGLE
'
' Write wConfig register to the ADC
'
    ADS_WriteReg(ADS_bAddress, cADS_POINTER_CONFIG, wConfig)
'
' Wait for the conversion to complete
'
    DelayMS ADS_bDelay
'
' Read the conversion results
' Shift 12-bit results right 4 bits for the ADS1015
'
    Result = ADS_ReadReg(ADS_bAddress, cADS_POINTER_CONVERT)
    Result = Result >> ADS_bBitShift
EndProc

'---------------------------------------------------------------------------------------
' Read the conversion results, measuring the voltage difference between the P (AIN0) and N (AIN1) input.
' Generates a signed value since the difference can be either positive or negative.
'
Proc ADS_ReadADC_Diff_0_1(), SWord
    Dim wConfig As Word
'
' Start with default values
'
    wConfig = cADS_CQUE_NONE | cADS_CLAT_NONLAT | cADS_CPOL_ACTVLOW | cADS_CMODE_TRAD | cADS_DR_1600SPS | cADS_MODE_SINGLE  ' Disable the comparator (default)
                                                                                                                            ' Non-latching (default)
                                                                                                                            ' Alert/Rdy active low (default)
                                                                                                                            ' Traditional comparator (default)
                                                                                                                            ' 1600 samples per second (default)
                                                                                                                            ' Single-shot mode (default)
' Set PGA/voltage range
'
    wConfig = wConfig | ADS_wGain
'
' Set channels
'
    wConfig = wConfig | cADS_MUX_DIFF_0_1          ' AIN0 = P, AIN1 = N
'
' Set 'start single-conversion' bit
'
    wConfig = wConfig | cADS_OS_SINGLE
'
' Write config register to the ADC
'
    ADS_WriteReg(ADS_bAddress, cADS_POINTER_CONFIG, wConfig)
'
' Wait for the conversion to complete
'
    DelayMS ADS_bDelay
'
' Read the conversion results
'
    Result = ADS_ReadReg(ADS_bAddress, cADS_POINTER_CONVERT)
    Result.Word = Result.Word >> ADS_bBitShift
    If ADS_bBitShift > 0 Then
        '
        ' Shift 12-bit results right 4 bits for the ADS1015, making sure to keep the sign bit intact
        '
        If Result.Word > $07FF Then
            Result.Word = Result.Word | $F000         ' Negative number - extend the sign to 16th bit
        EndIf
    EndIf
EndProc

'---------------------------------------------------------------------------------------
' Read the conversion results, measuring the voltage difference between the P (AIN2) and N (AIN3) input.
' Generates a signed value since the difference can be either positive or negative.
'
Proc ADS_ReadADC_Diff_2_3(), SWord
    Dim wConfig As Word
'
' Start with default values
'
    wConfig = cADS_CQUE_NONE | cADS_CLAT_NONLAT | cADS_CPOL_ACTVLOW | cADS_CMODE_TRAD | cADS_DR_1600SPS | cADS_MODE_SINGLE  ' Disable the comparator (default)
                                                                                                                            ' Non-latching (default)
                                                                                                                            ' Alert/Rdy active low (default)
                                                                                                                            ' Traditional comparator (default)
                                                                                                                            ' 1600 samples per second (default)
                                                                                                                            ' Single-shot mode (default)
' Set PGA/voltage range
'
    wConfig = wConfig | ADS_wGain
'
' Set channels
'
    wConfig = wConfig | cADS_MUX_DIFF_2_3          ' AIN2 = P, AIN3 = N
'
' Set 'start single-conversion' bit
'
    wConfig = wConfig | cADS_OS_SINGLE
'
' Write config register to the ADC
'
    ADS_WriteReg(ADS_bAddress, cADS_POINTER_CONFIG, wConfig)

' Wait for the conversion to complete
    DelayMS ADS_bDelay
'
' Read the conversion results
'
    Result = ADS_ReadReg(ADS_bAddress, cADS_POINTER_CONVERT)
    Result.Word = Result.Word >> ADS_bBitShift
    If ADS_bBitShift > 0 Then
        '
        ' Shift 12-bit results right 4 bits for the ADS1015, making sure to keep the sign bit intact
        '
        If Result.Word > $07FF Then
            Result.Word = Result.Word | $F000             ' Negative number - extend the sign to 16th bit
        EndIf
    EndIf
EndProc

'---------------------------------------------------------------------------------------
' Set up the comparator to operate in basic mode
' Causing the ALERT/RDY pin to assert (go from high to low) when the ADC value exceeds the specified threshold.
' This will also set the ADC in continuous conversion mode
'
Proc ADS_StartCom_SE(pChannel As Byte, pThreshold As SWord)
'
' Start with default values
'
    Dim wConfig As Word
    wConfig = cADS_CQUE_1CONV | cADS_CLAT_LATCH | cADS_CPOL_ACTVLOW | cADS_CMODE_TRAD | cADS_DR_1600SPS | cADS_MODE_CONTIN  ' Comparator enabled and asserts on 1 match
                                                                                                                            ' Latching mode
                                                                                                                            ' Alert/Rdy active low (default)
                                                                                                                            ' Traditional comparator (default)
                                                                                                                            ' 1600 samples per second (default)
                                                                                                                            ' Continuous conversion mode
' Set PGA/voltage range
'
    wConfig = wConfig | ADS_wGain
'
' Set single-ended input channel
'
    If pChannel = 0 Then
        wConfig = wConfig | cADS_MUX_SINGLE_0
    ElseIf pChannel = 1 Then
        wConfig = wConfig | cADS_MUX_SINGLE_1
    ElseIf pChannel = 2 Then
        wConfig = wConfig | cADS_MUX_SINGLE_2
    ElseIf pChannel = 3 Then
        wConfig = wConfig | cADS_MUX_SINGLE_3
    Else
        Result = 0
        ExitProc
    EndIf
'
' Set the high threshold register
' Shift 12-bit results left 4 bits for the ADS1015
'
    ADS_WriteReg(ADS_bAddress, cADS_POINTER_HITHRESH, pThreshold << ADS_bBitShift)
'
' Write config register to the ADC
'
    ADS_WriteReg(ADS_bAddress, cADS_POINTER_CONFIG, wConfig)
EndProc

'---------------------------------------------------------------------------------------
' In order to clear the comparator, we need to read the conversion results.
' This function reads the last conversion results without changing the config value
'
Proc ADS_GetLastConvRes(), SWord
'
' Wait for the conversion to complete
'
    DelayMS ADS_bDelay
'
' Read the conversion results
'
    Result = ADS_ReadReg(ADS_bAddress, cADS_POINTER_CONVERT)
    Result.Word = Result.Word  >> ADS_bBitShift
    If ADS_bBitShift > 0 Then
        '
        ' Shift 12-bit results right 4 bits for the ADS1015, making sure to keep the sign bit intact
        '
        If Result.Word > $07FF Then
            Result.Word = Result.Word | $F000             ' Negative number - extend the sign to 16th bit
        EndIf
    EndIf
EndProc



Pepe

demo proteus