Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

12 Volt Car battery charger with ADC

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
rafael_jimenez

Bascom Member



Joined: 26 Oct 2006
Posts: 27
Location: Wesseling

dominicanrep.gif
PostPosted: Wed Mar 11, 2015 11:52 am    Post subject: 12 Volt Car battery charger with ADC Reply with quote

Hello People,
I'm doing a battery charger for my car,but can not get me as I wish functions.
in my code, I read the ADC status every two seconds, I think it is configured well timer1,
but the timer1 makes me blink on port B, 0 each interruption, when the battery is charging,
maybe you can tell me where my problem?
by the way,
under conditions( IF Then , I have tried in various ways) that would begin charging the battery when low volt 12.20
but I can not get the relay is activated when the battery is low volt 12.20,
maybe someone can lend a hand, please.

Code:
'*******************************************************************************
'** 12 Volt Car Battery Charger Monitor
'** Battery 68Ah 560A.
'** Battery need 14,40 Volt constant current
'** Compiler version :2.0.7.8
'** Compiler build   :2.0.7.8.004
'** IDE version      :2.0.7.8.003
'** Windows OS       :Windows 7 Ultimate
'** Windows Sp : Service Pack 1
'** Rafael B.Jimenez
'*******************************************************************************

$regfile = "m32def.dat"                                     ' Atmega32
$crystal = 1000000                                          '1Mhz
$hwstack = 20
$swstack = 20
$framesize = 20
'$sim

Dim A As Byte                                               'for 14 x ADC
Dim Voltage As Word                                         'calculated voltage ADCvalue
Dim Valueadc As Word                                        'Battery Voltage
Dim Timecount As Word
Dim Displayvolt As String * 3                               'convert number to string for decimal point value
Dim Voltstring As String * 4
 Config Portb.0 = Output                                    'Switch Solid State Relay
 Config Porta.6 = Input                                     ' pin for ADC

'----------configuration Timer1--------------

 Config Timer1 = Timer , Prescale = 64
Enable Timer1
On Timer1 Isrtimer1
Enable Interrupts
Timer1 = 34286

'_________________________________________
'_______________Configuration LCD ________________________________

 Config Lcdpin = Pin , Rs = Portc.2 , E = Portc.3 , Db4 = Portc.4 , Db5 = Portc.5 , Db6 = Portc.6 , Db7 = Portc.7

Config Lcd = 20 * 4

'---------------------Configuration ADC----------------------
   Config Adc = Single , Prescaler = Auto , Reference = Avcc
   Start Adc


  Do

  Wait 2

  Loop

'__________________________________________________
  Isrtimer1:

    Reset Portb.0                                           'Relay is OFF
   
        Waitms 5

    Valueadc = 0
   For A = 1 To 14
    Valueadc = Valueadc + Getadc(6)
    Next A

  '---------display state ---------------
     If Valueadc > 4176 Then                                ' if the value is over 14,40, 4176/14,40 = 29
    Cls
    Lcd "FuLL"
    End If

   If Valueadc = 0 Then                                     'when value adc cero , no battery
  Cls
 Locate 1 , 1
 Lcd "  no "
 Locate 2 , 1
 Lcd "battery"
 End If

   If Valueadc < 4076 And Valueadc > 0 Then                 'If valueADC are under ValueADC and over cero

    Set Portb.0                                             'Switch Relay ON
 Cls
      Voltage = Valueadc / 29
      Displayvolt = Str(voltage)
            Voltstring = Format(displayvolt , " 0.0")
            Locate 1 , 1
            Lcd "charging"
            Locate 2 , 1
    Lcd Voltstring ; " V"

      End If
    Return
    Final:

      Reset Portb.0                                         'switch Relay OFF

      Cls
        Lcd "full"
     Wait 2
    Return
 

your help is appreciated
Thank you
Regards

(BASCOM-AVR version : 2.0.7.8 )
Back to top
View user's profile
snipsnip

