Shop OBEX P1 Docs P2 Docs Learn Events
DS1302 backup power — Parallax Forums

DS1302 backup power

RickMRickM Posts: 10
edited 2008-03-28 20:00 in BASIC Stamp
I have a DS 1302 connected to a BS2. Works great, buzzer buzzes on the hour as its supposed to do.
If i use the battery backup power to the DS1302, AND the BS2 looses power, dont i have to reset
the proper time and date? (because the BS2 will try to reset the time and date from the Pbasic program.)
Thanx
Rick

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2008-03-25 18:33
    No, if the DS1302 has battery backup, and you've enabled it properly, the DS1302 will continue to 'keep time' for 5 years.

    So you might use an eeprom setting in the BS2 (or a memory location in the DS1302) to indicate the time has already been 'set' accurately, so the BS2 program doesn't require you to enter what the correct time is every time the power goes off and the BS2 resets.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2008-03-25 19:10
    RickM

    Please post the code that you are using

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

    ·
    ·
    ·
    ·
    Sam
  • RickMRickM Posts: 10
    edited 2008-03-25 19:43
    I can post code tomorrow, its on a machine at home
    rick
  • RickMRickM Posts: 10
    edited 2008-03-26 01:04
    '

    ' {$STAMP BS2}

    'BASIC Stamp II (BS2-IC) Program Listing #1

    '***************************************************************************

    '* Title: DS1302_1.BS2 Author: Jeff A Martin Date: 5/18/98 *

    '* *

    '* Description: This BASIC Stamp II program interfaces to the Dallas Semi. *

    '* DS1302 Real Time Clock (RTC) chip. The date and time is *

    '* read and displayed in long and short formats on the debug *

    '* screen. *

    '* *

    '* Notes: This program can be modified to fit into a smaller code space. *

    '* It is not written as compact as possible to make it more readable *

    '* and to demonstrate all the useful functions of the chip. *

    '* The DS1302 features seconds, minutes, hours (AM/PM-12/24 modes), *

    '* date of month, month, day of week and year time-keeping with *

    '* leap year compensation valid up to 2100. Scratchpad RAM memory *

    '* (31 bytes), single-byte and multi-byte reads and writes, software *

    '* clock-halt, software write-protection, trickle charge and *

    '* operation down to 2.0 volts @ 300 nA are other notable features. *

    '***************************************************************************

    ' http://www-eng.lbl.gov/~lafever/Parallax/Source_Code/Application_Kits/DS1302_Real_Time_Clock/DS1302_1.bs2







    'Define I/O pins and RTC variables

    Clk CON 0

    Dta CON 1

    RTCCS CON 2

    RTCCmd VAR Byte

    Value VAR Byte

    Seconds VAR Byte

    Minutes VAR Byte

    Hours VAR Byte

    Date VAR Byte

    Month VAR Byte

    Day VAR Byte

    Year VAR Byte

    Idx VAR Byte



    'Define RTC Command Constants

    SecReg CON %00000

    MinReg CON %00001

    HrsReg CON %00010

    DateReg CON %00011

    MonReg CON %00100

    DayReg CON %00101

    YrReg CON %00110

    CtrlReg CON %00111

    TChgReg CON %01000

    BrstReg CON %11111



    'Define Days-Of-Week, Months and AM/PM text.

    'All text is stored in EEPROM with a binary 0 as the end-of-text character

    Sun DATA "Sun",0

    Mon DATA "Mon",0

    Tue DATA "Tues",0

    Wed DATA "Wednes",0

    Thu DATA "Thurs",0

    Fri DATA "Fri",0

    Sat DATA "Satur",0



    Jan DATA "January",0

    Feb DATA "February",0

    Mar DATA "March",0

    Apr DATA "April",0

    May DATA "May",0

    Jun DATA "June",0

    Jul DATA "July",0

    Aug DATA "August",0

    Sep DATA "September",0

    Oct DATA "October",0

    Nov DATA "November",0

    Dcm DATA "December",0



    AM DATA " AM",0

    PM DATA " PM",0



    'Set I/O pin states and directions

    OUTS = %0000000000000000 'All logic low

    DIRS = %0000000000000111 'I/O 0,1 and 2 are output, rest are input



    Initialize:

    'Set Time and Date to 05/18/98 - 3:00 PM

    'NOTE: Date must be set only once for every power-up of DS1302 chip.

    Day = $06 'Friday ENTER A NUMBER HERe

    Month = $03 'July

    Date = $16 '06

    Year = $08 '2007

    Hours = $09 '09 AM (in 24-hour mode)

    Minutes = $33

    Seconds = $00

    GOSUB SetTimeAndDate



    Loop:

    'Read out all date and time values and display them in two formats on

    'the debug screen.

    GOSUB ReadRTCBurst

    'DEBUG HOME,"LONG FORMAT DATE AND TIME:",CR

    'GOSUB PrintLongDate

    GOSUB Print12HourTime

    'DEBUG CR,CR,"SHORT FORMAT DATE AND TIME:",CR

    'GOSUB PrintShortDate

    GOSUB Checkit

    'DEBUG CLS

    'GOSUB Print24HourTime

    'DEBUG CR

    GOTO Loop
    '
    Checkit:
    ' Lawn Sprinkler On / Off code
    'IF (Month = $07 AND Date = $06 AND Hours = $09 AND Minutes = $08) THEN SprinklerOn
    'IF (Month = $07 AND Date = $06 AND Hours = $09 AND Minutes = $15)THEN SprinklerOff
    'IF (Month = $07 AND Date = $11 AND Hours = $18 AND Minutes = $00) THEN SprinklerOn
    'IF (Month = $07 AND Date = $11 AND Hours = $18 AND Minutes = $30)THEN SprinklerOff
    'IF (Hours = $18 AND Minutes = $55) THEN SprinklerOn
    'IF (Hours = $18 AND Minutes = $56)THEN SprinklerOff

    ' Exterior Christamas Lights
    'IF (Hours = $16 AND Minutes = $30) THEN SprinklerOn
    'IF (Hours = $23 AND Minutes = $58)THEN SprinklerOff

    'IF (Hours = $17 AND Minutes = $00) THEN SprinklerOn
    'IF (Hours = $17 AND Minutes = $20)THEN SprinklerOff

    '
    my current code starts here
    HIGH 14 'LED on pin 14
    PAUSE 300
    LOW 14
    PAUSE 300
    IF ( Minutes = $00 AND Seconds = $00 ) THEN Sound1
    IF ( Minutes = $15 OR Minutes = $30 OR Minutes = $45 AND Seconds = $00 ) THEN Sound2
    RETURN

    '
    Sound1:
    FREQOUT 9, 1500, 2000
    FREQOUT 9, 1500, 2000
    RETURN

    Sound2:
    FREQOUT 9, 1500, 2000
    RETURN

    SprinklerOn:
    DEBUG CLS
    DEBUG " Sprinkler On "
    DEBUG CR
    HIGH 15 'pin15 high as output
    SLEEP 1
    RETURN
    '
    SprinklerOFF:
    DEBUG CLS
    DEBUG " Sprinkler Off"
    DEBUG CR
    LOW 15 'pin15 low as output
    SLEEP 1
    RETURN
    '

    '==================== DS1302 Real-Time Clock Subroutines ===================

    PrintLongDate:

    'Print long date format on debug screen

    LOOKUP Day-1,[noparse][[/noparse]Sun,Mon,Tue,Wed,Thu,Fri,Sat],Idx

    GOSUB PrintIt

    DEBUG "day, "

    LOOKUP Month-1,[noparse][[/noparse]Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dcm],Idx

    GOSUB PrintIt

    'NOTE: The following line prints the proper 4-digit year for the years

    '1990 through 2089

    DEBUG " ",HEX2 Date,", ",DEC2 20-(Year/90),HEX2 Year, CR

    RETURN



    PrintShortDate:

    'Print short date format on debug screen

    DEBUG HEX2 Month,"/",HEX2 Date,"/",HEX2 Year, CR

    RETURN



    Print12HourTime:

    'Print 12-hour time format on debug screen

    'NOTE: The DS1302 has 12 and 24 hour time-keeping modes (bit 7 of HrsReg

    'sets 12/24 mode and bit 5 indicates AM/PM or 20+ hours). For purposes

    'of this example, we're using 24 hour mode only, and converting it to

    '12-hour in the next two lines below.

    DEBUG DEC2 12-(24-(Hours.HIGHNIB*10+Hours.LOWNIB)//12),":",HEX2 Minutes,":",HEX2 Seconds

    LOOKUP Hours/$12,[noparse][[/noparse]AM,PM],Idx

    GOSUB PrintIt

    RETURN



    Print24HourTime:

    'Print 24-hour time format on debug screen

    DEBUG HEX2 Hours,":",HEX2 Minutes,":",HEX2 Seconds

    RETURN



    PrintIt:

    'Prints zero (0) terminated text from EEPROM

    READ Idx,Value 'Get next character

    IF Value = 0 THEN Finished 'Make sure it's not a binary 0

    DEBUG Value 'Display it on screen

    Idx = Idx + 1

    GOTO PrintIt

    Finished:

    RETURN



    WriteRTCRAM:

    'Write to DS1302 RAM Register

    HIGH RTCCS

    SHIFTOUT Dta, Clk, LSBFIRST, [noparse][[/noparse]%0\1,RTCCmd\5,%11\2,Value]

    LOW RTCCS

    RETURN



    WriteRTC:

    'Write to DS1302

    HIGH RTCCS

    SHIFTOUT Dta, Clk, LSBFIRST, [noparse][[/noparse]%0\1,RTCCmd\5,%10\2,Value]

    LOW RTCCS

    RETURN



    ReadRTCBurst:

    'Read all time-keeping registers in one burst

    HIGH RTCCS

    SHIFTOUT DTA, Clk, LSBFIRST, [noparse][[/noparse]%1\1,BrstReg\5,%10\2]

    SHIFTIN DTA, Clk, LSBPRE, [noparse][[/noparse]Seconds,Minutes,Hours,Date,Month,Day,Year]

    LOW RTCCS

    RETURN



    ReadRTCRAM:

    'Read DS1302 RAM Register

    HIGH RTCCS

    SHIFTOUT DTA, Clk, LSBFIRST, [noparse][[/noparse]%1\1,RTCCmd\5,%11\2]

    SHIFTIN DTA, Clk, LSBPRE, [noparse][[/noparse]Value]

    LOW RTCCS

    RETURN



    SetTimeAndDate:

    'Write time values into all time-keeping registers, being sure to clear

    'the write-protect bit in CtrlReg before the write, and set the

    'write-protect bit after the write

    FOR Idx = 0 TO 8

    LOOKUP Idx,[noparse][[/noparse]0,Seconds,Minutes,Hours,Date,Month,Day,Year,128],Value

    LOOKUP Idx,[noparse][[/noparse]CtrlReg, SecReg, MinReg, HrsReg, DateReg, MonReg, DayReg, YrReg, CtrlReg],RTCCmd

    GOSUB WriteRTC

    NEXT

    RETURN
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2008-03-26 20:47
    RickM

    If you do not want the time to reset when you turn power back
    ON to the Basic Stamp then " ' "·comment these line out

    That should take care the problem of resetting the clock

    Initialize:

    'Set Time and Date to 05/18/98 - 3:00 PM

    'NOTE: Date must be set only once for every power-up of DS1302 chip.

    ·
    This will run every time you power up the Basic Stamp from a power loss
    unless you do this to this routine

    ·'·Day = $06 'Friday ENTER A NUMBER HERE

    ·' Month = $03 'July

    ·' Date = $16 '06

    ·' Year = $08 '2007

    ·' Hours = $09 '09 AM (in 24-hour mode)

    ·' Minutes = $33

    ·' Seconds = $00

    ·' GOSUB SetTimeAndDate

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

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 3/26/2008 8:57:05 PM GMT
  • RickMRickM Posts: 10
    edited 2008-03-27 14:11
    If I comment out these lines, how do I initially set the time and date.
    I could do a·once through loop, but if power is lost, then it will run the
    once through loop and reset the time.
    Thanx
    Rick·
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2008-03-27 14:26
    transdot.gif
    RickM


    You·ONLY need this routine if you lose power·only to the DS1302 Time Chip

    This·NOT need if you only lose Power to the Basic Stamp


    ·Day = $06 'Friday ENTER A NUMBER HERE

    · Month = $03 'July

    · Date = $16 '06

    · Year = $08 '2007

    · Hours = $09 '09 AM (in 24-hour mode)

    · Minutes = $33

    · Seconds = $00

    · GOSUB SetTimeAndDate




    I initially set the time and date.

    If you have to reset the Time And Date then you will have to UN comment

    These line above And load the Basic Stamp One Time



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

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 3/27/2008 2:33:40 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-03-27 18:53
    If you check out the Binary Digital Clock posted in the Completed Projects Forum you will see that I have a program for setting the time initially, and then another which displays it…It doesn’t lose the time between because the system stays powered and is also backed-up as well.

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

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • RickMRickM Posts: 10
    edited 2008-03-28 13:45
    I think i get it, if the DS1302 has backup power, and the power fails to the BS2, when the BS2 regains power
    it will igore this loop until a button is pressed (and use the time from the DS1302 )
    Is this correct?
    Thanx in advance
    Rick

    Start:
    DO ' DEBUG Menu
    DEBUG CLS ' Clear The DEBUG Screen
    DEBUG CRSRXY, 0, 0, "Press 1 to Set Date/Time.", CR
    DEBUG CRSRXY, 6, 1, "2 to Display Date/Time.", CR
    DEBUG CRSRXY, 6, 2, "3 to Set/Read DS1302 RAM.", CR
    DEBUG CRSRXY, 6, 4, "RESET Stamp to return to menu.", CR
    DEBUGIN DEC1 work ' Get 1 Number
    IF work = 1 THEN
    GOSUB Set_Mode
    GOSUB Set_Time
    ELSEIF work = 2 THEN
    DEBUG CLS ' Clear The DEBUG Screen
    DO
    GOSUB Get_Time ' Get The Current Date/Time
    GOSUB Show_Time ' Display It
    LOOP
    ELSEIF work = 3 THEN
    GOSUB RAM_Mode
    ENDIF
    LOOP
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-03-28 20:00
    Rick,

    If your code doesn’t have a facility to set the date time in it on the fly then you should run a different program to set the time before downloading the main program. The main program should not be forcing the time to a set value each time you run it.

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