Hi All,

The title says it all. I'm trying to utilize the PIC's internal opamp rather than a separate opamp to measure load current on AN6 but I cannot get it to work. I've RTFM so many times I think I could quote it, but still not working.

The device compiles and runs correctly as evidenced by normal serial display with some A/D values on a different channel but zero for Opamp1. If someone has an example of code with working internal opamp or can breadboard and test the setup I'd appreciate the help.

Any comments or suggestions are welcome. Worse case I'll use an external opamp - but don't want to.

Cheers,
Bill

Code:
'****************************************************************************************************
'*  Name    : Opamp test.pbp                                                                        *
'*  Date    : 12Apr21                                                                               *
'*  Device  : 16F1705                                                                               *
'*  Notes 1 : Cannot see any output on pin 8 for PIC internal opamp1. Connecting in unity gain mode *    
'*          : (as a buffer) makes no difference.                                                    *
'*          :                                                                                       *
'*          : Testing with 200mV injected into pin 10 (non-inv input of Opamp1, Gain of 10) should  *
'*          : see 2V at pin 8 output.  When set as buffer should see 200mV.  Both tests show 0V.    *
'*          :                                                                                       *
'****************************************************************************************************
'
' Load current is measured and sent serially for monitoring.
'
' NOTE the PIC's internal Opamp 1 monitors volt drop across a 0R2 resistor in the ground return path from the load with
' pin 10(+) connecting at the return point.  External feedback resistor (RF, 9k1) sits between pins 8(out) and Pin 9(-)   
' while Gain resistor (RG, 1k) connects Pin 9 to Gnd to achieve a gain of 1+(R1/R2) = 10.1 or 1 amp producing 2.02 volts
' for a maximum load current of 2.47 amps (when it reaches Vdd of 5v).  Output pin 8 is internally connecting to An6.
'
'=======================================================================================================================
'        CONFIGURE DEVICE
'=======================================================================================================================
#CONFIG ; 16F1705
    __config _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _BOREN_ON & _CLKOUTEN_OFF
    __config _CONFIG2, _WRT_OFF & _PPS1WAY_OFF & _ZCDDIS_ON & _PLLEN_OFF & _STVREN_ON & _BORV_LO & _LPBOR_OFF & _LVP_OFF	   
#ENDCONFIG

' Connections as follows:
'     ****16F1705 PIC**** Comments
'
' Vdd          (pin 1)    5 volts.      
'         RA5  (pin 2)    T1clk.                Spare.       
'         RA4  (pin 3)	  AN3, T1G.             Spare.
' MCLR    RA3  (pin 4)    IOC.  Pull High 10k.  Spare.
'         RC5  (pin 5)	  Opamp2in+, CCP1.      Serout via 1k.
'         RC4  (pin 6)    Opamp2in-.            Serin via 22k.
'         RC3  (pin 7)    AN7, Opamp2Out, CCP2. Spare.      
'         RC2  (pin 8)    AN6, Opamp1Out.       ### output - see notes.
'         RC1  (pin 9)    AN5, Opamp1in-.       Junction of RF and RG.
'         RC0  (pin 10)   AN4, Opamp1in+.       + sense of 0R1 series load resistor.
'         RA2  (pin 11)   AN2, DAC1out2.        Battery voltage divider 3:1 ratio.
' ICSPclk RA1  (pin 12)   AN1, Vref+.           ICSPclk
' ICSPdat RA0  (pin 13)   AN0, DAC1out1.        ICSPdat
' Vss  Ground  (pin 14)   

'=======================================================================================================================
'        PIN ASSIGNMENTS, SYSTEM CONSTANTS, TEMPORARY VARIABLES
'=======================================================================================================================
' Variables
AD_res	var	word			' Variable to hold 10-bit AD result. 
accum	var	word			' For accumulation of average.
x       var byte            ' Counter for loops.
BV      var word            ' Converted counts for Battery Voltage.
LC      var word            ' Converted counts for Load Current in mA.

    Include "MODEDEFS.BAS"      ' Include Shiftin/out modes.
	
    DEFINE DEBUG_REG PORTC      ' Debug pin port.
    DEFINE DEBUG_BIT 5          ' Debug pin.
    DEFINE DEBUG_BAUD 9600      ' Debug baud rate
    DEFINE DEBUG_MODE 1         ' Debug mode: 0 = True, 1 = Inverted
