Shop OBEX P1 Docs P2 Docs Learn Events
RTC help — Parallax Forums

RTC help

JSGrewalJSGrewal Posts: 9
edited 2008-07-08 11:39 in BASIC Stamp
So Im messing around with the DS1302 RTC. Im using the DS1302 template found in this forum. I want to make a pin high if the minutes are below 15. Im having trouble figuring out the syntax to make this happen. The line Im having issues with is:

IF HEX2 mins<15 THEN HIGH 13 ELSE LOW 13 ' make pin 13 high if minutes are 0-15

Obviously Im new, be gentle... Thanks!


Code:

' =========================================================================
'
' File...... DS1302_Template.bs2
' Purpose... Template For DS1302 Clock & RAM Functions
' Author.... Chris Savage -- Parallax, Inc.
' E-mail.... csavage@parallax.com
' Started...
' Updated... 04-26-2006
'
' {$STAMP BS2}
' {$PBASIC 2.5}
'
' =========================================================================


'
[noparse][[/noparse] Program Description ]

' This is a template for using the DS1302 in your own program. The I/O pin
' declarations, constants, variables and subroutines are already in place
' for you to use. Please see the DS1302_Demo.bs2 code for a more detailed
' explanation of how each section works. You can remove what you don't use
' without issue.


'
[noparse][[/noparse] Revision History ]



'
[noparse][[/noparse] I/O Definitions ]

DataIO PIN 8 ' DS1302.6
Clock_RTC PIN 7 ' DS1302.7
CS1302 PIN 9 ' DS1302.5


'
[noparse][[/noparse] Constants ]

WrSecs CON $80 ' Write Seconds
RdSecs CON $81 ' Read Seconds
WrMins CON $82 ' Write Minutes
RdMins CON $83 ' Read Minutes
WrHrs CON $84 ' Write Hours
RdHrs CON $85 ' Read Hours
CWPr CON $8E ' Write Protect Register
WPr1 CON $80 ' Set Write Protect
WPr0 CON $00 ' Clear Write Protect
WrBurst CON $BE ' Write Burst Of Data
RdBurst CON $BF ' Read Burst Of Data
WrRam CON $C0 ' Write RAM Data
RdRam CON $C1 ' Read RAM Data
Hr24 CON 0 ' 24 Hour Mode
Hr12 CON 1 ' 12 Hour Mode
modeFlag CON 0 ' Force 24 hour mode

'
[noparse][[/noparse] Variables ]

index VAR Byte ' Loop Counter
reg VAR Byte ' Read/Write Address
ioByte_RTC VAR Byte ' Data To/From DS1302
secs VAR Byte ' Seconds
secs01 VAR secs.LOWNIB
secs10 VAR secs.HIGHNIB
mins VAR Byte ' Minutes
mins01 VAR mins.LOWNIB
mins10 VAR mins.HIGHNIB
hrs VAR Byte ' Hours
hrs01 VAR hrs.LOWNIB
hrs10 VAR hrs.HIGHNIB
date VAR Byte
month VAR Byte
day VAR Nib ' Day
year VAR Byte ' Year

ampm VAR hrs.BIT5 ' AM/PM Flag Bit
clockMode VAR hrs.BIT7 ' 12/24 Hour Mode Bit
ampmFlag VAR Bit ' 0 = AM, 1 = PM
'modeFlag VAR Bit ' 0 = 24, 1 = 12 (Hours)

work VAR Byte ' Work Data


'
[noparse][[/noparse] EEPROM Data ]

Sun DATA "SUN", 0 ' Day Abbreviations
Mon DATA "MON", 0
Tue DATA "TUE", 0 ' These data statements could
Wed DATA "WED", 0 ' contain the full day name and
Thu DATA "THU", 0 ' the code would still work
Fri DATA "FRI", 0 ' without change.
Sat DATA "SAT", 0


'
[noparse][[/noparse] Initialization ]

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


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

Start:
DO ' Place your main routines here
GOSUB Get_Time ' Get The Current Date/Time
GOSUB Show_Time ' Display It

