Battery voltage monitoring for use on 16F1825


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: Battery voltage monitoring for use on 16F1825

    thanks guys , i found that the defines of DEFINE ADC_BITS 10 , DEFINE ADC_SAMPLEUS 10 were not inplace as such the acdin would not work , i did try the direct register approch and this also worked with bit ' ADCON0.1 = 1 'start the conversion set

    In the end due to the voltage range of 3.8v -9v Vsupply input ,the voltage divider resistor values i had chosen and using the Vref internal of the chip of 2v off the Vref module i found that at the highest voltage the ACD reading was higher that 1024 , so decided to use VDD as Vref in the ADCON1 register ( as regularor is inplace on this cct)

    Also found that using the FRC internal allowed the ACD reading to allow for the voltage range using the existing Voltage divider resistor values ,i am still usure of how to work out the calculations for what the expected reading should be , but in practice i measured the voltage and did debug of the values by writing them to eeprom at the high and low points when voltage was applied

    so here is the working code for those like me that find its best to post code results as those that read this forum a lot gain lots .

    I also note i included DT's Average subroutine to do an accumulative average of samples

    next PWM how to set that up correctly for what i want as adding the ACD routines has screwed the regular timing of the simple pulse output code i have

    Cheers

    Sheldon

    Code:
      DEFINE ADC_BITS 10        ' Number of bits in ADCIN result - Required for adcin command 
      DEFINE ADC_SAMPLEUS 10    ' ADC sampling time in microseconds - Required for adcin command 
    
    ' ------------ADC routines and Varaiables ----------------
      ADval    VAR WORD      ' Value of raw ACD voltage read 
      
      AvgCount	CON  10		' Number of samples to average in Average routine
      FAspread	CON  200	' Fast Average threshold +/-  in Average routine
      ADavg		VAR  WORD   ' Accumulated Varable of Readings in Average routine
      Value		VAR  WORD   ' Temp varable assigned in Average routine
    
    
    
    
    ' ---------- Gets a Voltage reading on Port1.0 --------- 
     
     ' Note:  - Application circuit has a 3.3v voltage reg fitted so  use VDD as Vref in ASCON1 register
     '        - have used the ADC Fixed Voltage Ref Modual in 16F1825.
     '        - PortA.0 ia also used as Digital Output of Power LED active low via Pch - Mosfet
     '        - Supply Voltage input range is 9V - 3.5V to Voltage divider ( Vsupply --> 27k -|- 15K -- GND)  giving ADC input on PORTA.0 = 3.1 - 1.4V
     '                                                                       (                PortA.0        )
     
     Get_volts :
      
      ADCON0 =  %00000001           ' bit 7 -N/A , Bit 6-2 = ADC chan Sel /Tempature output /FVR output = AN0 sel = 00000 ,
                                    ' Bit 1 - Go/_done status flag - 1 = Go do ADC / 0 = Done ADC , Bit 0 = Enable ADC = 1  / 0 = Disable ADC
      
      ADCON1 =  %11110000           ' bit 7 = 1 ADFM ( ADRESH, ADRESL - right justify ).Bit 6-4 ADCS = 111 - using FRC internal ( 1.6us per sample) 
                                    ' Bit3 - N/A , Bit2 = 0 (Vref- is connected to Vss) , Bit1-0 = 00 - Vref+ connected to VDD 
      
      FVRCON  = %00000010           ' Bit 7 =0 disable Fixed Voltage Ref Modual, Bit 6 = VRef ready bit =1 always ok on 16F1825, Bit 5 - Temp indicator 1= enable/0 = disable
                                    ' Bit 4 = Temp Indicator Range Sel 0=Vout -Low range / 1= Vout -High range , Bit 3-2 = 00( DAC Fixed volt sel )
                                    ' Bit 1-0 = 10 - ADC Fixed Vref of 2.048v
    
      pauseus 10                    ' allow ADC change and  Vref to stablise                              
      
      ANSELA =  %00000001           ' Set Port A.0 - Analoge I/O  , 0 = Digital  1 = Analog
      TRISA  =  %00001001           ' setup Port A.0 input=1,output=0 for I/O pins ( Note RA3 - is input only (digital)- refer spec)
      
    
     y = 0                          ' ensure 0
    While Y < 10                    ' do 10 voltage reads
      ADCIN 0,ADval                 ' Read PortA.0 value into variable , this applies the fixed voltage ref (via ADCON0,ADCON1,FVRCON)
                                    ' to comparitor and ADC pin selected and gives result.    
      Value = ADval                 ' Assign ADval from ACD  to Value for Average Routine Calculations 
      gosub Average                 ' Go do running average of ADC reading
     
       y = y+1                      ' Increment loop counter
    wend  
      
       
         If ADval <= 510  then      ' below value of 1.6V ( 4.8v Vsupply) then set Power low values
               Pulse_off  = 500     ' Set Pulse_off duration to 500us to signal power low to IR_RX 
               Flash_rate =  10     ' Set Power Led toggle to fast rate to show low power  
            Else 
               Pulse_off  = 276     ' Set Pulse_off duration to 300us( adj) to signal NOT power low to IR_RX 
               Flash_rate = 200     ' Set Power Led toggle to Normal rate to show NOT low power   
         endif   
          
                                    ' Restore PortA.0 to digital . ACD OFF etc 
       ADCON0 =  %00000000          ' Set ADC to Disable  - Bit 0 = Enable ADC = 1  / 0 = Disable ADC
       ANSELA =  %00000000          ' Set Port A.0 = 0 Digital, 0 = Digital  1 = Analog
       TRISA  =  %00001000          ' setup Port A.0 input=1,output=0 for I/O pins ( Note RA3 - is input only (digital)- refer spec) 
     
     return 
      
    
    ' -=-=-=-=-=-=  Average Analog values - a DT routine -=-=-=-=-=-=-=-=-=-=
    Average:
        IF Value = ADavg Then NoChange
        IF ABS (Value - ADavg) > FAspread OR Value < AvgCount Then FastAvg
        IF ABS (Value - ADavg) < AvgCount Then RealClose
        ADavg = ADavg - (ADavg/AvgCount)
        ADavg = ADavg + (Value/AvgCount)
        GoTo AVGok
      FastAvg:
        ADavg = Value
        GoTo AVGok
      RealClose:
        ADavg = ADavg - (ADavg/(AvgCount/4))
        ADavg = ADavg + (Value/(AvgCount/4))
      AVGok:
        Value = ADavg			' Put Average back into Value
      NoChange:
    Return

  2. #2
    Join Date
    Feb 2010
    Location
    USA, New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default Re: Battery voltage monitoring for use on 16F1825

    Congrats on getting it working. Will you be using the software-based PWM or the hardware based HPWM?

    Best Regards,
    Paul
    The way to avoid mistakes is to gain experience. The way to gain experience is to make mistakes.

Similar Threads

  1. Voltage Monitoring
    By Megahertz in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 11th July 2011, 23:51
  2. ADCIN car battery monitoring
    By mitchf14 in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 29th June 2008, 08:04
  3. pwm with voltage monitoring
    By jetpr in forum General
    Replies: 3
    Last Post: - 29th January 2008, 08:49
  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