Bascom Member



Joined: 10 Feb 2014
Posts: 66
Location: Melbourne

australia.gif
PostPosted: Wed Mar 11, 2015 12:45 pm    Post subject: Reply with quote

Try not using CLS, instead just print extra spaces to over write the longest text ie "hello world" will be replaced with "gone ........... "(dots are spaces)
CLS Takes a long time!!


Also - why you doing everything in the interrupt - do as little as possible. you've set it for two secs but have a wait 2 at the end of it before the exit ??

Id re-write the whole thing without using interrupts - You don't need it for "get adc". keep it simple.


Last edited by snipsnip on Wed Mar 11, 2015 1:08 pm; edited 3 times in total
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1534
Location: Basel, Switzerland

switzerland.gif
PostPosted: Wed Mar 11, 2015 12:51 pm    Post subject: Reply with quote

Hello,

Please do not put your program in the ISR. What you should do is keep the ISR and short as possible. What you should do is just set a flag in the ISR and the main program looks at this flag.
Something like:

Code:

do
  if Flag=1 then
    do something
    flag=0
loop
end

ISR:
  Flag=1
  return
 


Regards
Ian Dobson

_________________
Home of AVRTimer, the Bascom timer value calculator.
http://www.planet-ian.com

Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4966
Location: Holland

blank.gif
PostPosted: Wed Mar 11, 2015 12:54 pm    Post subject: Reply with quote

- I removed your serial nr. from your post.

how do you connect the battery to the A/D converter input? I assume you have a resistor divider. Since you have a reference of AVCC, which is 5v ? make sure the maximum input is 5V. (use a zener to protect against overload).
I do not get this code :
For A = 1 To 14
Valueadc = Valueadc + Getadc(6)
Next A

if you want an average you need to divide it.

i also do not get the fact that you have an empty do..loop and use an ISR.
you can simply put your ISR code inside this loop.
Last, increase $hwstack to 40 when using an ISR.

_________________
Mark
Back to top
View user's profile Visit poster's website
rafael_jimenez

Bascom Member



Joined: 26 Oct 2006
Posts: 27
Location: Wesseling

dominicanrep.gif
PostPosted: Thu Mar 12, 2015 12:02 pm    Post subject: Reply with quote

Thank you very much,

Hello Mr. snipsnip,
you are reason, but I did only to see Voltage State, because my code is for Attiny45 without display Very Happy
Thank you

Hello Mr.i.dobson,
I donīt have to much experience with Interrupt but I make change in my code now.

Hello Mr. Arbert, Sorry for my error ,
I Do Test with one Adjustable Power Supply from 11,80V to 16,80V.
Your are reason, I have Resistor divider but with Pot 10K, so
Ground 5K6 >>>Pot 10K<<<< 100K Resistor to 12V + Battery,
I Adjust with Pot the ADC to work fine.
Now my code Work.
But


[code]
'*******************************************************************************
'** 12 Volt Car Battery Charger Monitor
'** Battery 68Ah 560A.
'** Battery need 14,40 Volt constant current
'** Compiler version :2.0.7.8
'** Compiler build :2.0.7.8.004
'** IDE version :2.0.7.8.003
''** Windows OS :Windows 7 Ultimate
'** Windows Sp : Service Pack 1
'** Rafael B.Jimenez
'*******************************************************************************
$regfile = "m32def.dat" ' Atmega32
$crystal = 1000000 '1Mhz
$hwstack = 40
$swstack = 40
$framesize = 40
'$sim

Dim A As Byte 'for 14 x ADC
Dim Voltage As Word 'calculated voltage ADCvalue
Dim Valueadc As Word 'Battery Voltage
Dim Timecount As Word
Dim Displayvolt As String * 3 'convert number to string for decimal point value
Dim Voltstring As String * 4
Config Portb.0 = Output 'Switch Solid State Relay
Config Porta.6 = Input ' pin for ADC