'IF HEX2 mins<15 THEN HIGH 13 ELSE LOW 13 ' make pin 13 high if minutes are 0-15
PAUSE 1000
LOOP


'
[noparse][[/noparse] Subroutines ]

Show_Time:
' This routine uses DEBUG to display the Date/Time information but you
' would customize the routine to display the data you want on the device
' you want. Not all information needs to be displayed.

LOOKUP (day - 1), [noparse][[/noparse]Sun, Mon, Tue, Wed, Thu, Fri, Sat], work

DO ' Display Day Abbreviation
READ work, ioByte_RTC ' Read Each Character Into ioByte_RTC
IF (ioByte_RTC = 0) THEN EXIT ' If 0 then done
DEBUG ioByte_RTC ' Send Character To DEBUG Screen
work = work + 1 ' Increment Address
LOOP ' Next Character, If 0 Then Done

'modeFlag = clockMode ' Save 12/24 Hour Clock Mode
clockMode = 0 ' Clear BIT7
ampmFlag = ampm ' Save AM/PM Flag

IF modeFlag = 1 THEN ampm = 0 ' Clear BIT5 (12 Hour Mode only!)

DEBUG " ", HEX2 month, "/", HEX2 date, "/", HEX2 year, " "

IF (modeFlag = Hr24) THEN ' Check For 24 Hour Display mode
DEBUG HEX2 hrs, ":", HEX2 mins, ":", HEX2 secs
ELSE
DEBUG HEX2 hrs, ":", HEX2 mins ' Print Hours/Minutes
IF ampmFlag = 0 THEN
DEBUG "AM"
ELSE
DEBUG "PM"
ENDIF

DEBUG " [noparse][[/noparse]", HEX2 secs, "]"
ENDIF
RETURN

RTC_Out:
HIGH CS1302 ' Select DS1302
SHIFTOUT DataIO, Clock_RTC, LSBFIRST, [noparse][[/noparse]reg, ioByte_RTC]
LOW CS1302 ' Deselect DS1302
RETURN

RTC_In:
HIGH CS1302 ' Select DS1302
SHIFTOUT DataIO, Clock_RTC, LSBFIRST, [noparse][[/noparse]reg]
SHIFTIN DataIO, Clock_RTC, LSBPRE, [noparse][[/noparse]ioByte_RTC]
LOW CS1302 ' Deselect DS1302
RETURN

Set_Time: ' DS1302 Burst Write
HIGH CS1302 ' Select DS1302
SHIFTOUT DataIO, clock_RTC, LSBFIRST, [noparse][[/noparse]WrBurst]
SHIFTOUT DataIO, clock_RTC, LSBFIRST, [noparse][[/noparse]secs, mins, hrs,
date, month, day, year, 0]
LOW CS1302 ' Deselect DS1302
RETURN

Get_Time: ' DS1302 Burst Read
HIGH CS1302 ' Select DS1302
SHIFTOUT DataIO, clock_RTC, LSBFIRST, [noparse][[/noparse]RdBurst]
SHIFTIN DataIO, clock_RTC, LSBPRE, [noparse][[/noparse]secs, mins, hrs, date, month, day, year]
LOW CS1302 ' Deselect DS1302
RETURN

