16F1705 internal Opamp woes


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2005
    Location
    West Australia
    Posts
    116

    Default 16F1705 internal Opamp woes

    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

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: 16F1705 internal Opamp woes

    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.
    are you seeing this voltage at opamp out pin with a meter ?
    your description is not clear whether the opamp is not working or the adc measurement .
    working with opamps with inputs near rail potentials is never easy unless chip is designed to have rail to rail capability
    Warning I'm not a teacher

  3. #3
    Join Date
    Jun 2005
    Location
    West Australia
    Posts
    116


    Did you find this post helpful? Yes | No

    Default Re: 16F1705 internal Opamp woes

    Hi Richard,

    I knew you'd be the first to respond

    I've been monitoring the output (pin 8) throughout the testing and it doesn't go above ground at any stage. Ultimately I want to process the opamp output with AN6 (or another if this isn't possible) and is how I initially breadboarded it, but when nothing worked I tried disabling the ADC module and repeated all tests. Zip, diddly squat, nada.

    The data sheet and some of the App Notes rave about the specs of the internal opamp and, yes, it is Rail to Rail IO.

    I have the pcb space to put in an external opamp (probably use an instrument amp) but don't see why I should and, sooner or later, I will need to use this feature so it's worth pursuing.

    Cheers,
    Bill

  4. #4
    Join Date
    Aug 2011
    Posts
    408


    Did you find this post helpful? Yes | No

    Default Re: 16F1705 internal Opamp woes

    Code:
    OPA1CON = %1100000          ' Opamp 1 Enabled, SP set.
    Isn't that missing a bit?
    Try
    Code:
    OPA1CON = %11000000          ' Opamp 1 Enabled, SP set.
    For OPAMP1 you have
    Code:
    ' 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.
    So, to use these for the opamp:
    - set the TRIS bits for RC0, RC1, and RC2
    - set ANSEL bits for AN4, AN5, and AN6

  5. #5
    Join Date
    Jun 2005
    Location
    West Australia
    Posts
    116


    Did you find this post helpful? Yes | No

    Default Re: 16F1705 internal Opamp woes

    Hi Tumbleweed,

    You are a star, thank you.

    I don't know how I missed that bit, correcting it saw the Opamp spring to life... in fact I tried a bunch of combinations with TRIS and ANSEL and it was difficult to make the Opamp fail to do its job!

    It sounds obvious but to use the Opamp as a buffer one needs to remove any feedback resistors (of course).

    Thanks again, I hope this is of use to others.

    Kind regards,
    Bill

Similar Threads

  1. Current sensor output modification. PIC or opamp or something else?
    By retepsnikrep in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 10th December 2020, 06:22
  2. Weirdness with 16F1705's eeprom and DAC
    By wjsmarine in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 5th August 2020, 07:01
  3. Hserin / out Woes..
    By andybarrett1 in forum Serial
    Replies: 11
    Last Post: - 27th April 2015, 23:57
  4. How can I get a PIC to switch 2.5V DC to my Opamp pin?
    By HankMcSpank in forum Schematics
    Replies: 4
    Last Post: - 9th January 2010, 23:04
  5. Still HSEROUT woes
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 34
    Last Post: - 11th July 2006, 22:13

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts