Menu

PIC16F1828 Internal Ref

2017-07-08
2017-07-17
1 2 3 > >> (Page 1 of 3)
  • Daniel Cioba

    Daniel Cioba - 2017-07-08

    Hi , how can i change internal reference between 1V , 2V , 4V when using ADC ?

    CLOSURE NOTE: The methods and scripts in this posting have been replaced by public methods in V0.98.00 or later.

    FVR_Initialize and ADFVR_IsOutputReady are not supported as these have been replaced by FVRInitialize() and FVRIsOutputReady

     

    Last edit: Anobium 2017-10-02
  • kent_twt4

    kent_twt4 - 2017-07-08

    Here some code that might help you. It limits the a-d outputs to AN0 to AN3 and the FVR. It could be expanded if need be. FVR should not be lower than 2.048V when used as an adc reference.

    #define AN0 0
    #define AN1 1
    #define AN2 2
    #define AN3 3
    #define FVR b'00011111'     'define FVR buffer1 output for adc
    ...
    ...
    ...
    Main:
      FixedVoltRef = AD12F1822(FVR)
    ...
    ...
    ...
    goto Main
    
    Function AD12F1822(adport)
    'ADCON1 = b'01100000'   'Left justified, FOSC/64, Vref+ to FVR
    ADCON1 = b'01100000'
    If adport = FVR Then
      'ADCON1 = ADCON1 OR b'01100011'
      FVRCON = b'10000010'    'turn on fixed voltage ref of 2.048V
      FVRNotstable:
      btfss FVRCON,FVRRDY
      goto FVRNotstable
    End if
    ANSELA = adport + 1       'turn off digital input ANSA2
    If adport > 3 Then ANSELA = 0 'internal FVR or Temp Indicator conversion
    rotate adport left simple
    rotate adport left simple
    'bin2ascii adport
    'HSERSend " "
    ADCON0 = ADCON0 OR adport   'adport
    'bin2ascii adport
    'HSERSend " "
    ADON = 1        'ENABLE ADC AND START CONVERSION
    wait 10 us        'use nop's or ??? so no us delay sub????
    'wait 1 ms        'need 200us min for temp module
    'ADGO = 1
    GO_DONE = 1
    ADCPOLL:
    BTFSC ADCON0,GO_DONE    'ADGO
    goto ADCPOLL
    FVRCON = 0
    ADCON0 = 0
    ANSELA = 0        'Shut down adc module
    AD12F1822 = ADRESH    'read out the result
    
    FVRCON = b'00000010'    'turn off fixed voltage ref of 2.048V
    End Function
    
     
  • Daniel Cioba

    Daniel Cioba - 2017-07-09

    Thx , I asked because I thought it was possible somehow to change it simple and still using GC instructions readad10 , etc . Probably not implemented yet .

     
  • Anobium

    Anobium - 2017-07-09

    Let me check. I have a memory on that we have been through this before.

     
  • kent_twt4

    kent_twt4 - 2017-07-09

    Not sure if FVR made it to the library or not. If help is needed to keep things moving, and change the above function to 10bit, let me know.

     
  • Anobium

    Anobium - 2017-07-09

    Look extremly simple to implement in the latest library. I can add in about an hour or so.

    Yes/No?

     
  • Anobium

    Anobium - 2017-07-09

    Here is the latest ADC library. It is from the upcoming release. https://www.dropbox.com/s/k2a8kx6zjpzlem8/a-d.h?dl=0

    This version has a version of FVR included. The use ADC as you do normally but you should check the latest help http://gcbasic.sourceforge.net/help/ so see all the updated ADC functionality. Remember you can really optimise the library and you can optimise the library with just a few #defines.

    The FVR functionality is exposed as:

    ADFVR_Initialize ( ADFVR_OFF | ADFVR_1x | ADFVR_2x| ADFVR_3x )
    ADFVR_IsOutputReady 'returns true or false
    

    Where ADFVR_OFF, ADFVR_1x, ADFVR_2x and ADFVR_4x are defined constants to turn off, v1.024, v2.048 or v4.096 respectively.

    Let us know if this works.

     
  • Daniel Cioba

    Daniel Cioba - 2017-07-09

    THX for the update , i read the help but i cant find so far how to use the updated functionality .
    Just to add in program #define ADFVR_Initialize ( ADFVR_2x ) if i want 2,048V Ref ?

     

    Last edit: Daniel Cioba 2017-07-09
  • Anobium

    Anobium - 2017-07-09

    Of course, a good question.

    These are two methods not Defines.

    The first is a sub routine called ADFVR_Initialize. You can pass one parameter.
    ADFVR_Initialize ()

    The one paramter is one of the following
    ADFVR_OFF, ADFVR_1x, ADFVR_2x or ADFVR_4x ( which relate to OFF, 1.024v, 2.048v or 4.096v)

    The second method is a function. No parameter but the function returns true or false.
    ADFVR_IsOutputReady ' returns true or false

    Typical Usage

    To set to 1.024v

    `ADFVR_Initialize (ADFVR_1x)`
    

    To set to 2.048v

    `ADFVR_Initialize (ADFVR_2x)`
    

    To test if the FVR is ready.

    wait while ADFVR_IsOutputReady = false
    

    To install. Make a backup of your existing library in you lowlevel folder and replace with the new library.

    Let us know how you get on.

     
  • Daniel Cioba

    Daniel Cioba - 2017-07-09

    So far the result is exactly the same as with the old library . ADC of course is working , but Vref is "stuck" at 4,096V.
    Even with , ADFVR_Initialize (ADFVR_OFF) the readings are the same .
    I didn't use the FVR test yet , so i don't know if that is working .

    The hardware is simple , I use the PIC and an LCD display to measure a DC voltage . The program is displaying the ADC value read ( 0 - 1023 ) , so i can easily see if the value is changing.

     
  • Anobium

    Anobium - 2017-07-10

    Another, final go hopefully. I failed to setup the ADC for you.

    My test code - works on test here, hopefully, now works for you.

    #chip 16f18855, 32
    
        #startup InitPPS, 85
    
        Sub InitPPS
            UNLOCKPPS
    
                'Module: EUSART
                RC0PPS = 0x0010    'TX > RC0
                TXPPS = 0x0008    'RC0 > TX (bi-directional)
    
            LOCKPPS
        End Sub
    
        #define USART_BAUD_RATE 19200
        #define USART_TX_BLOCKING
    
    ' ADFVR_Initialize ( ADFVR_OFF | ADFVR_1x | ADFVR_2x| ADFVR_4x )
    ' ADFVR_IsOutputReady returns true or false
    
        ADFVR_Initialize ( ADFVR_1x )
        wait while ADFVR_IsOutputReady = false
    
        ''
        #define LEDD2 PORTA.0
        #define LEDD3 PORTA.1
        #define LEDD4 PORTA.2
        #define LEDD5 PORTA.3
        #define Potentiometer   PORTA.4
    
        Dir     LEDD2 OUT
        Dir     LEDD3 OUT
        Dir     LEDD4 OUT
        Dir     LEDD5 OUT
        DIR   Potentiometer In
    
        HSerPrintCRLF 2
        dim adcval as word
    
        do
    
            Wait 100 ms
            adcval =  ReadAD10 ( AN4 )
            porta = adcval / 64
            HSerPrint adcval
            HSerPrintCRLF
    
        loop
    

    Please get the latest library again from https://www.dropbox.com/s/k2a8kx6zjpzlem8/a-d.h?dl=0

    Anobium

     
  • kent_twt4

    kent_twt4 - 2017-07-10

    The FVR could be setup properly, but it doesn't look like it is #defined as an a-d input in the new library, much like AN0, AN1, etc. (per above 12f1822 example)

    #define FVR b'00011111'     'define FVR buffer1 output for adc
    
    FixedVoltRef = AD12F1822(FVR)
    -i.e.-
    FixedVoltRef = ReadAD (FVR)
    FixedVoltRef = ReadAD10 (FVR)
    

    In this way a constantly changing battery supply voltage (which is the actual Vdd reference for the a-d inputs) can be monitored, and an analogue input be given the proper context. From the earlier example a battery voltage is monitored. Once that is established then the adc inputs can be properly calculated.

    '2.048(V)/BatVolt = AD-FVR/255
    'BatVolt = ((205(V) * 255)/FixedVoltRef(count)) + 26(V) (diode drop)  'promote by 100
      FixedVoltRef = AD12F1822(FVR)
      BatVolt = (52275/FixedVoltRef) + 30   '26
    

    Or, I haven't had my morning cup of coffee yet, and I failed to see the logic already in the library?

    EDIT: Having a coffee now, so Beg your pardon, using the FVR as the a-d reference is correct. But the FVR as an a-d input is a thing too, sorry for the (my) confusion :)

     

    Last edit: kent_twt4 2017-07-10
  • Daniel Cioba

    Daniel Cioba - 2017-07-10

    I tried again but is not working yet . Same as before , I can't change Vref .
    Also , if i use#define ADSpeed HighSpeed the reading is "0" . Medium and low give normal readings , not sure if the ADC speed is actually changing .

     
  • Anobium

    Anobium - 2017-07-10

    Please attach a zip with your source and your asm files

     
  • Daniel Cioba

    Daniel Cioba - 2017-07-10

    OK , here is the very basic code i'm using for testing

     
  • Anobium

    Anobium - 2017-07-10

    I cannot see any obvious - I can improve the code with your type of chip to use one byte less. I will do that once we have it working.

    Can I just check a few things? Does ADC work normally? Remove all the optmisations - All ok? And, does the #define ADSpeed HighSpeed always give 0 but the others give a normal full range of 0 to 1023?

     
  • Daniel Cioba

    Daniel Cioba - 2017-07-10

    I checked again , ADC work normally , the PIC16F1828 is new ( i have many ) .
    With Normal ( I asume this is anyway the default value used in library ) and Low is full range 0 - 4V , with High is allways 0.
    This issue was present from the start , with the original library , but i didn't mentioned it before .

     
  • Anobium

    Anobium - 2017-07-10

    Still trying to figure out the root cause.

    Try this please. I have removed the optimisations. You do not need to define LAT for LCD. This is now handled correctly. But, if I broke the LCD please fix to complete the test.

    #chip 16F1828 , 32
    #config osc=int , pllen=on
    
    dim citire as word
    
    #define ADSpeed lowSpeed
    
    #define lcd_io 4
    #define lcd_no_rw
    #define lcd_enable portc.7
    #define lcd_db4 portc.6
    #define lcd_db5 portc.3
    #define lcd_db6 portc.4
    #define lcd_db7 portc.5
    #define lcd_rs portc.2
    #define lcd_enable portc.7
    
    cls
    
    ADFVR_Initialize ( ADFVR_1x )
    
    do
    
    citire = readad10 (AN1)
    
    locate 1,10
    print citire
    print "   "
    
    loop
    
     
  • Daniel Cioba

    Daniel Cioba - 2017-07-10

    It is the same with no optimisation ... only 4V Vref and "0" reading with HighSpeed .

     
    • Anobium

      Anobium - 2017-07-10

      I need to understand more. Can you draw me a diagram of the connectivity?

       
  • Daniel Cioba

    Daniel Cioba - 2017-07-10

    Ok , the schematic is here ... of course for testing the circuit is simplified , I have one input as IN1 and I can jump over the resistive divider if I want ... not the circuit is the issue . If an ADC input is fixed at 1V , lets say ( aprox 0,8V in my case ) , if you change the refference at 1V will read aprox 1023 , with 2V 512 and with 4V aprox 250 . Very easy to see if refference is changing or not . So I got 175 all the time I change ADFVR_Initialize ... and 0 if i use ADSpeed High
    I worked with ADC in many projects , but with Atmega microcontrollers in Bascom , thats why I prefer something similar with PICs ( BASIC instructions ).

     

    Last edit: Daniel Cioba 2017-07-10
    • Anobium

      Anobium - 2017-07-11

      Still nothing jumping out at me.

      1. I have just requested the same microcontroller from Microchip
      2. Meanwhile. can anyone spot the error I have made? :-)

      Should only take a day or two but vacation period is looming.

       
  • Daniel Cioba

    Daniel Cioba - 2017-07-11

    I will try to look at your code , but i'm not very familiar with it , also i got PIC16F1936 , 1938 with the same reference register , I will check if with this microcontroller works .

     
    • Anobium

      Anobium - 2017-07-11

      What a good idea. I like the approach. Good to hear to the results.

      I am away from real silicon at the moment - I am travelling.

       
  • David Stephenson

    We've had some problems with this before. Hidden away in the datasheet it is stated that the + voltage reference for the ADC cannot be less than 1.8V so the 1.024V ref will not work on the ADC.

     
1 2 3 > >> (Page 1 of 3)

Log in to post a comment.