Shop OBEX P1 Docs P2 Docs Learn Events
DS1302 hangs after power loss — Parallax Forums

DS1302 hangs after power loss

FalconFalcon Posts: 191
edited 2011-09-17 09:04 in BASIC Stamp
Hello,
I'm using Chris's LCD Demo code in Slot 0 of a BS2px to create a time stamp when a sensor is triggered. The Sensor code is spread out in Slots 1 thru 5. I then use RUN statements to skip from Slot 5 back to Slot 1 skipping the initialization in Slot 0 so it only occurs at the start. I placed a copy of the Get Time subroutine in each Slot to minimize jumping back and forth among the 5 "active" Slots. I also have a battery on the DS1302 to keep the time.

The problem I'm having is that when I lose power, the DEBUGIN command waits for an input, and the remainder of the code does not execute until I press either the "1" to re-enter the Date/Time, or the "2" button to display the Date/Time . When I press "2' the time is recorded correctly so the battery back-up is working.

How can I skip the initialization of the Date/Time after a brief power outage (since it is backed-up with the battery) and have the main code execute? Do I need to add a loop and couter to skip out of the loop if the Date/Time isn't input within a certain time?


falcon

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-08-24 17:46
    It's not at all clear about what you're doing, but it sounds like your problem is that the DS1302 has battery backup, but the BS2px does not. When the BS2px resets after a brief power outage, it doesn't know that the DS1302 is already initialized. As far as I can tell, there's nothing you can read from the DS1302 that tells you that it's already initialized. You could store a special value in a couple of locations in one of the EEPROM slots that is set when the RTC is initialized and use that to skip the initialization code. You'd have to have some way to clear those locations if you want the Stamp to go through its RTC initialization the next time it's powered up. I think you still have to initialize some of the control registers in the DS1302 each time it's powered up, but you don't have to reset the time.
  • Clive WakehamClive Wakeham Posts: 152
    edited 2011-08-25 00:17


    Actually the Trickle Charger Resistor and Diode Select register has a power on state of 5C (hex) which can be changed afterwards.
    So if the program changes that register earlier and then is reset, that register will hold that value if its got battery backup and the program can check that value and continue its data collection.

    Page 7 of the manual
  • stamptrolstamptrol Posts: 1,731
    edited 2011-08-25 05:12
    I used Chris' program as the basis for my project as well and had the same need to restart gracefully after a power failure.

    As Mike has suggested, I use a flag to let the main program ( slot 0) know whether the clock (slot 1) has been set. If so, continue without waiting.

    I've attached my programs for you to have a look at. The main program is quite long and involved, but the the Initialization section is up near the beginning. The clock program also has been modified to correct for Daylight Saving Time.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2011-08-25 11:02
    There are two ways I have used to determine that the DS1302 has been initialized. The first (and oldest method) is reading bit 7 of the seconds register(IIRC, check the datasheet), which tells if the clock is in halt mode or not. This should be the default power-up state of the DS1302 if it has not had a battery backup. The seconds will not count when this bit is set. In fact, it stops the oscillator. Method two is one I helped someone with here on the forums before which was simply reading the DS1302 RAM for a flag I set. If power had been lost the RAM would not contain the value I had stored in that location. You could store the flag in EEPROM on the BASIC Stamp Module, however that is not a certain indication that the DS1302 is still initialized, but that you had previously initialized it. I am all for making the program bulletproof. In fact, I also use the Write Protect register to WP the clock in between normal updates. This prevents it from being written accidentally should your MCU fail or go into an unknown state.
  • FalconFalcon Posts: 191
    edited 2011-08-25 17:31
    I appreciate all the replies!

    I'll have to look at the suggestions and try to work my way through that code.

    I haven't used the DS1302's RAM yet so that might be a good exercise to help me learn about that.

    Thank you to all,

    Falcon
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2011-08-25 17:41
    A demonstration for using the RAM can be found in any of my DS1302 projects. If you need additional help just reply here.
  • CalMarinerCalMariner Posts: 64
    edited 2011-08-27 23:48
    This might be a low-fi approach, but if you know that the time will remain set, just design your program so that it launches the time immediately. I originally had my firmware set where it would default to the main menu, but since it was to be installed in a remote location, I needed it to just start on its own.

    http://forums.parallax.com/showthread.php?133886-Six-Digits-of-Doom
  • FalconFalcon Posts: 191
    edited 2011-09-09 19:17
    stamptrol wrote: »
    I used Chris' program as the basis for my project as well and had the same need to restart gracefully after a power failure.

    As Mike has suggested, I use a flag to let the main program ( slot 0) know whether the clock (slot 1) has been set. If so, continue without waiting.

    I've attached my programs for you to have a look at. The main program is quite long and involved, but the the Initialization section is up near the beginning. The clock program also has been modified to correct for Daylight Saving Time.

    Stamptrol,

    I like the suggestion using the ippdump_main_final...... code that you and Mike suggested.

    I have this code in Slot 0:
      GET Clk_rd,clkrun     'check to see if clock has been recently read
                            ' Clk_rd will be 1 if clock has been read
      IF clkrun =0 THEN
        RUN 6               ' get updated RTC data
      ENDIF
    
    RUN 1
    

    That code is intended to jump to Slot 6 if the clock has been recently read (which will be a no after a restart).

    I put the clock initialization code into Slot 6, then jump to Slot 1 to begin the main code.

    My main code is in Slots 1 thru 5. I have the "PUT" line of code at the end of each Slot's code before the RUN statement that directs it to the next Slot. See that code for Slot 1 below:
    PUT Clk_rd,0               'reset clkrun to 0 after successful read
    RUN 2
    

    A similar statement at the end of Slot 5 jumps back to Slot 1.

    However, my code is still waiting for the "Press 1, 2 or 3" on the DEBUG screen.


    Question 1: Is the following code snippet necessary for my purpose? Specifically the "PUT Restart, 5" part?
    GET Restart,fetch
      IF fetch = 0 THEN       ' if "0", show that system is restarting
        SEROUT prnt16,prntbaud,[STR leng\3,"   Controller Reset  "]  'use in permanent program
    '    SEROUT prnt16,prntbaud,["  System Restart  ",CR,LF]  ' for testing without printer
        PUT Restart,5
      ENDIF
    

    Question 2: How is the clkrun variable initially set? I assume clkrun will be a 0 when the code is first run.

    falcon
  • stamptrolstamptrol Posts: 1,731
    edited 2011-09-10 05:10
    falcon,

    Glad to see you're still at it!

    Qu1: The code was used to give the operator a clue that the system had restarted. Without the SEROUT, you still need the GET and PUT for proper operation.

    Qu2: It is initially at 0 which lets the system immediately go and read the clock. If its not zero,(set every time clock is read) it means the system is running and ready to read the barcodes, etc. After each barcode is read, it gets set back to 0 so the clock is re-read before the next barcode is scanned.

    Cheers
  • FalconFalcon Posts: 191
    edited 2011-09-10 07:59
    stamptrol wrote: »
    falcon,

    Glad to see you're still at it!

    Qu1: The code was used to give the operator a clue that the system had restarted. Without the SEROUT, you still need the GET and PUT for proper operation.

    Qu2: It is initially at 0 which lets the system immediately go and read the clock. If its not zero,(set every time clock is read) it means the system is running and ready to read the barcodes, etc. After each barcode is read, it gets set back to 0 so the clock is re-read before the next barcode is scanned.

    Cheers

    Stamptrol,
    Won't the Clkrun variable stored in the Clk_rd (Con 105) location be lost during a power loss since it is saved in a scratchpad RAM location? If so, that would place a 0 in Clkrun every time power is lost, sending the program to the RTD initialization code in Slot 6.

    falcon
  • stamptrolstamptrol Posts: 1,731
    edited 2011-09-10 11:42
    In my use of the rtc, the setting routine is operator initiated. You can see that in the code where a pushbutton is watched.The clock keeps running during the power failure with the battery.

    On a power failure, the system will see the 0, go get current time and return. It only goes to the time set routine if the operator has signaled that he wants to set the clock.

    Cheers,
  • FalconFalcon Posts: 191
    edited 2011-09-17 09:04
    With off-line help of Stamptrol, I was able to get my RTC to skip the initialization routine after reading from the EPROM that the time was still "set". I also added a switch to choose to bypass the RTC initialization routine as necessary.

    A huge value of this forum is receiving help with a question, but, as in this case, I still had to work through the solution to get it to work in my application.

    Thanks again to all that made suggestions.

    falcon
  • The ds1302 does not keep time using with 2.4 tft clock but I have solved it by modifying the code now it is keeping time after unpluging from power supply.
  • This is a cold thread (9 years old), but welcome anyway, @mshafibhat
  • mshafibhat wrote: »
    The ds1302 does not keep time using with 2.4 tft clock but I have solved it by modifying the code now it is keeping time after unpluging from power supply.

    Could you share how you solved it?
Sign In or Register to comment.