Comments

  • JSGrewalJSGrewal Posts: 9
    edited 2008-07-07 21:31
    Add:

    I can get the minutes on the debug screen if I use:

    DEBUG HEX2 mins, CR

    I must be missing some math or a command...
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2008-07-07 21:39
    Try this Demo Code

    ' =========================================================================
    '
    '·· File...... DS1302_Demo.bs2
    '·· Purpose... Demonstrate The DS1302 Clock & RAM Functions
    '·· Author.... Chris Savage -- Parallax, Inc.
    '·· E-mail.... csavage@parallax.com
    '·· Started...
    '·· Updated... 10-09-2005
    '
    '·· {$STAMP BS2}
    '·· {$PBASIC 2.5}
    '
    ' =========================================================================

    '
    [noparse][[/noparse] Program Description ]
    ' This code demonstrates the use of the DS1302 RTC/RAM Chip with the BASIC
    ' Stamp 2 Microcontroller, making easy use of the 12 Hour & 24 Hour time
    ' modes, as well as accessing the RAM.· The sample code will use the DEBUG
    ' window for input and output for simplicity.· You can change the output
    ' to the device of your choice.· For example, using a Parallax Serial LCD
    ' requires very few changes to the code to use it for output.
    ' A modified version of this code was adapted to display the date and time
    ' on a Parallax Serial LCD Display (#27976), also with backlight (#27977)
    ' and on Parallax Professional Development Board (#28138, coming soon!)
    ' using a MAX7219 8-Digit Display Driver.
    ' This code will work on all BS2 Stamp Modules.
    '
    ' The Parallax 2 X 16 Serial LCD Displays are availble at the following
    ' links:
    ' http://www.parallax.com/detail.asp?product_id=27976
    ' http://www.parallax.com/detail.asp?product_id=27977
    '
    ' Pin Assignments
    ' 0·· DS1302 DataIO
    ' 1·· DS1302 Clock
    ' 2·· DS1302 Chip Select

    '
    [noparse][[/noparse] Revision History ]

    '
    [noparse][[/noparse] I/O Definitions ]
    DataIO········· PIN···· 0·············· ' DS1302.6
    Clock·········· PIN···· 1·············· ' DS1302.7
    CS1302········· PIN···· 2·············· ' DS1302.5

    '
    [noparse][[/noparse] Constants ]
    WrSecs········· CON···· $80············ ' Write Seconds
    RdSecs········· CON···· $81············ ' Read Seconds
    WrMins········· CON···· $82············ ' Write Minutes
    RdMins········· CON···· $83············ ' Read Minutes
    WrHrs·········· CON···· $84············ ' Write Hours
    RdHrs·········· CON···· $85············ ' Read Hours
    CWPr··········· CON···· $8E············ ' Write Protect Register
    WPr1··········· CON···· $80············ ' Set Write Protect
    WPr0··········· CON···· $00············ ' Clear Write Protect
    WrBurst········ CON···· $BE············ ' Write Burst Of Data
    RdBurst········ CON···· $BF············ ' Read Burst Of Data
    WrRam·········· CON···· $C0············ ' Write RAM Data
    RdRam·········· CON···· $C1············ ' Read RAM Data
    Hr24··········· CON···· 0·············· ' 24 Hour Mode
    Hr12··········· CON···· 1·············· ' 12 Hour Mode

    '
    [noparse][[/noparse] Variables ]
    index·········· VAR···· Byte··········· ' Loop Counter
    reg············ VAR···· Byte··········· ' Read/Write Address
    ioByte········· VAR···· Byte··········· ' Data To/From DS1302
    secs··········· VAR···· Byte··········· ' Seconds
    secs01········· VAR···· secs.LOWNIB
    secs10········· VAR···· secs.HIGHNIB
    mins··········· VAR···· Byte··········· ' Minutes
    mins01········· VAR···· mins.LOWNIB
    mins10········· VAR···· mins.HIGHNIB
    hrs············ VAR···· Byte··········· ' Hours
    hrs01·········· VAR···· hrs.LOWNIB
    hrs10·········· VAR···· hrs.HIGHNIB
    date··········· VAR···· Byte
    month·········· VAR···· Byte
    day············ VAR···· Nib············ ' Day
    year··········· VAR···· Byte··········· ' Year
    ampm··········· VAR···· hrs.BIT5······· ' AM/PM Flag Bit
    clockMode······ VAR···· hrs.BIT7······· ' 12/24 Hour Mode Bit
    ampmFlag······· VAR···· Bit············ ' 0 = AM, 1 = PM
    modeFlag······· VAR···· Bit············ ' 0 = 24, 1 = 12 (Hours)
    work··········· VAR···· Byte··········· ' Work Data

    '
    [noparse][[/noparse] Initialization ]
    Init:
    · reg = CWPr··························· ' Initialize DS1302
    · ioByte = WPr0························ ' Clear Write Protect
    · GOSUB RTC_Out························ ' Send Command

    '
    [noparse][[/noparse] Program Code ]
    ··· Look at this part
    Start:
    mins = $00
    secs = $00
    GOSUB Set_Time
    DO
    · GOSUB Get_Time
    · DEBUG HOME, HEX2 mins, ":", HEX2 secs
    · IF mins = $03 AND secs = $15 THEN EXIT
    LOOP
    DEBUG CR, CR, "3:15 Elapsed"
    STOP

    ^^^^^^^^
    '
    [noparse][[/noparse] Subroutines ]

    RTC_Out:
    · HIGH CS1302·························· ' Select DS1302
    · SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]reg, ioByte]
    · LOW CS1302··························· ' Deselect DS1302
    · RETURN
    RTC_In:
    · HIGH CS1302·························· ' Select DS1302
    · SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]reg]
    · SHIFTIN DataIO, Clock, LSBPRE, [noparse][[/noparse]ioByte]
    · LOW CS1302··························· ' Deselect DS1302
    · RETURN
    Set_Time:······························ ' DS1302 Burst Write
    · HIGH CS1302·························· ' Select DS1302
    · SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]WrBurst]
    · SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]secs, mins, hrs,
    ··· date, month, day, year, 0]
    · LOW CS1302··························· ' Deselect DS1302
    · RETURN
    Get_Time:······························ ' DS1302 Burst Read
    · HIGH CS1302·························· ' Select DS1302
    · SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]RdBurst]
    · SHIFTIN DataIO, Clock, LSBPRE, [noparse][[/noparse]secs, mins, hrs, date, month, day, year]
    · LOW CS1302··························· ' Deselect DS1302
    · RETURN

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

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 7/7/2008 9:46:59 PM GMT
  • JSGrewalJSGrewal Posts: 9
    edited 2008-07-07 22:00
    Thanks for the help Sam!

    Looks like all I need was to change:
    IF HEX2 mins<15 THEN HIGH 13 ELSE LOW 13 ' make pin 13 high if minutes are 0-15

    to:
    IF mins<$15 THEN HIGH 13 ELSE LOW 13 ' make pin 13 high if minutes are 0-15

    Works like I wanted now. Not exactly sure what the $ does, but it does what I want.

    Thanks again!
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2008-07-08 11:39
    A few thing that will make thing easier

    #1

    Instead of using low 13 and high 13 use a label like relay for pin 13 it would be

    Relay·· PIN· ·13


    Here is the example

    '
    [noparse][[/noparse] I/O Definitions ]
    DataIO········· ·PIN···· 0·············· ' DS1302.6
    Clock··········· ·PIN···· 1·············· ' DS1302.7
    CS1302········· PIN···· 2·············· ' DS1302.5
    Relay··········· ·PIN···· 13


    #2

    DO
    · GOSUB Get_Time
    · DEBUG HOME, HEX2 mins, ":", HEX2 secs
    · IF mins = $00 THEN HIGH relay ELSE LOW relay
    LOOP

    ·
    You can do it this way but there others way to do it as well

    ·DO
    · GOSUB Get_Time
    · DEBUG HOME, HEX2 mins, ":", HEX2 secs
    · IF mins < $15 THEN
    · HIGH relay
    · ELSE
    · LOW relay
    · ENDIF
    LOOP


    OR


    DO
    · GOSUB Get_Time
    · DEBUG HOME, HEX2 mins, ":", HEX2 secs
    · IF mins = $00 THEN
    · HIGH relay
    · IF mins = $15 THEN
    · LOW relay
    · ENDIF
    · ENDIF
    LOOP


    ·The $ is for HEX values·and you will need to·use it for this Demo Code that I
    Posted


    ·

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

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 7/8/2008 12:16:59 PM GMT
Sign In or Register to comment.