Shop OBEX P1 Docs P2 Docs Learn Events
DS1302 So that in the event of a power failure with a SUPER CAP Can Not get th — Parallax Forums

DS1302 So that in the event of a power failure with a SUPER CAP Can Not get th

sam_sam_samsam_sam_sam Posts: 2,286
edited 2007-12-18 18:57 in BASIC Stamp
Chirs

Can you help with this.....>>> In how to write this where when the Power is Lost it will pick up where· it left off· when power back up

I want to use the Pool Pump Timer as an example so·I can·see what your talking about

Thank You for any help that you can give with this ·

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any·idea.gif·that you may have and all of your time finding them

·
·
·
·
Sam

Post Edited (sam_sam_sam) : 12/16/2007 5:14:14 AM GMT

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-12-04 17:49
    Sam, I just replied to your thread at the following link. If this doesn't help you then please clarify what the controller is doing what what would need to happen should power be lost.

    http://forums.parallax.com/showthread.php?p=690690

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-12-04 18:03
    Chris

    The way this routine is set up NOW is that if the power is lost is just start the routine·over

    which is ok for the pool pump but not for the light controler that i want to build

    What I want to do now is this if the power is lost i want it to pick up where it was when the power was lost

    Example if the pump was running and·it had 30 minutes left to run then it would pick where it left off

    Example if· was in the timer routine·to be·OFF and the·power was lost the and there

    ·is 40 minutes left it would pick up where it left off

    I just want to use this·as an example because i have·it already·written


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 12/4/2007 6:09:32 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-12-04 18:17
    Sam,

    If I were you I would store the pump time remaining (in minutes) to the DS1302 RAM each minute that it is updated. When power is restored your program can check that location for a non-zero value, indicating that the last cycle was not completed, as well as containing the remaining time.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-12-04 22:57
    Chris Savage

    If I were you I would store the pump time remaining (in minutes) to the DS1302 RAM each minute that it is updated.

    Can you give me an example of how to this


    ·When power is restored your program can check that location for a non-zero value, indicating that the last cycle was not completed, as well as containing the remaining time.

    I understand what you have here·but dont know to go about writing the the code for this

    This is what i am looking for

    Can you give me an example of how to this


    Can you show how to do this with the Counter Demo that you wrote

    The same one that i used when i wrote this routine

    Thank you for all of your help and time in this matter

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-12-05 15:27
    Sam,

    ·· Okay, here’s some code for you to pick at…I’m sure you have the first one since that’s probably what your timer routine is based on, but I wanted to provide a simple example of a timer. Of course this is an elapsed time, timer. To use it for countdown we covered in another thread the concept of waiting for the seconds to change and subtracting 1 from a counter variable until the result is zero.

    ·· Now, you needed an example of storing data in the DS1302 RAM and an example of the program re-initializing the variable on startup. Both of these concepts are handled within the second attached program which is a Digital Thermostat controller I wrote in NY. Hopefully this will allow you to integrate the necessary functionality into your program. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-12-05 23:18
    Chris Savage


    To use it for countdown we covered in another thread the concept of waiting for the seconds to change and subtracting 1 from a counter variable until the result is zero.
    Can you Post a Link where you talked· about this


    Now, you needed an example of storing data in the DS1302 RAM and an example of the program re-initializing the variable on startup.

    Part of this code from a post i did sometime ago

    http://forums.parallax.com/showthread.php?p=594278

    I post what i have come with so far and not sure how to write some parts

    Can you help with what i am missing ( just want the secs to update for now)

    Thank You for your Time and Help

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 12/5/2007 11:52:43 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-12-06 16:03
    Sam,

    Picture this…You have a variable with the number of minutes you want your timer to run…We’ll call it count. So count = 45 for 45 minutes. In the example you linked to, I showed how the DS1302 loop was dropping out every second. So now you have two ways to do this. One is to multiple your count variable by 60 to get the number of seconds in one minute. In this case count would become 2700 (you would need a Word variable). Each time the loop drops, you subtract one from count until it equals zero.

    Option two, you write a simple routine to subtract 1 from the count variable every 60 passes out of the main loop. This would require another byte variable, so the same amount of variable space would be consumed either way. You could save a byte if the DS1302 was only being used as a timer, but I am on the assumption it is also storing the real date/time. From all the provided examples I am not clear on what you’re missing.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-12-06 17:51
    Chirs

    Can you help with something

    Do I have this right in what i have here


    Dose this go in the·DO Loop with the IF hrs =$05 AND mins =$00

    ·index = 0············································· ' RAM Address On DS1302
    · reg = $C0 | (index << 1)·······················' RAM Write Mode + Address
    · ioByte = oldsecs
    · GOSUB RTC_Out

    · index = 1············································· ' RAM Address On DS1302
    · reg = $C0 | (index << 1)························' RAM Write Mode + Address
    ·ioByte = secs
    · GOSUB RTC_Out


    Do i write the routine·that checks what the time in the DS1302 chip and if the time
    is anything·OTHER·THAN··hrs = 00 and mins = 00· then gose back to main routine

    Would this be when power is frist turn on to the Basic Stamp·^^^^^^
    What i·have·above······························································^^^^^^
    ····························································

    Init:
    · reg = CWPr············································ ' Initialize DS1302
    · ioByte = WPr0········································· ' Clear Write Protect
    · GOSUB RTC_Out

    · index = 0················································ ·' Address Of Saved SetTemp
    · reg = $C1 | (index << 1)·························· ' RAM Read Mode + Address
    · GOSUB RTC_In·········································· ' Get Saved SetTemp
    ··secs = ioByte··········································· ·' Restore SetTemp
    · index = 1·················································· ·' Address Of Saved Mode
    · reg = $C1 | (index << 1)·····························' RAM Read Mode + Address
    · GOSUB RTC_In·········································· ' Get Saved Mode
    · mode = ioByte········································· · ' Restore Mode


    This·where i would·have what to·do if the hrs and mins·ARE ·hrs = 00 AND mins = 00 in the Mode routine

    ·Am·I right about all of ·this or am i missing something here

    Thanks for all of your time and help so far with this

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-12-06 23:41
    In the first section you're attempting to save the seconds and old seconds. I thought you wanted to save minutes? In the second section you're simply over-writing the seconds register with what is in the same location after the first read. This of course all assumes you have the constants defined for those registers.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-12-07 00:42
    Chris



    Thank you for your reply

    I thought you wanted to save minutes >>>> Yes I do want to use minutes

    But I want·to test the code using seconds because it would not take as long to see if the code

    is working

    ·Do i have what i need in order for this to work

    #1

    If the Power is Lost to the Basic Stamp will it pick up where it left off·when the power come back ON·and keep doing what it was doing before the power when OFF

    #2

    If you had NO Power to the Basic Stamp And DS1302 would this routine work when both are power UP

    '
    [noparse][[/noparse] Initialization ]


    Init:
    · reg = CWPr··························· ' Initialize DS1302
    · ioByte = WPr0························ ' Clear Write Protect
    · GOSUB RTC_Out························ ' Send Command·


    index = 0·············································· ' Address Of Saved secs
    · reg = $C1 | (index << 1)······························ ' RAM Read Mode + Address
    · GOSUB RTC_In·········································· ' Get Saved SetTemp
    · secs = ioByte········································· ' Restore secs
    · index = 1············································· ' Address Of Saved Mode
    · reg = $C1 | (index << 1)······························ ' RAM Read Mode + Address
    · GOSUB RTC_In·········································· ' Get Saved Mode
    · mode = ioByte········································· ' Restore Mode




    ·· Start:
    mins = $00
    secs = $00



    GOSUB Set_Time


    ·DO
    · GOSUB Get_Time························································· ' Get Currt Time/DateData


    ·· index = 0···································································' RAM Address On DS1302
    · reg = $C0 | (index << 1)·············································' RAM Write Mode + Address
    · ioByte = secs
    · GOSUB RTC_Out·························································' Store Current secs


    · DEBUG HOME, HEX2 mins, ":", HEX2 secs, CR
    · DEBUG· HEX2 oldsecs, CR



    · counter = counter + 1···················································· ' Update Timeout Counter


    · IF mins = $00 AND secs = $05 THEN
    ··· GOSUB relay_ON································································· ' Turn relay ON
    · ELSEIF secs <> oldsecs THEN················································ ·' Have Seconds Changed?
    · DEBUG DEC counter, CR························································ ·' Display Value To DEBUG Window
    ··· counter = 0······································································· ' Reset Counter
    ··· oldsecs = secs··································································· ' Remember Update
    · ENDIF


    ·IF counter· > 100 THEN···························································' Stop if counter get to 100 if clock is not running
    · GOSUB relay_OFF
    · STOP
    · ENDIF


    ·IF mins = $03 AND secs = $15 THEN EXIT


    LOOP


    DEBUG CR, CR, "3:15 Elapsed", CR
    GOSUB relay_OFF····································································' Turn relay OFF





    ·· In the first section you're attempting to save the seconds and old seconds

    Thank You for·pointing this out to me

    I will try this over the week but want to understand how to set this up frist



    Can you tell me what this is used >>>>· or·Is this for the DS1620

    SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]WrCfg, %10]········ ' Use with CPU,
    · PAUSE 10
    · SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]StartC]············ ' Start Conversions




    Thanks for all of your help and time·with this

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 12/7/2007 6:17:52 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-12-07 17:05
    Sam,

    I’m not sure I understand what you’re doing. I thought you wanted to store the minutes remaining so when power resumed you could run the pump for the remaining cycle. If that is the case, then you would store the minutes, not seconds. What you store has nothing to do with the resolution of you count. Your main routine can still count seconds based on the remaining minutes. Knowing the minutes doesn’t affect how quickly the system detects power loss. If you lose power it will be instant. The circuit won’t wait until the next minute to react. In your most recent post you’re still loading seconds and now mode from the DS1302 RAM. Again, I would think you want the minutes remaining unless you’re converting to all seconds.

    I also don’t understand what the counter routine you posted means to your Pool Timer code. That will only wait for an elapsed time (assuming the time was reset prior to calling it) not deal with the data from the DS1302 RAM. Here is what your program needs to do…You might want to create a flowchart to outline the functions and then build your code from it.

    1. At power up, load the last saved remaining minutes from the DS1302 RAM. If the value is zero, then nothing needs to be done. If the value is greater than zero then the timing routine should be executed with that value to complete the cycle.
    2. During the timing routine you should be checking for any change in seconds and when the seconds register has changed, you update you counts and reload the DS1302 RAM with the new minutes if it has changed.

    Everything else is glue and menu options and things. Those are the two more important routines. There is no reason to save oldSeconds. That variable exists only to detect a change in seconds. That only needs to be done within the loop. The last code fragment could be for a DS1620, but there’s no way to know as there’s not enough information there. Where did you get it?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-12-10 19:49
    Sam, were you able to use the examples provided as well as the programming guidelines to implement your needs for your project?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-12-11 21:08
    Chris

    Thank You for your help and time and your reply

    I was to busy this weeken to try this out but as soon as i do i will let you know how it work

    I going to order some 1 frad caps and give it try

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-12-14 01:41
    Chris

    I order some super cap today

    I order two different types

    1 Frad @ 5.5 volts·

    4.5 Frad @ 2.5 volts

    Which one should i use with the DS1302 Time Chip
    Now I know it says in the data sheet that will run 2 volts· is this to mean that when it on the back up it will run on 2 volts

    Do i have the right Idea here
    If not·can you help me this test code

    Where do i put these two ROUTINE from· Initialization· to Program Code


    Is there any code routine that i need to write if the hrs = $00·AND·mins = $oo AND secs· when the Basic Stamp and the DS1302 Time Chip at Power Up· other than· GOSUB Set_Time·hrs = $00 mins = ·$00 secs = $00 this is when the Basic Stamp and· DS1302 when frist power up and the SUPER CAP is NOT CHARGED

    ·Is there any code·routine that i need to write if hrs = something AND mins = something AND secs = something when the Basic Stamp and the DS1302 Time Chip at Power Up with the SUPER CAP charged and running DS1302 chip

    ·Now I·DO NOT·want to go GOSUB Set_Time· and Reset Time
    ·I want pick up from where it left off before the power was· lost if the relay was ON or OFF then it go back to that·routine depending on·what hrs and mins are in the DS1302 chip when power come back on to the Basic Stamp


    ·At Power up

    Dose the DS1302 chip have a TIME anything·OTHER than·hrs = $00 mins = ·$00 secs = $00 OR $80 in any of them

    If·YES goto Set Time hrs =$00 mins = $00 secs = $00

    If·NO then go to Which·EVER·routine base on what time the DS1302 chip has as for time



    These·two routine·i am not sure·where to put them
    ·I know how i want it to work though

    ·'
    [noparse][[/noparse] Initialization ]

    ·
    Will this work what i have·below or am i missing something

    ·Do i need to skip this part if the DS1302·has some value in hrs and mins and secs other than $00
    ·Where do i put the set time routine

    ·Init:
    · reg = CWPr············································ ' Initialize DS1302
    · ioByte = WPr0········································· ' Clear Write Protect
    · GOSUB RTC_Out·········································
    ' Send Command
    ·index = 0·············································· ' Address Of Saved mins
    · reg = $C1 | (index << 1)······························ ' RAM Read Mode + Address
    · GOSUB RTC_In·········································· ' Get Saved SetTemp
    · mins = ioByte········································· ' Restore mins
    · index = 1············································· ' Address Of Saved Mode
    · reg = $C1 | (index << 1)······························ ' RAM Read Mode + Address
    · GOSUB RTC_In·········································· ' Get Saved Mode
    · mode = ioByte·········································
    ' Restore Mode



    '
    [noparse][[/noparse] Program Code ]

    Run_clock:

    DO

    GOSUB Set_Time
    · GOSUB Get_Time············································· ·' Get Current Time/Date Data
    ·· index = 0····················································· · ' RAM Address On DS1302
    · reg = $C0 | (index << 1)································· · ' RAM Write Mode + Address
    · ioByte = mins
    · GOSUB RTC_Out·············································· ·
    ' Store Current mins

    IF hrs = $00 AND mins = $00 AND secs = $05 THEN
    HIGH relay
    IF hrs = $00 AND mins = $03 AND secs = $00 THEN
    LOW relay
    ENDIF
    ENDIF


    LOOP


    '
    [noparse][[/noparse] Subroutines ]


    Thank you for all of your help and time·that you given me in the past
    ·I will have to wait until i get the SUPER CAPS come in to test this code

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 12/14/2007 6:13:28 AM GMT
  • RDL2004RDL2004 Posts: 2,554
    edited 2007-12-14 12:59
    The voltage rating on the caps is the maximum they will tolerate. The 5.5 volt is easier to use because you won't have to add extra circuitry to drop the charging voltage to a safe level. The internal diodes in the 1302 won't get you low enough to use a 2.5 volt rated capacitor.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Rick
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-12-14 16:04
    Rick

    Thank you for your reply

    I realy did not understand this part because it says that it run at 2 Volts

    Then when it charges the voltage must be·close to ·5 volts

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-12-14 16:19
    Rick is right…you will need to use the 5.5V unit. Of course, it will only charge to the voltage set by the DS1302. First let me answer your question about the time-keeping registers at power on…as far as I know, the time registers will mostly contain zeros, except the seconds, which will contain $80. This is because the clock stop bit is set by default until the time is initialized. Of course, once you have a battery backup, it won’t power down again anyway (unless the cap/battery goes dead).

    As for initializing the charging circuit for the 5.5V Supercap. You should be able to send the following to it in your Initialization routine.

    reg = %10010000
    ioByte = %10100101
    GOSUB RTC_Out
    


    This of course, assumes you’re using my own template or examples. This will set one diode and one (smaller) resistor, which is fine for the Supercap.

    While the DS1302 will run down as low as 2V, there is no way to charge a backup battery that low and the DS1302 will run off the higher of Vcc1 or Vcc2, thereby defeating your backup scheme. Thus, if you plan on having a backup battery your main supply should be slightly higher than the battery backup to ensure proper operation. I hope this helps. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-12-16 04:49
    CHris

    Or anyone else can explane why one will work but other will not

    I had some to day to try some code

    This is the only code that seem to work when you take the power away from the Basic Stamp then put it back

    This one will turn the relay back·ON when you remove power and power it back up

    DO WHILE mins < $02

    · GOSUB Get_Time

    · DEBUG HOME, HEX2 mins, ":", HEX2 secs, CR

    · IF secs > $05 THEN
    · HIGH relay
    ·· ENDIF


    LOOP

    ·
    DO



    ·GOSUB Get_Time

    · DEBUG HOME, HEX2 mins, ":", HEX2 secs, CR

    LOW relay

    LOOP




    This will not work when you do the same thing as above

    This will one the relay will NOT come back·ON when you remove the power to the Basic Stamp then power it back up

    DO

    · GOSUB Get_Time

    · DEBUG HOME, HEX2 mins, ":", HEX2 secs

    · IF mins = $00 AND secs = $05 THEN
    · HIGH relay
    · ENDIF
    · IF mins = $01 AND secs = $10 THEN
    · LOW relay
    · ENDIF

    · IF mins = $02 AND secs = $15 THEN EXIT
    LOOP


    DEBUG CR, CR, "2:15 Elapsed"

    STOP

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 12/16/2007 5:09:08 AM GMT
  • hmlittle59hmlittle59 Posts: 404
    edited 2007-12-17 01:50
    OOPS!!!

    I didn't do my research on the forum before posting my question(s). SORRY...Chris, I saw your 3 lines of code to Init. the trickle charge.
    1) Does this need to be after the DS1302 is up and running?
    2) I have a different description from the Demo, so i just need to cross match them
    3) Will a 5.5v Cap and 3.6v home phone battery work together?

    thanks a bunch

    hmlittle59
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-12-17 07:14
    I’m not sure what you mean. You could hang a 5.5V Cap off the charging circuit, but it won’t be charged to a full 5.5V…Still, it would work as it should. You could use the 3.6V battery as well if it were Ni-Cad only.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • hmlittle59hmlittle59 Posts: 404
    edited 2007-12-18 03:21
    Hello Chris,

    1) Can the Cap. and BATT. 3.6v be used together with this chip?
    2) Once the Trickle Charge is init., does it stay on til. power is remove?


    thanks

    hmlittle59
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-12-18 18:57
    hmlittle,

    You wouldn’t use the cap and the battery at the same time. The DS1302 will run off which ever is the greater of the two input voltages (Vcc1 or Vcc2). Now, only one is going to be charged (Vcc1) and its voltage will come from the other source (Vcc2). Not to mention you want to run the DS1302 primary voltage at the same voltage as the I/O. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
Sign In or Register to comment.