'_______________Configuration LCD ________________________________

Config Lcdpin = Pin , Rs = Portc.2 , E = Portc.3 , Db4 = Portc.4 , Db5 = Portc.5 , Db6 = Portc.6 , Db7 = Portc.7

Config Lcd = 20 * 4
Cursor = Off
'---------------------Configuration ADC----------------------
Config Adc = Single , Prescaler = Auto , Reference = Avcc
Start Adc


Do
'__________________________________________________

Valueadc = 0
For A = 1 To 15
Valueadc = Valueadc + Getadc(6)
Next A

'---------display state ---------------
If Valueadc > 4176 Then ' if the value is over 14,40, 4176/14,40 = 29
Portb.0 = 0
Cls
Lcd "FuLL"

End If


If Valueadc = 0 Then 'when value adc cero , no battery
' Portb.0 = 1
Cls
Locate 1 , 1
Lcd " No "
Locate 2 , 1
Lcd "Battery "
End If

If Valueadc < 4177 And Valueadc > 0 Then 'If valueADC are under ValueADC and over cero

Portb.0 = 1 'Switch Relay ON

Voltage = Valueadc / 29
Displayvolt = Str(voltage)
Voltstring = Format(displayvolt , " 0.0")

Locate 1 , 1
Lcd "charging"
Locate 2 , 1
Lcd Voltstring ; "V "

End If

Loop


I mean:
I want that when the Port-B is low is kept low until the voltage of the battery reaches 12 volts, the battery should always be between 12.20 to 12.30 volt and then under 12,20 the PortB.0 go High.
I test divers Form but do not get fine.
maybe someone has a better idea than I.
Always thought as my code works, but want it
when the value reaches 12.20 adc disable relays

Thnak you very much.
Regards
Back to top
View user's profile
Deanus

Bascom Member



Joined: 26 May 2006
Posts: 188
Location: Adelaide

australia.gif
PostPosted: Sun Mar 15, 2015 8:53 am    Post subject: Reply with quote

Hi Rafael_jimenez,


I agree with the others whe they say do the minimum abount possible sutch as set a flag and do the rest in the main loop.
A couple of things with battery chargers,

1) Turn OFF the charging voltage Before measuring the battery voltage else you will only be reading the charge voltage and the program will terminate the charge too early.

2) You don't mention what type of battery you are charging. NiMh, NiCd, SLA, LiIo all have different charging patterns.

3) I really would like to see the battery that requires charging anywhere from
Quote:
'** Battery 68Ah 560A.
. Quite a battery. Submarine battery maybe?

4) After the

Quote:
For A = 1 To 15
Valueadc = Valueadc + Getadc(6)
Next A


you should do a" Valueadc /15" to average the number out, and to deal with smaller numbers. Not strickly necessary but thats what I would do.

5) what is your ADC reference voltage set to ?

6) Your voltage divider needs looking at. If you say

Quote:
Ground 5K6 >>>Pot 10K<<<< 100K Resistor to 12V + Battery
The using the calculator at this site, http://www.raltron.com/cust/tools/voltage_divider.asp
Then when the pot is at the uppermost voltage to the ADC is 0.6V


Input Voltage 12V
R1 100000r
R2 5600r
Output Voltage 0.636V

When the input voltage is 1V the output to the ADC is 0.053V

These voltages to the ADC are too low. The ADC measures voltages from 0v to 5V
Back to top
View user's profile
rafael_jimenez

Bascom Member



Joined: 26 Oct 2006
Posts: 27
Location: Wesseling

dominicanrep.gif
PostPosted: Sun Mar 15, 2015 1:14 pm    Post subject: Reply with quote

Hello Deanus,
Thank you for you answer,

My Battery is for Car, GigaWatt Calcium Technology and form manufacturer say need Constant 14,40 volt,
my Adjustable constant Power Supply is from 0Volt to 15Volt 4Amp

