Shop OBEX P1 Docs P2 Docs Learn Events
Experiment #33 in StampWorks Manual (DS1307 RTC) need code for updating Year an — Parallax Forums

Experiment #33 in StampWorks Manual (DS1307 RTC) need code for updating Year an

MigsMigs Posts: 95
edited 2010-07-18 11:16 in BASIC Stamp
Friends:

I have set up and run Experiment #33 in the StampWorks Manual involving the Professional Development Board and the DS1307 Real Time Clock, a BS2P40 Module and software SW21-EX33-DS1307.BSP

' =========================================================================
'
'   File....... SW21-EX33-DS1307.BSP
'   Purpose.... Real-time-clock interfacing
'   Author..... (C) 2000 - 2005, Parallax, Inc.
'   E-mail..... support@parallax.com
'   Started....
'   Updated.... 16 AUG 2006
'
'   {$STAMP BS2p}
'   {$PBASIC 2.5}
'
' =========================================================================


' -----[noparse][[/noparse] Program Description ]---------------------------------------------
'
' This program demonstrates the access and control of an external real-
' time-clock chip, the DS1307.


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

SDA             PIN     0                       ' I2C serial data line
SCL             PIN     1                       ' I2C serial clock line

BtnBus          VAR     INB                     ' four inputs, pins 4 - 7


' -----[noparse][[/noparse] Constants ]-------------------------------------------------------

Ack             CON     0                       ' acknowledge bit
Nak             CON     1                       ' no ack bit

DS1307          CON     %1101 << 4


' -----[noparse][[/noparse] Variables ]-------------------------------------------------------

secs            VAR     Byte                    ' DS1307 time registers
mins            VAR     Byte
hrs             VAR     Byte
day             VAR     Byte                    ' weekday
date            VAR     Byte                    ' day in month, 1 - 31
month           VAR     Byte
year            VAR     Byte
control         VAR     Byte                    ' SQW I/O control

btns            VAR     Nib                     ' debounced button inputs
btnBack         VAR     btns.BIT3               ' roll back
btnDay          VAR     btns.BIT2               ' +/- day
btnHr           VAR     btns.BIT1               ' +/- hours
btnMn           VAR     btns.BIT0               ' +/- minutes

idx             VAR     Nib                    ' loop control
pntr            VAR     Byte                    ' ee pointer
char            VAR     Byte                    ' character for display


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

DayNames        DATA    "SunMonTueWedThuFriSat"


' -----[noparse][[/noparse] Initialization ]--------------------------------------------------

Reset:
  #IF ($STAMP < BS2P) #THEN
    #ERROR "Please use BS2 version: SW21-EX33-DS1307.BS2"
  #ENDIF

Setup:
  DEBUG CLS,
        "DS1307 Demo", CR,
        "-----------"

Reset_Clock:
  GOSUB Get_Buttons                             ' scan buttons
  idx = btns & %0011                            ' isolate hrs & mins
  IF (idx = %11) THEN                           ' if both pressed, reset
    secs = $00
    mins = $00
    hrs = $06                                   ' 6:00 AM
    day = $07                                   ' Saturday
    date = $01                                  ' 1st
    month = $01                                 ' January
    year = $05                                  ' 2005
    control = 0                                 ' disable SQW output
    GOSUB Set_Clock                             ' block write clock regs
  ENDIF


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

Main:
  GOSUB Get_Clock                               ' read DS1307
  hrs = hrs & $3F
  DEBUG CRSRXY, 0, 2,
        HEX2 hrs, ":", HEX2 mins, ":", HEX2 secs, CR
  GOSUB Print_Day
  PAUSE 100

  GOSUB Get_Buttons
  IF (btns > %0000) THEN                        ' button pressed?
    IF (btns <> %1000) THEN                     ' ignore back only
      hrs = hrs.NIB1 * 10 + hrs.NIB0            ' BCD to decimal
      mins = mins.NIB1 * 10 + mins.NIB0

      IF (btnBack = 0) THEN                     ' increment values
        day = ((day - 1) + btnDay // 7) + 1     ' keep 1 - 7
        hrs = hrs + btnHr // 24                 ' keep 0 - 23
        mins = mins + btnMn // 60               ' keep 0 - 59
      ELSE
        day = ((day - 1) + (btnDay * 6) // 7) + 1
        hrs = hrs + (btnHr * 23) // 24
        mins = mins + (btnMn * 59) // 60
      ENDIF

      hrs  = (hrs / 10 << 4) + (hrs // 10)      ' decimal to BCD
      mins = (mins / 10 << 4) + (mins // 10)
      secs = $00
      GOSUB Set_Clock                           ' update DS1307
    ENDIF
  ENDIF

  GOTO Main


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

Get_Buttons:
  btns = %1111                                  ' enable all four inputs
  FOR idx = 1 TO 5
    btns = btns & ~BtnBus                       ' test inputs
    PAUSE 5                                     ' delay between tests
  NEXT
  RETURN


Print_Day:
  pntr = DayNames + ((day - 1) * 3)             ' point to 1st char
  FOR idx = 0 TO 2                              ' print 3 letters
    READ (pntr + idx), char                     ' read letter
    DEBUG char                                  ' print it
  NEXT
  RETURN


' Do a block write to clock registers

Set_Clock:
  I2COUT SDA, DS1307, 0, [noparse][[/noparse]STR secs\8]           ' update clock registers
  RETURN


' Do a block read from clock registers

Get_Clock:
  I2CIN SDA, DS1307, 0, [noparse][[/noparse]STR secs\8]            ' retrieve clock registers
  RETURN




1. Does anyone have modified code for displaying and updating with the complete 4 digit year? (and the wiring schematic?)

2. Does anyone have code for writing this information to the 7 Segment LED display on the Professional Development Board?

I remain most grateful,

Migs

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"Sometimes we forget that the world itself is paradise." Douglas Coupland, 'Microserfs'
"If the doors of perception were cleansed every thing would appear to man as it is, infinite" - William Blake
"We either make ourselves miserable, or we make ourselves strong. The amount of work is the same." Carlos Castaneda
"One single grateful thought raised to heaven is the most perfect prayer. " G. E. Lessing
“How much of human life is lost in waiting.” Ralph Waldo Emerson
"Men often mistake notoriety for fame, and would rather be remembered for their vices and follies than not be noticed at all.” Harry Truman
My website: www.intoku.net my e-mail:mreznicek@pretensa.com me:Miguel Reznicek

Post Edited (Migs) : 7/17/2010 10:45:41 PM GMT

Comments

  • FranklinFranklin Posts: 4,747
    edited 2010-07-18 00:18
    Where is it printing the two digit year?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • MigsMigs Posts: 95
    edited 2010-07-18 01:36
    In the Main routine in the third line it prints using DEBUG, however the year is never printed even though it is declared as a variable earlier and written to the DS1307 when the program first runs and no time data is in the chip.

    Here is an application from another post that explains why the year is not dealt with in the listing above:

    http://forums.parallax.com/forums/default.aspx?f=5&m=461713

     
    ' =========================================================================
    '
    '   File....... Date & Time.BSP
    '   Purpose.... Real-time-clock interfacing
    '   Author.....
    '   E-mail.....
    '   Started.... 09 June 2010
    '   Updated.... 14 June 2010
    '
    '   {$STAMP BS2p}
    '   {$PBASIC 2.5}
    '
    ' =========================================================================
    
    ' -----[noparse][[/noparse] Program Description ]---------------------------------------------
    '
    ' This program demonstrates the access and control of an external real-
    ' time-clock chip, the DS1307.
    
    ' -----[noparse][[/noparse] I/O Definitions ]-------------------------------------------------
    SDA             PIN     0                       ' I2C serial data line
    SCL             PIN     1                       ' I2C serial clock line
    Spkr            PIN     2                       ' speaker output
    BtnBus          VAR     INH                     ' input pins 8 - 15
    
    ' -----[noparse][[/noparse] Constants ]-------------------------------------------------------
    
    DS1307          CON     %1101 << 4
    
    ' -----[noparse][[/noparse] Variables ]-------------------------------------------------------
    control         VAR     Byte                    ' SQW I/O control
    DecConvert      VAR     Byte
    DateArray       VAR     Byte(9)                 ' DateArray(0) = Seconds
                                                    ' DateArray(1) = Minutes
                                                    ' DateArray(2) = Hours
                                                    ' DateArray(3) = DOW
                                                    ' DateArray(4) = Date
                                                    ' DateArray(5) = Month
                                                    ' DateArray(6) = year
                                                    ' DateArray(7) = control
    btns            VAR     Byte                    ' debounced button inputs
    btnProg         VAR     btns.BIT7               ' enable programing the clock
    btnbk           VAR     btns.BIT6               ' Back button (not enabled/intrgrated)
    btnYear         VAR     btns.BIT5               ' +/- year
    btnMonth        VAR     btns.BIT4               ' +/- month
    btnDate         VAR     btns.BIT3               ' +/- date
    btnDay          VAR     btns.BIT2               ' +/- day
    btnHr           VAR     btns.BIT1               ' +/- hours
    btnMn           VAR     btns.BIT0               ' +/- minutes
    pntr            VAR     Byte                    ' ee pointer
    cntr            VAR     Byte                    ' Counter
    char            VAR     Byte                    ' character for display
    Dirty           VAR     Bit                     ' control set_clock occurances
    ' -----[noparse][[/noparse] EEPROM Data ]-----------------------------------------------------
    DayNames        DATA    "SunMonTueWedThuFriSat"
    ' -----[noparse][[/noparse] Initialization ]--------------------------------------------------
    Reset:
      #IF ($STAMP < BS2P) #THEN
        #ERROR "This code requires BS2P or greater"
      #ENDIF
    Setup:
      DEBUG CLS,
            "DS1307 Clock set up and control", CR,
            "-----------", CR
    'First deteermine if the clock was previously set
      I2CIN SDA, DS1307, 08, [noparse][[/noparse]decconvert]                          ' retrieve clock registers
      IF decconvert < 20 THEN                                      ' clock not set
        I2COUT SDA, DS1307, 08, [noparse][[/noparse]20]                               ' write 2 MSB for year        '
        DTMFOUT Spkr, 100, 100, [noparse][[/noparse]1, 2, 3]                          ' simple alert for debugging
        Reset_Clock:
        GOSUB Get_Buttons                                          ' scan buttons
    ' load the date array with the clock defaults to be used
        datearray(0) = 00                                          ' sec
        datearray(1) = 50                                          ' min
        datearray(2) = 13                                          ' 13:00 hours
        datearray(3) = 07                                          ' DOW
        datearray(4) = 12                                          ' DOM
        datearray(5) = 06                                          ' MOY
        datearray(6) = 10                                          ' year (Lower byte)
        datearray(7) = 0                                           ' initialize the control bit
        datearray(8) = 20                                          ' SRAM Address. This byte is
                                                                   ' has 2 uses, 1. indicates the
                                                                   ' clock has been set and 2.
                                                                   ' it is the upper byte for
                                                                   ' setting of the year
        GOSUB set_clock
      ELSE                                                         'clock already set
        DTMFOUT Spkr, 10, 10, [noparse][[/noparse]1, 2, 3, 4, 5, 6]                   'Simple alert for debugging
      ENDIF
      Dirty = 0                                                    ' clear the flag
    ' -----[noparse][[/noparse] Program Code ]----------------------------------------------------
    Main:
      GOSUB Get_Clock                                              ' read DS1307
      'line 1 prints th time and line 2 prints the date
      DEBUG CRSRXY, 0, 12,
            DEC2 datearray(2), ":", DEC2 datearray(1), ":",
            DEC2 datearray(0), CR,
            DEC2 datearray(5), "/", DEC2 datearray(4), "/",
            DEC2 datearray(8), DEC2 datearray(6), CR
      DEBUG CRSRXY, 0, 14
      GOSUB print_day                                               ' turn DOW # into text
      GOSUB Get_Buttons                                             ' check if buttons pressed
      SELECT btns
        CASE = %10000001                                            ' adjust minutes
          datearray(0) = $00                                        ' set seconds back to 0
          DateArray(1) = DateArray(1) + 1                           ' Increment the array
          IF DateArray(1) >= 59 THEN                                ' if more than 59 sec
            DateArray(1) = 0                                        ' set back to 0
          ENDIF
          Dirty = 1                                                 ' set the flag
        CASE = %10000010                                            ' adjust hours
          DateArray(2) = DateArray(2) + 1                           ' increment the value
          IF DateArray(2) >= 24 THEN                                ' if more than 24 hrs
            DateArray(2) = 0                                        ' set back to 0
          ENDIF
          Dirty = 1                                                 ' set the flag
        CASE = %10000100                                            ' adjust DOW
          DateArray(3) = DateArray(3) + 1                           ' increment the value
          IF DateArray(3) >= 8 THEN                                 ' if more than 7
            DateArray(3) = 1                                        ' set back to 1
          ENDIF
          Dirty = 1                                                 ' set the flag
        CASE = %10001000                                            ' adjust DOM
          DateArray(4) = DateArray(4) + 1                           ' increment the value
          IF DateArray(4) >= 32 THEN                                ' if more than 31
            DateArray(4) = 1                                        ' Set back to 1
          ENDIF
          Dirty = 1                                                 ' set the flag
        CASE = %10010000                                            ' adjust MOY
          DateArray(5) = DateArray(5) + 1                           ' increment the value
          IF DateArray(5) >= 13 THEN                                ' if more than 12
            DateArray(5) = 1                                        ' set back to 1
          ENDIF
          Dirty = 1                                                 ' set the flag
        CASE = %10100000                                            ' adjust the year
          datearray(6) = datearray(6) + 1                           ' increment the value
          IF DateArray(6) >= 100 THEN                               ' if moe than 99
            DateArray(6) = 0                                        ' set back to 0
            datearray(8) = datearray(8) + 1                         ' increment the upper byte
          ENDIF
          Dirty = 1                                                 ' set the flag
      ENDSELECT
      IF Dirty = 1 THEN                                             ' a button was pressed and
        Dirty = 0                                                   ' date or time has changed
        GOSUB Set_Clock                                             ' so update DS1307
      ENDIF
      GOTO Main
    
    ' -----[noparse][[/noparse] Subroutines ]-----------------------------------------------------
    Get_Buttons:
      btns = %11111111                                              ' enable all eight inputs
      FOR cntr = 0 TO 7
        btns = btns & ~BtnBus                                       ' test inputs
      NEXT
    RETURN
    Print_Day:
      pntr = DayNames + ((datearray(3) - 1) * 3)                    ' point to 1st char
      FOR cntr = 0 TO 2                                              ' print 3 letters
        READ (pntr + cntr), char                                     ' read letter
        DEBUG char                                                  ' print it
      NEXT
      RETURN
    ' Do a block WRITe To clock registers
    Set_Clock:
      FOR cntr = 0 TO 8
        IF cntr < 8 THEN                                             'Don't convert Ram Data
          DecConvert  = (datearray(cntr) / 10 << 4) + (datearray(cntr) // 10) ' decimal to BCD
          I2COUT SDA, DS1307, cntr, [noparse][[/noparse]DecConvert]                     ' update clock registers
        ELSE
          I2COUT SDA, DS1307, 08, [noparse][[/noparse]datearray(8)]                     ' update year upper byte
        ENDIF
      NEXT
      RETURN
    ' Do a block read from clock registers
    Get_Clock:
      FOR cntr = 0 TO 8
        I2CIN SDA, DS1307, cntr, [noparse][[/noparse]DecConvert]                          ' retrieve clock registers
        IF cntr < 8 THEN                                               ' Don't convert Ram Data
          datearray(cntr) = DecConvert.NIB1 * 10 + DecConvert.NIB0     ' BCD to decimal
        ELSE
          I2CIN SDA, DS1307, 08, [noparse][[/noparse]datearray(8)]                        ' get year upper byte
        ENDIF
      NEXT
      RETURN
    
    





    Migs

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "Sometimes we forget that the world itself is paradise." Douglas Coupland, 'Microserfs'
    "If the doors of perception were cleansed every thing would appear to man as it is, infinite" - William Blake
    "We either make ourselves miserable, or we make ourselves strong. The amount of work is the same." Carlos Castaneda
    "One single grateful thought raised to heaven is the most perfect prayer. " G. E. Lessing
    “How much of human life is lost in waiting.” Ralph Waldo Emerson
    "Men often mistake notoriety for fame, and would rather be remembered for their vices and follies than not be noticed at all.” Harry Truman
    My website: www.intoku.net my e-mail:mreznicek@pretensa.com me:Miguel Reznicek

    Post Edited (Migs) : 7/18/2010 1:41:08 AM GMT
  • FranklinFranklin Posts: 4,747
    edited 2010-07-18 05:13
    This line in the code
      DEC2 datearray(8), DEC2 datearray(6), CR
    
    

    should print out the four digit date.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • MigsMigs Posts: 95
    edited 2010-07-18 11:16
    Hi Stephen, and in the first listing how would you print the year?

    Thanks for your help!

    Migs

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "Sometimes we forget that the world itself is paradise." Douglas Coupland, 'Microserfs'
    "If the doors of perception were cleansed every thing would appear to man as it is, infinite" - William Blake
    "We either make ourselves miserable, or we make ourselves strong. The amount of work is the same." Carlos Castaneda
    "One single grateful thought raised to heaven is the most perfect prayer. " G. E. Lessing
    “How much of human life is lost in waiting.” Ralph Waldo Emerson
    "Men often mistake notoriety for fame, and would rather be remembered for their vices and follies than not be noticed at all.” Harry Truman
    My website: www.intoku.net my e-mail:mreznicek@pretensa.com me:Miguel Reznicek
Sign In or Register to comment.