Battery monitoring - ever tried AN1072?


Closed Thread
Results 1 to 40 of 57

Hybrid View

  1. #1


    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

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,134


    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

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


    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

  4. #4


    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

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


    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: 386
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

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,134


    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

  7. #7


    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

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 : 0

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