I do so
For A = 1 To 15
Valueadc = Valueadc + Getadc(6)
Next A
for better unit resultad,

Sorry, perhaps ye not understand my explanation, I use 2 Resistor and one Potentiometer in my Resistor dividir to Adjust the correct ADC Value for Battery and looking so,

GND<5K6> (to 10K Potentiometer here to adc(6) <100> to Battery Plus
please See Schematic circuit.
My code prepare for one Attiny45 with Led charging control.

My code it works, but only I nedd when Battery is to 14,40 Volt Switch Off and Waiting when Battery voltage come down to 12,30 Volt, my Code doe Hystery and continuous loading all,
I need a solution, when the battery is charged burdens stop and wait until it is depleted
I am no to much experiencie with bascom , I am I am an electrician
Razz
Thank you for you help
Regards
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 671

poland.gif
PostPosted: Sun Mar 15, 2015 8:42 pm    Post subject: Reply with quote

This example show simple logic. You can set "name" for constans for better code readable and then use simple "state of unit mechanism"
State of unit You can change in any part/ place of code and then in next loop another part of code will be run.

Code:

 Dim Status As Byte

 Const Unknown = 0
 Const Charging = 1
 Const Discharging = 2

'### program START ###
Do
 'this is only example to show logic

    Valueadc = Getadc(6)

        Select Case Status
        'after Boot
         Case Unknown

               Select Case Valueadc

                  Case 0 To 11.0 V                          ' insert Your calculated value

                    Home : Lcd "No battery  "

                   Case 11.1 To 14 , 3 V

                     Home : Lcd "Charging... "
                      Status = Charging
                       Set Relay

                   Case 14 , 4 To 15 , 3 V

                     Home : Lcd "Battery Full"
                       Status = Dicharging

                 End Select                                 'end select voltage after boot

           'if unit charging battery
          Case Charging

                Select Case Valueadc

                   Case 0 To 11.0 V
                    'shortcut protection
                     Reset Relay
                      Status = Unknown

                   Case

                     14 , 4 To 15 , 3

                       Home : Lcd "Battery Full"
                       Status = Dicharging

                   End Select                               'end select when charging

           Case Discharging

                  Select Case Valueadc

                    Case 12.2 V To 12 , 4 V

                      Home : Lcd "Charging... "
                      Status = Charging
                       Set Relay

                   End Select                               'end select when waiting for discharge

           End Select                                       'end select STATUS


Wait 1

Loop
End
'### END ###
 
Back to top
View user's profile Visit poster's website
P_Santos

Bascom Member



Joined: 07 Jul 2011
Posts: 102

PostPosted: Mon Mar 16, 2015 4:50 pm    Post subject: Reply with quote

Hello,
I take this post to try to know how to properly do the evaluation of the percentage of the autonomy of a Litium battery.
I think that when we are measuring the voltage it must will have to be injected a load to the battery.
Will my reasoning correct?

Best regards
P_Santos
Back to top
View user's profile
rafael_jimenez

Bascom Member



Joined: 26 Oct 2006
Posts: 27
Location: Wesseling

dominicanrep.gif
PostPosted: Sun Mar 22, 2015 1:31 pm    Post subject: Reply with quote

Thank you EDC,for you logic Function, but no undestand to much, I Teste your code, but I can not find good results,
I test now so
Code:
'*  *******************************************************************************
'** 12 Volt Car Battery Charger Monitor
'** Battery 68Ah 560A.
'** Battery need 14,40 Volt constant current
'** Compiler version :2.0.7.8
'** Compiler build   :2.0.7.8.004
'** IDE version      :2.0.7.8.003
''** Windows OS       :Windows 7 Ultimate
'** Windows Sp : Service Pack 1
'** Rafael B.Jimenez
'*******************************************************************************
$regfile = "m168def.dat"                                    ' Atmega32
$crystal = 8000000                                          '1Mhz
$hwstack = 40
$swstack = 40
$framesize = 40
'$sim
Dim A As Byte                                               'for 15 x ADC
Dim Voltage As Word                                         'calculated voltage ADCvalue
Dim Valueadc As Word                                        'Battery Voltage
Dim Timecount As Word
Dim Displayvolt As String * 3                               'convert number to string for decimal point value
Dim Voltstring As String * 4

 Config Portb.0 = Output                                    'Solide Stat Relay
 Config Portd.6 = Output                                    ' by low led Flash
 Config Portd.7 = Output                                    'Reserve
 Config Portc.0 = Input                                     ' pin for ADC
 Relay Alias Portb.0
 Led Alias Portd.6
'_______________Configuration LCD ________________________________
 Config Lcdpin = Pin , Rs = Portd.0 , E = Portd.1 , Db4 = Portd.2 , Db5 = Portd.3 , Db6 = Portd.4 , Db7 = Portd.5
Config Lcd = 20 * 4
Cursor = Off
Cls
'---------------------Configuration ADC----------------------
   Config Adc = Single , Prescaler = Auto , Reference = Avcc
   Start Adc

' __________________________________________________
 Relay = 0
Do

    Valueadc = 0
   For A = 1 To 15
    Valueadc = Valueadc + Getadc(0)
    Next A

   Select Case Valueadc

            Case 00000 To 12225 : Gosub Status              ' 0 Volt to 12.25volt  Battery is Low
                          Waitms 50

            Case 12226 To 14410 : Gosub Charging            ' 12.26Volt to  14.40 Battery Charging
                                  Waitms 50
             Case 14511 To 15421 : Gosub Full               '14.45 to 15.40 Battery is Full
                                Waitms 50

   End Select

    Loop
    End

Full:

    Locate 1 , 1
    Lcd "Battery is      "
     Locate 2 , 1
    Lcd "FuLL      "
     Relay = 0                                              'relay is OFF
      Return


Status:

 Toggle Led                                                 ' led flash by low battery
Waitms 50

 Locate 1 , 1
 Lcd "Battery is    "
 Locate 2 , 1
 Lcd "   Low       "
 Relay = 1                                                  ' relay is ON
     Return

 Charging:
   Relay = 1                                                'Switch Relay ON
      Voltage = Valueadc / 100
      Displayvolt = Str(voltage)

            Voltstring = Format(displayvolt , " 0.0")

            Locate 1 , 1
            Lcd "charging     "
            Locate 2 , 1
    Lcd Voltstring ; "V  "

    Return


Result,
When Battery is Full 14,40 Volt and Volt come Down battery come back to charging , so is no my plan,
I wish thats ,when battery are Full 14,40 volt and Battery is under 12,30 volt beginn to charging and no under 14,40 volt.
I donīt Find one logic Soluction.
someone hat best Ideas?.
Please I need help.
Thank you
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 671

poland.gif
PostPosted: Sun Mar 22, 2015 2:04 pm    Post subject: Reply with quote

Try this joined code Smile Ofcourse some bug can be..


Code:
'$sim
$regfile = "m168def.dat"                                    ' Atmega32
$crystal = 8000000                                          '1Mhz
$hwstack = 40
$swstack = 40
$framesize = 40

'_______________Configuration LCD ________________________________
Config Lcdpin = Pin , Rs = Portd.0 , E = Portd.1 , Db4 = Portd.2 , Db5 = Portd.3 , Db6 = Portd.4 , Db7 = Portd.5
Config Lcd = 20 * 4
Cursor = Off
Cls
'---------------Configuration ADC---------------------------------
   Config Adc = Single , Prescaler = Auto , Reference = Avcc
   Start Adc


 Config Portb.0 = Output : Relay Alias Portb.0              'Solide Stat Relay
 Config Portd.6 = Output : Led Alias Portd.6                ' by low led Flash
 Config Portd.7 = Output                                    'Reserve
 Config Portc.0 = Input                                     ' pin for ADC



Dim A As Byte                                               'for 15 x ADC
Dim Valueadc As Word                                        'Battery Voltage

Dim Voltage As Word                                         'calculated voltage ADCvalue
Dim Displayvolt As String * 5                               'convert number to string for decimal point value

 Dim Status As Byte
   Const Unknown = 0
   Const Charging = 1
   Const Discharging = 2


'### program START ###
Do

  Valueadc = 0

   For A = 1 To 15
    Valueadc = Valueadc + Getadc(0)
   Next A

        Select Case Status
        'after Boot
         Case Unknown

               Select Case Valueadc

                  Case 0 To 11000

                    Home : Lcd "No battery  "

                   Case 12226 To 14510

                     Home : Lcd "Charging... "
                      Status = Charging
                       Set Relay

                   Case 14511 To 15421

                     Home : Lcd "Battery Full"
                       Status = Discharging

                 End Select                                 'end select voltage after boot

           'if unit charging battery
          Case Charging

                Select Case Valueadc

                   Case 0 To 9000
                    'shortcut protection
                    Home : Lcd "No battery  "
                     Reset Relay
                      Status = Unknown

                   Case 14511 To 15421

                      Home : Lcd "Battery Full"
                      Status = Discharging
                       Reset Relay

                   End Select                               'end select when charging

           Case Discharging

                  Select Case Valueadc

                    Case 0 To 11000

                      Home : Lcd "No battery  "
                      Status = Unknown

                    Case 11001 To 12225

                      Home : Lcd "Charging... "
                      Status = Charging
                       Set Relay

                   End Select                               'end select when waiting for discharge

           End Select                                       'end select STATUS


    Voltage = Valueadc / 100
    Displayvolt = Str(voltage)
    Displayvolt = Format(displayvolt , " 0.0")
    Lowerline : Lcd Displayvolt ; "V  "


Wait 1

Loop
End
'### END ###

 
Back to top
View user's profile Visit poster's website
rafael_jimenez

Bascom Member



Joined: 26 Oct 2006
Posts: 27
Location: Wesseling

dominicanrep.gif
PostPosted: Sun Mar 22, 2015 2:54 pm    Post subject: Reply with quote


Thank very much EDC
Now you function , very nice Very Happy
I studie you Code .
I wannt now one Ask,
to see nummber in decimal in Display but so
00.00, I test with String but I do not get the point.
can you give me an example, Please.

Thank you to much.
Regards
Rafael
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 671

poland.gif
PostPosted: Sun Mar 22, 2015 3:18 pm    Post subject: Reply with quote

If I`m understand what You want then instead of this
Code:
Voltage = Valueadc / 100
    Displayvolt = Str(voltage)
    Displayvolt = Format(displayvolt , " 0.0")
    Lowerline : Lcd Displayvolt ; "V  "


You can

Code:
Voltage = Valueadc / 10 ' <-10 NOT 100 now..
    Displayvolt = Str(voltage)
    Displayvolt = Format(displayvolt , " +00.00")  'even plus sign can be added if You want
    Lowerline : Lcd Displayvolt ; "V  "



Remember that decimal point is a sign/char too so string must me bigger to acommodate characters.
..and remember that Status You can change anytime. So You can add Button for example and change Status immediately to Charging (without waiting to battery Low).
You can also reset Relay before measure voltage..everything can be better Very Happy
I think that code where is nothing to improve not exist Very Happy
Back to top
View user's profile Visit poster's website
rafael_jimenez

Bascom Member



Joined: 26 Oct 2006
Posts: 27
Location: Wesseling

dominicanrep.gif
PostPosted: Mon Mar 23, 2015 12:24 pm    Post subject: Reply with quote

Very Happy Ok now i understand,
Thank for your clarification.

Regard
Raffi
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum