Shop OBEX P1 Docs P2 Docs Learn Events
BS2px and DS1302 — Parallax Forums

BS2px and DS1302

Andy McLeodAndy McLeod Posts: 40
edited 2008-02-04 00:15 in BASIC Stamp
Has anyone had problems using the following standard PBASIC code for the DS1302 on the BS2px?

'   {$STAMP BS2px}
'   {$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.
' This code will work on all BS2 Stamp Modules.
'
' Pin Assignments
' 11   DS1302 DataIO
' 12   DS1302 Clock
' 10   DS1302 Chip Select


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

DataIO          PIN     6               ' DS1302.6
Clock           PIN     7               ' DS1302.7
CS1302          PIN     5               ' 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] 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 = WPr0                         ' Clear Write Protect
  GOSUB RTC_Out                         ' Send Command


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

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


' -----[noparse][[/noparse] Subroutines ]-----------------------------------------------------

Set_Mode:
  DEBUG CLS                             ' Clear The DEBUG Screen
  DEBUG CRSRXY, 0, 0, "Enter 2 digit year, i.e. 05 = 2005:", CR
  DEBUGIN HEX2 year                     ' Set Year
  DEBUG CRSRXY, 0, 1, "Enter 2 digit month, i.e. 03 = March:", CR
  DEBUGIN HEX2 month                    ' Set Month
  DEBUG CRSRXY, 0, 2, "Enter 2 digit date, i.e. 02 = 2nd:", CR
  DEBUGIN HEX2 date                     ' Set Date
  DEBUG CRSRXY, 0, 3, "Enter day of the week as a number 1-7", CR
  DEBUG CRSRXY, 0, 4, "1=SUN, 2=MON, 3=TUE, 4=WED, 5=THU, 6=FRI, 7=SAT", CR
  DEBUGIN HEX1 day                      ' Set Day
  DEBUG CRSRXY, 0, 5, "Enter 1 for 12 Hour Mode or 0 for 24 Hour Mode:", CR
  DEBUGIN HEX1 modeFlag
  IF modeFlag = 0 THEN
    DEBUG CRSRXY, 0, 6, "Enter 2 digit hour, 00-23:", CR
    DEBUGIN HEX2 hrs                    ' Set Hours (24 Hour Mode)
  ELSE
    DEBUG CRSRXY, 0, 6, "Enter 2 digit hour, 01-12:", CR
    DEBUGIN HEX2 hrs                    ' Set Hours (12 Hour Mode)
    DEBUG CRSRXY, 0, 7, "Enter 0 for AM, 1 for PM:", CR
    DEBUGIN HEX1 ampmFlag               ' Set AM/PM Flag
  ENDIF
  DEBUG CRSRXY, 0, 8, "Enter 2 digit minutes, 00-59:", CR
  DEBUGIN HEX2 mins                     ' Set Minutes


  ' Setting clockMode to modeFlag will effectively
  '  set or clear BIT7 in the hrs variable.

  clockMode = modeFlag


  ' Setting ampm to ampmFlag will effectively set BIT5 in the
  ' hrs variable to the proper value.
  ' This must only be done when modeFlag is set (12 Hour Mode),
  ' otherwise you can destroy hours above 19 in 24 Hour Mode.

  IF modeFlag = 1 THEN ampm = ampmFlag

  DEBUG CRSRXY, 0, 9, "Press ENTER to set time.", CR
  DEBUGIN work
  RETURN                                ' Send Time/Date To DS1302