'    DEFINE DEBUG_PACING 1000    ' Debug character pacing in us
    DEFINE DEBUGIN_BIT 4        ' Input pin.

    DEFINE  OSC 4               ' Adjust to suit design.
    OSCCON  = %01101011         ' Internal 4MHz osc.
'    OSCCON  = %01110011         ' Internal 8MHz osc.
'    OSCCON  = %01111011         ' Internal 16MHz osc.
'    OSCCON  = %11110011         ' Internal 32MHz osc PLL.

    OPTION_REG.7 = 1            ' Disable weak pullups.
'    OPTION_REG.7 = 0            ' Enable weak pullups.

    DEFINE ADC_BITS 10          ' Set number of bits in result.
    DEFINE ADC_CLOCK 3          ' Set clock source (rc = 3).
    DEFINE ADC_SAMPLEUS 50      ' Set sampling time in microseconds. 

'    ADCON0 = 0                  ' No ADC.
'    ANSELA = 0                  ' Disable ADC.
    ADCON0 = %00000001          ' Enable ADC.
    ADCON1 = %10000000          ' Right justify, Frc, use Vdd for Vref.
    ADCON2 = %00000000          ' No Trigger selects.
    ANSELA = %00000100          ' AN2 the rest Dig.
    ANSELC = %00000111          ' AN0,1,2 the rest Dig.

    CM1CON0 = 0                 ' Comparators off.

    OPA1CON = %1100000          ' Opamp 1 Enabled, SP set.
'    OPA1CON = %1110000          ' Opamp 1 Enabled, SP set, unity gain.
    OPA2CON = %0000000          ' Opamp 2 Disabled.
'    OPA2CON = %1100000          ' Opamp 2 Enabled, SP set.

    TRISA = %111111             ' 
    TRISC = %011011             ' 

    FVRCON = %0                 ' Disabled.
'    FVRCON = %11000101          ' Enabled, Vref 1.024V (4mV/bit).
'    FVRCON = %11001001          ' Enabled, Vref 2.048V (8mV/bit).
'    FVRCON = %11001101          ' Enabled, Vref 4.096V (16mV/bit).

'    DAC1CON0 = %10101000        ' Vref from FVR, DAC1out1 (RA0).
'    DAC1CON0 = %10011000        ' Vref from FVR, DAC1out2 (RA2).
'    DAC1CON1 = %10000000        ' Set initial output value to 50% of Vref.
'    DAC1CON1 = %00000000        ' Set initial output value to 0.
'    DAC1CON1 = %10011100        ' Set initial output value (156*4) to 0.624V (Vref 1.024V).
'    DAC1CON1 = %10011100        ' Set initial output value (156*16) to 2.496V (Vref 4.096V).

    latC.5 = 0                  ' Else first serial chars are garbled... Inverted.

    pause 1000                  ' Let everything settle...
    GOTO First                  ' Jump over the subroutines area.

'=========================================================================================================
' Subroutines here in the first page...
'=========================================================================================================
GetBv:
    accum=0                     ' Reset the count.
    for x=0 to 9                ' Ten conversions.
    adcin 2,AD_res              ' 10 bit count 0-1024 for 0-20 volts.
    accum=AD_res+accum			' Add each sample.
    next						' Loop back again.
    AD_res=accum/10				' Get average, result is counts.
    Bv=(AD_res*/509)            ' Battery voltage units.
    debug "BV count is ",dec AD_res,", ",dec2 Bv/100,".",dec2 Bv,"V",10,13   ' For monitoring.
    return

GetLC:
' 0R2 load current sense resistor is in the ground return path, 1000mA load will drop 200mV across it. Gain of
' the opamp is 10.1 so this becomes 2.02 volts or 5029/2.02 = 2489mA max load sensing.
    accum=0                     ' Reset the count.
    for x=0 to 9                ' Ten conversions.
    adcin 6,AD_res              ' 10 bit count 0-1024 for 0-2500 mA.
    accum=AD_res+accum			' Add each sample.
    next						' Loop back again.
    AD_res=accum/10				' Get average, result is counts.
    LC=(AD_res*/62)*10          ' Load Current in mA.
    debug "LC count is ",dec AD_res,", ",dec4 LC,"mA",10,13,10   ' For monitoring.
    return

'=====================================================================================================
' Main programme starts here...
'=====================================================================================================
First:  ' Operation when system first powered up. 
    gosub GetBv                 ' 
    gosub GetLC                 ' 
    Pause 1000                  ' 
    goto First                  ' Cycle.

end