Battery monitoring - ever tried AN1072? - Page 2


Closed Thread
Page 2 of 2 FirstFirst 12
Results 41 to 57 of 57
  1. #41
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,810


    Did you find this post helpful? Yes | No

    Default Re: Battery monitoring - ever tried AN1072?

    OK, now I see how this works. Sorry I did not gave the proper attention to the AN1072.

    It works in reverse. It does supply a 0,6 volts to the ADC input and the refererence is read from the Vdd supply.

    Unfortunately this is NOT what Roger wants. He has a stable Vdd from his LDO regulator so his ADRES result will always be the same, no matter the battery condition.

    Clever technique but not the one needed here...

    Ioannis

  2. #42
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default Re: Battery monitoring - ever tried AN1072?

    I'll need to download the latest DS, mine is from 2007

    George, would you have a little piece of PICBASIC code about how to use the 0,6V reference please?

    As a matter of fact, my AN1072 is from 2006 and there is no code example in there. And even if there would be some, it wouldn't be in PICBASIC anyway.
    Roger

  3. #43
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default Re: Battery monitoring - ever tried AN1072?

    Thanks Ioannis. I just replied while you were sending your reply.

    The first lines of the AN1072 say:

    INTRODUCTION
    This application note describes how to measure the voltage supplied to a PIC ® microcontroller, VDD . The device used in preparation of this application note was the PIC16F690. The ability to measure VDD lends itself to battery applications where VDD is likely to fall over time. In this application note, an example program is provided with routines to measure VDD.

    THEORY OF OPERATION
    Select Microchip PIC microcontrollers contain a 0.6V or 1.2V internal reference that is selectable as an input to
    the ADC module
    . This provides a fixed reference to allow measurement of VDD . The PIC16F690 was chosen for this feature.



    A:- I can't still find a code example in this AN.
    B:- Can this AN be wrong?

    Right; on MICROCHIP's website, I could find two ASM code examples....but I don't know machine language or I wouldn't probably be on this forum

    But here they are (ZIPfile) so if somebody want's to have a look and "translate" the essential stuff in PICBASIC, it would be great.

    In case I can't really use the 0,6 reference voltage, I plan to make my circuit looking like this:
    Name:  2015-09-27 18_38_05-1 .png
