Menu

Reading the value of FVR using ADC

Help
2019-05-24
2019-05-31
  • David Stephenson

    Is there a way of reading (16F18426) the fixed voltage reference(FVR) like READad12(FVR).
    I know I could do it by setting the register ADCON0.
    In case you're wondering why, it's to check the battery voltage level. Wth Vref+ set to Vdd and reading the FVR should give an indication of the voltage level (and charge left).

     
  • kent_twt4

    kent_twt4 - 2019-05-24

    I know what you mean, but don't see how you get away with that yet. For readability I have used a define constant in this situation for a 16f1827 like:

    #define FVR = 31  'there is the ADCON0 part
    ...
    ...
    Vref = ReadAD(FVR)
    
     
  • David Stephenson

    I've got something to work I switched to a 16F18326 as the ADC is a little easier to understand (and I don't need 12-bits).
    I am a little confused by the datasheet. It suggested to check the FVRRDY of the fvrcon register to make sure that the voltage reference has stabilised yet further on it states that this is always "1" (see attachment). Can anyboby enlighten me as to whether this bit can be used to determine if the FVR is ready.

     
  • kent_twt4

    kent_twt4 - 2019-05-30

    I don't think you will find a difference. Put a toggle led at the end of the adc conversion, and the adc conversion itself in an endless loop. Test both ways with DVM Hz. You shouldn't see more than about a 4 instruction cycle difference for the FVR ready instructions. Both methods should give valid readings from my experience.

     
  • David Stephenson

    Below is the code. This gives a reading of the battery voltage (higher reading = lower voltage).
    I am using a LCD display that needs to have the contrast adjusted as the voltage falls. Good batteries LDC background is black and low battery it is difficult to read.
    Another question:
    The datasheet uses GOTO $-1 to jump back one instruction yet using this is generally considered "bad form" anybody have a strong view? Should I use GOTO linelabel instead I suppose BRA $-1 would have also worked.

    SUB BATLEVEL
    'FVRCON=b'00000001' '1.024 V
    FVRCON=b'00000010' '2.048 V
    ADCON1=b'11000000' 'SET REFS AND Fadc WITH Fosc=4MHz
    BAT2=0
    BAT1=0
    BSF FVRCON,FVREN 'ENABLE FVR
    NOP
    BTFSS FVRCON,FVRRDY 'wait for FVR to stabilise
    GOTO $-1
    ADCON0=b'11111100' 'SET adc to read FVR
    REPEAT 64
    BSF ADCON0,ADON 'adc on
    WAIT 20 US 'AQUISITION DELAY
    BSF ADCON0,ADGO 'start conversion
    NOP
    BTFSC ADCON0,ADGO 'check for coversion complete
    GOTO $-1
    BCF ADCON0,ADON 'turn off adc
    BAT1=ADRESL
    BAT1_H=ADRESH
    BAT2=BAT2+BAT1
    END REPEAT
    BCF FVRCON,FVREN 'DISABLE FVR
    END SUB
    
     
  • mmotte

    mmotte - 2019-05-31

    David,
    This has nothing about FVR but it is the way i checked battery condition in one project. I made a voltage divider between battery+ and Gnd out of two 10K resistors and sent the middle to ADC0.

    Sub LED_BLINK_BAT
              batValue = ReadAD(AN0)  ' 5.79 bat volt = 168
                                        '5.2 v bat = 151  new bat =6.4 =184
                                        '% useful bat = (adc-151)/3
              If batValue > 181 then batValue = 181
              If batValue < 154 then batValue = 154
    
              If batValue >= 160 then
              percent = (batValue -151)/3
              FlashAMBER(percent)
              end if
              If batValue < 160 then
              percent = (batValue -151)/3
              FlashGRN(percent)
              end if
    
    end sub
    
    sub FlashGRN( times)
        Repeat times
        PulseOut GRNLED, 150 ms
        Wait 400 ms
        End Repeat
    end sub
    

    The processor runs two diodes below the battery because I needed the 6vdc for the servos. I mention this because of the numbers being used and the adc using the the full processor voltage for top of scale which moves as the battery wears down.
    This is a rough estimate of amount % of battery left and is working fine in my project.

    Best regards
    Mike

     
  • David Stephenson

    Not sure how that works don't the diodes just drop a fixed amount and your voltage divider halves the ~6vdc.
    The problem is (as usual) I am running out of pins so I wanted a solution that did not use up an extra pin.
    I've taken to using microchip's LDO voltage regulators (MCP1700) to drop voltage when it is required.

     
  • kent_twt4

    kent_twt4 - 2019-05-31

    Just a thought on the lcd contrast problem, you could consider a small buck/boost module to maintain the 5V required for the character lcd? Would need a shutdown mechanism or under voltage protection.

     

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.