RAM_Mode:
  DO
    DEBUG CLS, "Enter an address from 0-30 (>31 Exits):", CR
    DEBUGIN DEC2 index                  ' Address in DS1302 RAM
    IF index > 30 THEN EXIT             ' Exit if out of range
    DEBUG CRSRXY, 0, 1, "Enter # from 0-255 to store at ", DEC index, CR
    DEBUGIN DEC3 ioByte                 ' Value to store in DS1302 RAM
    DEBUG CRSRXY, 0, 2, "Storing ", DEC ioByte, " at ", DEC index, CR


  ' The address to store data in the DS1302 RAM is obtained by OR-ing
  ' the command byte with the address shifted left one bit.  This is
  ' because the command byte uses BIT0, BIT6 and BIT7.  The address
  ' occupies BIT1 through BIT5.  You are effectively putting the address
  ' in BIT1-BIT5 of reg.
  ' Write RAM command = 11xxxxx0 (Uses 3 bits)
  ' Read  RAM command = 11xxxxx1 (Uses 3 bits)
  ' xxxxx = address (Uses 5 bits)

    reg = WrRam | (index << 1)          ' RAM Write Mode + Address
    GOSUB RTC_Out                       ' Send reg + ioByte

    ioByte = 0                          ' Clear Data Variable
    reg = RdRam | (index << 1)          ' RAM Read Mode + Address
    GOSUB RTC_In                        ' Get Data From Address

    DEBUG CRSRXY, 0, 3, "Address ", DEC index, " contains ", DEC ioByte, CR
    DEBUG CRSRXY, 0, 4, "Press ENTER.", CR
    DEBUGIN work
  LOOP
  RETURN

Show_Time:
  DEBUG HOME                            ' DEBUG Home Position
  DEBUG "RESET Stamp to return to menu.", CR
  LOOKUP (day - 1), [noparse][[/noparse]Sun, Mon, Tue, Wed, Thu, Fri, Sat], work

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


  ' When reading the DS1302's clock data, BIT7 will be set when the RTC
  ' is in 12 Hour Mode.  We need to save this information, but we must
  ' then clear this bit so it doesn't interfere with the display.  Since
  ' the data is in BCD format, leaving BIT7 set would give us invalid
  ' value to display.  If the DS1302 is in 12 Hour Mode then we must
  ' also save the status of BIT5, which is the AM/PM bit, however, once
  ' again we must clear this Bit so our digits are valid for display.
  ' If the DS1302 is in 24 Hour Mode we must LEAVE BIT5 alone, since it
  ' is used when the hours are greater than 19.
  ' These bits remain at their original value on the DS1302, and will
  ' be reset on each read.

  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 clockMode is cleared, then the modeFlag will be 0 and the
  ' time will display in 24 hour format.

  ' If clockMode is set, then the modeFlag will be 1 and the
  ' time will display in 12 Hour format with an AM/PM suffix.

  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, 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



When I attempt to run this, the debug window will remain stable for a few seconds and then start randomly destroying the menu. I can rapidly enter values to set time, but when it should write to the stamp, none of the values appear on a read time test when I reset. Also, I cannot set the stamp to talk to the debug window at 9600 baud. Without exception it appears to default to 19200. Has anyone seen these behaviors?

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-02-02 02:31
    Andy -

    I've never used that program, so I can't help you there. The default baud rate for the BS-2px is indeed 19.2 KBPS.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    There is no pleasure in having nothing to do;
    the fun is in having lots to do, and not doing it!
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2008-02-04 00:15
    Andy McLeod

    I have used this before and when· you input the time date is fine but when you enter the day of the week is where it can mess you up

    2008········ Year
    02············ Month
    03············ Day
    1···············Day of the Week
    0·············· OR 1········· 12 OR 24
    10············· hrs
    1··············· pm
    20············· mins

    But this is only shown in the debug screen at the top where it is white

    When I attempt to run this, the debug window will remain stable for a few seconds and then start randomly destroying the menu. I can rapidly enter values to set time, but when it should write to the stamp, none of the values appear on a read time test when I reset.

    It will all run together on the debug screen and you do not think that you have it set

    One more thing only hit the reset for the Basic Stamp and· keep the power to the the DS1302 chip unless you have a battery back up hook up to the DS1302 chip other wis you lose the time
    ·
    If you do not enter it in the right way none of it will be set and you have to start over again

    I hope this helps you out

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

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 2/4/2008 12:32:45 AM GMT
Sign In or Register to comment.