Views: 318
Size:  37.0 KB



    Attachment 8058
    Last edited by flotulopex; - 27th September 2015 at 17:42. Reason: added stuff
    Roger

  4. #44
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,810


    Did you find this post helpful? Yes | No

    Default Re: Battery monitoring - ever tried AN1072?

    Roger, for this to work you need to get rid of the LDO and feed a 4,5Volt battery pack to the PIC. That way you will measure the Vdd voltage in respect with the VP6 input to the ADC. There is no other way to do it.

    Your configuration would be:

    Code:
    ADCON0=%10110101   ' Here we drive the 0,6 volts to the ADC
    ADCON1=%00110000   ' I prefer to be sure to have FRC for the beginning at least
    VRCON= %00010000   ' Enable the VP6 reference
    Now if you want to measure the Vdd, just do this:

    Code:
    ADCON0.1=1
    while ADCON0.1: wend
    Bat_Value.Byte1=ADRESH:Bat_Value.Byte0=ADRESL
    But remember, you have to drop the LDO and supply your circuit from the battery directly.

    Ioannis
    Last edited by Ioannis; - 27th September 2015 at 17:42.

  5. #45
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default Re: Battery monitoring - ever tried AN1072?

    Quote Originally Posted by Ioannis View Post
    ...you need to get rid of the LDO and feed a 4,5Volt battery pack to the PIC. That way you will measure the Vdd voltage in respect with the VP6 input to the ADC.
    Thanks Ioannis; I now understand what you mean.

    This morning, I tried to manually setup the ADC but it was always "failing" (= giving same value all time); you just explained why it didn't work

    I found an example on which I based my test code here.

    I'll give your example a try
    Roger

  6. #46
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default Re: Battery monitoring - ever tried AN1072?

    ...and here are the two code example files I was talking about in my post#43 ;-)

    AN1072src.zip
    Roger

  7. #47


    Did you find this post helpful? Yes | No

    Default Re: Battery monitoring - ever tried AN1072?

    There is a post by Darrel re this but I can't find it. I don't have any PICBasic code to hand but this in Proton might help -

    Code:
      ADCON1 = %00100000                  ' ADCS=010 Fosc/32 4usec
      Symbol Go_Done = ADCON0.1
      Symbol ADON = ADCON0.0
      Symbol VP6EN = VRCON.4              ' 0.6V Reference Enable Bit
    
      Dim Ad_Result As ADRESL.Word        ' Convert the ADRESL register into a WORD variable
      Dim VDD       As Word
    
    
      VP6EN = 1                           ' Turn 0.6V reference ON
      ADCON0 = %10110101                  ' right justify, Vdd, select 0.6v ref, ADC on   
      DelayUS 100                         ' Wait for sample/hold capacitors to charge and VP6 to settle
      Go_Done = 1                         ' Start conversion
      While Go_Done = 1 : Wend            ' Poll the GO_DONE flag for completion of conversion
      ADCON0 = %10110100                  ' right justify, Vdd, Select 0.6v ref, ADC off   
      VP6EN = 0                           ' Turn 0.6V reference OFF
      VDD = 6144/Ad_Result                ' convert input reading to VDD voltage *VDD must be a Word
                                          ' 0.6 * 1024 = 614.4 - 6144 To get 1/10th
    George

  8. #48
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Thumbs up It works!!!!

    Finally YOU made it!!!

    I just changed my circuit to power it directly from my variable power-supply and simulate the batterie's weakening.

    @5VDC, my display shows a value of 135 and @4,2VDC (can't go lower or my display won't work anymore) I read 147.

    So, except the particular init sequence for the LCD I use, the code example here under is working fine and should be quite standard.

    Code:
    ' PIC 16F690 Fuses
    @ __config _FCMEN_OFF &_IESO_OFF &_CPD_OFF &_WDT_OFF &_HS_OSC &_BOR_OFF &_CP_OFF &_PWRTE_OFF &_MCLRE_OFF
    
    '-------------------------------------------------------------------------------
    ' Registers   76543210
    OPTION_REG = %10000000 'OPTION register
    ANSEL      = %00000000 'Select analog inputs Channels 0 to 7
    ANSELH     = %00000000 'Select analog inputs Channels 8 to 11
    WPUA       = %00000000 'Select weak pull-ups
    WPUB       = %00000000 'Select weak pull-ups
    ADCON0     = %00000000 'AD Module
    ADCON1     = %00000000 'AD control register
    CM1CON0    = %00000000 'Comparator1 Module
    CM2CON0    = %00000000 'Comparator2 Module
    INTCON     = %00000000 'INTerrupts CONtrol
    TRISA      = %00000000 'Select Input/Output (0 to 5)
    PORTA      = %00000000 'Set High/Low (0 to 5)
    TRISB      = %00000000 'Select Input/Output (4 to 7)
    PORTB      = %00000000 'Set High/Low (4 to 7)
    TRISC      = %00000000 'Select Input/Output (0 to 7)
    PORTC      = %00000000 'Set High/Low (0 to 7)
    
    '-------------------------------------------------------------------------------
    ' Defines
    DEFINE OSC 8
    
    DEFINE LCD_DREG PORTC  'LCD data port 
    DEFINE LCD_DBIT 4      'LCD data starting PORT.bit (0 or 4)
    DEFINE LCD_RSREG PORTC 'LCD register select port 
    DEFINE LCD_RSBIT 3     'LCD register select bit 
    DEFINE LCD_EREG PORTC  'LCD enable port 
    DEFINE LCD_EBIT 2      'LCD enable bit 
    DEFINE LCD_BITS 4      'LCD bus size 4 or 8
    
    DEFINE ADC_BITS 10     'Number of bits in ADCIN result
    
    '-------------------------------------------------------------------------------
    ' Init display
    
    ' ELECTRONIC ASSEMBLY DOGM081 LCD display Mandatory settings
    '  See datasheet for circuitry changes by 5V or 3,3V operation
    PAUSE 1000      'Time to settle Vdd (THIS IS CRUCIAL FOR THIS DISPLAY!!!)
    LCDOUT $FE, $29 'Function Set: 4 bits bus mode
    LCDOUT $FE, $1C 'Bias set
    LCDOUT $FE, $52 'Power control + Contrast (HiByte)(for 5V=$52 or 3,3V=55)
    LCDOUT $FE, $69 'Follower control (5V=$69/3,3V=6D)
    LCDOUT $FE, $78 'Contrast (LowByte)
    
    '-------------------------------------------------------------------------------
    ' Variables
    Bat_Value   var word 'holds ADC value
    Bat_Value   = 0
    
    '-------------------------------------------------------------------------------
    ' Start program
    
    MAIN:
        ADCON0=%10110101   ' Here we drive the 0,6 volts to the ADC
        'ADCON1=%00110000   ' I prefer to be sure to have FRC for the beginning at least
        VRCON= %00010000   ' Enable the VP6 reference
        ADCON0.1=1
        WHILE ADCON0.1 : WEND
        Bat_Value.HIGHBYTE = ADRESH : Bat_Value.LOWBYTE = ADRESL
        LCDOUT $FE,2,DEC4 Bat_Value
        PAUSE 1000
    GOTO MAIN
    
    END
    Using the internal 0,6V reference:
    - 0,8Vbatt difference = 12 value change
    + no external components

    Using an external voltage divider:
    + 0,8Vbatt difference = 84 value change
    - external components


    Thanks a lot for all your help
    Roger

  9. #49
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default Adcin 13

    ...and while testing things, here is something I just tried based on the code in my previous post using the ADCIN command:
    Code:
    ' Start program
    
    MAIN:
        ADCON1 = %00110000   ' I prefer to be sure to have FRC for the beginning at least
        VRCON  = %00010000   ' Enable the VP6 reference
        ADCIN 13, Bat_Value  ' ADCON0 = %10110100 where bits 5:2 are the 0,6V reference's channel address (1101 = 13)
        LCDOUT $FE,2,DEC5 Bat_Value
        PAUSE 1000
    GOTO MAIN
    
    END
    The results are @5VDC I read 8640 and 9790 @4,2VDC (?!) but it looks to work somehow.
    Roger

  10. #50


    Did you find this post helpful? Yes | No

    Default Re: Adcin 13

    The ADC is 10 bit, the largest value you can read is 1023 (10 1's). BTW check to table in the ADC section re. recommended ADC clock source, at 8 Mhz use Fosc/16 or Fosc/32. See note re using FRC as a clock source above 1Mhz.

    You seem to have missed out the conversion to 0v1.

    George

  11. #51
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,810


    Did you find this post helpful? Yes | No

    Default Re: Adcin 13

    Are you sure that you still have right justification? Maybe you have accidentaly set the ADCON.7 to zero?

    In general the bigger number for lower voltage is correct. Since you have the same 0,6 volts at the ADC input, lowering the Vref (through Vdd) will give bigger ADC result.

    But not more than 1023 as George stated.

    Ioannis

  12. #52
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default "Right Justifying" is the key

    Thanks Ioannis and George,

    Here is the corected and 100% working code example using ADCIN:
    Code:
    ' PIC 16F690 Fuses
    @ __config _FCMEN_OFF &_IESO_OFF &_CPD_OFF &_WDT_OFF &_HS_OSC &_BOR_OFF &_CP_OFF &_PWRTE_OFF &_MCLRE_OFF
    
    '-------------------------------------------------------------------------------
    ' Registers   76543210
    OPTION_REG = %10000000 'OPTION register
    ANSEL      = %00000000 'Select analog inputs Channels 0 to 7
    ANSELH     = %00000000 'Select analog inputs Channels 8 to 11
    WPUA       = %00000000 'Select weak pull-ups
    WPUB       = %00000000 'Select weak pull-ups
    ADCON0     = %00000000 'AD Module
    ADCON1     = %00000000 'AD control register
    CM1CON0    = %00000000 'Comparator1 Module
    CM2CON0    = %00000000 'Comparator2 Module
    INTCON     = %00000000 'INTerrupts CONtrol
    TRISA      = %00000000 'Select Input/Output (0 to 5)
    PORTA      = %00000000 'Set High/Low (0 to 5)
    TRISB      = %00000000 'Select Input/Output (4 to 7)
    PORTB      = %00000000 'Set High/Low (4 to 7)
    TRISC      = %00000000 'Select Input/Output (0 to 7)
    PORTC      = %00000000 'Set High/Low (0 to 7)
    
    '-------------------------------------------------------------------------------
    ' Defines
    DEFINE OSC 8
    
    DEFINE LCD_DREG PORTC  'LCD data port 
    DEFINE LCD_DBIT 4      'LCD data starting PORT.bit (0 or 4)
    DEFINE LCD_RSREG PORTC 'LCD register select port 
    DEFINE LCD_RSBIT 3     'LCD register select bit 
    DEFINE LCD_EREG PORTC  'LCD enable port 
    DEFINE LCD_EBIT 2      'LCD enable bit 
    DEFINE LCD_BITS 4      'LCD bus size 4 or 8
    
    DEFINE ADC_BITS 10     'Number of bits in ADCIN result
    
    '-------------------------------------------------------------------------------
    ' Init display
    
    ' ELECTRONIC ASSEMBLY DOGM081 LCD display Mandatory settings
    '  See datasheet for circuitry changes by 5V or 3,3V operation
    PAUSE 1000      'Time to settle Vdd (THIS IS CRUCIAL FOR THIS DISPLAY!!!)
    LCDOUT $FE, $29 'Function Set: 4 bits bus mode
    LCDOUT $FE, $1C 'Bias set
    LCDOUT $FE, $52 'Power control + Contrast (HiByte)(for 5V=$52 or 3,3V=55)
    LCDOUT $FE, $69 'Follower control (5V=$69/3,3V=6D)
    LCDOUT $FE, $78 'Contrast (LowByte)
    
    '-------------------------------------------------------------------------------
    ' Variables
    Bat_Value   var word 'holds ADC value
    Bat_Value   = 0
    
    '-------------------------------------------------------------------------------
    ' Start program
    
    MAIN:
        ADCON0 = %10000000   ' Here we drive the 0,6 volts to the ADC
        VRCON  = %00010000   ' Enable the VP6 reference
        ADCIN 13, Bat_Value  ' Select the VP6 channel's address ADCON0 = %xx1101xx
        Bat_Value.HIGHBYTE = ADRESH : Bat_Value.LOWBYTE = ADRESL
        LCDOUT $FE,2,DEC4 Bat_Value
        PAUSE 1000
    GOTO MAIN
    
    END
    Roger

  13. #53


    Did you find this post helpful? Yes | No

    Default Re: "Right Justifying" is the key

    Did you see how to convert he ADC count in Bat_Value to volts?

    I see you are still not using a recommended value for ADCS in ADCON1.

    George

  14. #54
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default the maths and the proof the system works

    Yes George,

    For any reason, either ADCON1.ADCS setting will affect the results.

    For those interested in the maths (where "2n" is 1024 because I use 10bits in ADC):
    Name:  2015-09-30 21_43_57-Clipboard.png
Views: 308
Size:  25.0 KB

    Regarding my results and comparing them to the table A-1 (Appendix A: AC Result Table in AN1072), something must be wrong.

    A 135 ADC result corresponds to 4,52V and 147 to 4,16V (?!).

    Grrrrrr! I didn't notice I hadn't removed the voltage regulator on my breadboard. My PICkit2 programmer delivers 4,78VDC (even if it shows 5V in the "VDD PICkit2" field) so the "new" ADC values I get now seem to be much closer to what they are supposed to be "on the paper".

    For 4,82V displayed on my FLUKE87, the ADC result is now 127 (exact as table A-1) and @4,31V I get 142 (exact as table A-1).

    ((1024 - 1) / 4,82) * 0,6 = 127 and ((1024 - 1) / 4,31) * 0,6 = 142
    Roger

  15. #55
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,810


    Did you find this post helpful? Yes | No

    Default Re: the maths and the proof the system works

    Glad you did it Roger! Bravo!

    Ioannis

  16. #56


    Did you find this post helpful? Yes | No

    Default Re: the maths and the proof the system works

    For any reason, either ADCON1.ADCS setting will affect the results.
    I don't know the internals of ADCIN, but unless it sets the ADCS bits to something else, you're ADC clock source will be Fosc/2 which at 8Mhz will give a Tad of 250ns. Fosc/16 or Fosc/32 are recommended.

    George

  17. #57
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,810


    Did you find this post helpful? Yes | No

    Default Re: the maths and the proof the system works

    George, If you do not want any headaches, just use the Frc. The internal 500Khz clock is good enough for most applications.

    Plus, you do not worry for any timing violations.

    Ioannis

Similar Threads

  1. Battery voltage monitoring for use on 16F1825
    By longpole001 in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 23rd April 2013, 14:57
  2. Replies: 13
    Last Post: - 22nd January 2012, 04:48
  3. ADCIN car battery monitoring
    By mitchf14 in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 29th June 2008, 08:04
  4. 12v car battery monitoring
    By oldcarguy85 in forum Schematics
    Replies: 2
    Last Post: - 10th December 2007, 00:26
  5. Using the A/D for Monitoring a Solor Cell and Battery
    By chuck.sieveking in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 13th July 2004, 19:27

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