Shop OBEX P1 Docs P2 Docs Learn Events
DS1302, DS1307, DS32Khz - What do I need to use TIME within BS2 finally? — Parallax Forums

DS1302, DS1307, DS32Khz - What do I need to use TIME within BS2 finally?

IceFireProIceFirePro Posts: 86
edited 2010-06-15 17:40 in BASIC Stamp
Hi! I've been trying my best to use time with the StampDock software, to no prevail! I want to really do know what i do need in order to be able to have:

-Time on a chip that I can use with BS2
-Being one that has BATTERY BACKUP

I am hearing all the things about DS1302, then 1307, then DS32Khz (???), please help me - which of those, a combination, or just one, or whatever, and what type of battery do I need to order so I can just end up having all required parts in front of me?

Thanks!

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-06-14 22:52
    There are examples of the DS1302 and the DS1307 used with the BS2 (and other models). The DS1307 works best with the BS2p/pe/px models because these have built-in I2C support. The DS1302 can work with any Stamp model. Go to the Parallax webstore pages for the two devices and download the datasheets and sample code. The datasheets discuss backup batteries and techniques. The StampWorks manual has examples of the use of the DS1302 (I think that's the one) and there are Nuts and Volts Columns (downloadable from Parallax) that have examples. Get used to doing your own research. Get used to browsing through Parallax's website. There's a tremendous amount of information there including sample programs, sample schematics, etc.· The chip manufacturers (Dallas/Maxim in this case) have oodles of other information including datasheets and application notes.
  • IceFireProIceFirePro Posts: 86
    edited 2010-06-14 23:48
    So what is the 32Khz for, do I need it?! Or only a 1302? I am using HomeWork board.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-06-15 00:11
    Both the DS1302 and the DS1307 use a 32KHz crystal as their time standard. Again, read the documentation and look at the examples and the Nuts and Volts Columns on the subject.

    You can buy ready made little printed circuit boards with a DS1302 or DS1307 and a crystal and sometimes a backup battery as well. SparkFun sells one with a DS1307. There's sample code in one of the Nuts and Volts Columns on using an I2C device with a BS2 like on the HomeWork board ("I2C Fun for Everyone").
  • IceFireProIceFirePro Posts: 86
    edited 2010-06-15 00:22
    I have ordered this, http://www.newark.com/maxim-integrated-products/ds32khz-dip/oscillator/dp/74C2865, but now I think it won't help at all. Is this correct?
  • FranklinFranklin Posts: 4,747
    edited 2010-06-15 01:55
    Only you will know for sure unless you tell us when you find out. Do let us know.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • NewBeeNewBee Posts: 35
    edited 2010-06-15 10:23
    Hi,

    I posted this yesterday "Code for bs2p40 w/DS1307 timestamp clock w/user control."

    I spent some tinme on the 1302 but quickly gave it up because it didn't appear to be a fully integrated Date & time clock that ran independently, was programmable, machine readable·and kept time as well as date, including a 24 hour clock·and leap year compensation.· The 1307 does all that so I chose the DS1307, then I realized Parallax did as well.· When I started my project I bought the Parallax Professional Development Board (PDB) part number 28138.· The PDB comes complete with·a DS1307 and is fully integrated with the microcontroller circuit.· Communicating with the 1307 requires understanding the I2C communication protocol.· Fortunately Parallax has solved this for you by including the I2CIN and I2COUT commands within the BS2 product capabilities.· Sincerely, go look at my posting from yesterday.·

    If you don't understand timers and crystal oscillators·you need to take a few more basic electronics courses.· Tell me about your development environment, I'll see if I can point you in the right direction.·
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2010-06-15 17:15
    The attached code will allow you set and display the time/date using the DS1307 with a BS2, BS2P, etc in 12 or 24 hour format.
    There is also a directive which will allow a Sparkfun serial LCD to show the time - just uncomment the #DEFINE UseLCD
    If a "y" is not entered in the DEBUGIN window, code setting the clock·is skipped...
    ' {$STAMP BS2}
    ' {$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
    ' -----[noparse][[/noparse] Constants ]-------------------------------------------------------
    Ack    CON 0 ' acknowledge bit
    Nak    CON 1 ' no ack bit
    DS1307 CON %1101 << 4
    AM     CON 0
    PM     CON 1
    Mode12 CON 1  '12 hour mode
    Mode24 CON 0  '24 hour mode
    ' -----[noparse][[/noparse] Variables ]-------------------------------------------------------
    slvAddr   VAR Byte ' I2C slave address
    devNum    VAR Nib  ' device number (0 - 7)
    addrLen   VAR Nib  ' bytes in word addr (0 - 2)
    #SELECT $STAMP
    ' -----[noparse][[/noparse] BS2 Subroutines ]-------------------------------------
      #CASE BS2, BS2E, BS2SX
    'i2cData   VAR Byte ' data to/from device
    i2cWork   VAR Byte ' work byte for TX routine
    i2cAck    VAR Bit  ' Ack bit from device
    wrdAddr   VAR Word ' word address
    #ENDSELECT
    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
    mode      VAR Bit  ' 12 or 24 hour mode
    am_pm     VAR Bit  ' am or pm (for 12 hour mode)
    idx       VAR Nib  ' loop control
    pntr      VAR Byte ' ee pointer
    char      VAR Byte ' character for display
    ' -----[noparse][[/noparse] EEPROM Data ]-----------------------------------------------------
    DayNames DATA "SunMonTueWedThuFriSat"
    '#DEFINE UseLCD       'directive to display time on Sparkfun serial LCD - comment line if no LCD
    #IF UseLCD #THEN
    ' =========Define names for LCD instructions, bps=======
      homeCur    CON 1  'Cursor home cntl-A
      bigChar    CON 2  'Begin big characters cntl-B
      regChar    CON 3  'End big characters cntl-C
      noCurs     CON 4  'Make cursor invisible cntl-D
      ulCurs     CON 5  'Show underline cursor cntl-E
      blkCurs    CON 6  'Show blinking block cursor
      ringBell   CON 7  'Bell output cntl-G
      backSp     CON 8  'Backspace cntl-H
      hTab       CON 9  'Horizontal tab (4 col) cntl-I
      curLF      CON 10 'Smart linefeed cntl-J
      vTab       CON 11 'cursor up one line cntl-K
      clrLCD     CON 12 'Clear entire LCD screen cntl-L
      carRet     CON 13 'Carriage return cntl-M
      onBkLight  CON 14 'Backlight on cntl-N
      offBkLight CON 15 'Backlight off cntl-O
      posCmd     CON 16 'Position cursor cntl-P
      clrCol     CON 17 'Clear column cntl-Q ?
      alignRight CON 18 'Accept right-alignment data cntl-R
      Esc        CON 27 'Escape sequences
      LCD        PIN 15
    #ENDIF
    'TrueBaud mode for non-inverted 9600-N81 (MAX233a line driver)
    'InvBaud mode for inverted, 9600-N81    (direct connect e.g. w/22k ohm)
    #SELECT $STAMP
      #CASE BS2, BS2E, BS2PE
        TrueBaud CON 84
        InvBaud CON 16468
      #CASE BS2SX, BS2P
        TrueBaud CON 240
        InvBaud CON 16624
      #CASE BS2PX
        TrueBaud CON 396
        InvBaud CON 16780
    #ENDSELECT
    ' -----[noparse][[/noparse] Initialization ]--------------------------------------------------
    Reset:
    slvAddr = DS1307 ' 1 byte in word address
    addrLen = 1
    DEBUG CLS, "Enter 'y' to set date"
    SERIN 16, TrueBaud, 10000, Skip_set, [noparse][[/noparse]WAIT("y")]
    'DEBUG CLS                             ' Clear The DEBUG Screen
    SetClock:
    DEBUG CRSRXY, 0, 0, "Enter 2 dig year", CLREOL
    DEBUGIN HEX2 year                     ' Set Year
    DEBUG CRSRXY, 12, 0, "mth", CLREOL
    DEBUGIN HEX2 month                    ' Set Month
    DEBUG CRSRXY, 12, 0, "day", CLREOL
    DEBUGIN HEX2 date                     ' Set Date
    DEBUG CRSRXY, 6, 0, "week day", CLREOL
    DEBUG CRSRXY, 0, 1, "1=SUN 2=MON 3=TUE 4=WED 5=THU 6=FRI 7=SAT", CR
    DEBUGIN HEX1 day                      'set day
    DEBUG CLS
    DEBUG CRSRXY, 0, 0, "Enter mode 1=12hr 0=24hr", CLREOL
    DEBUGIN HEX1 mode
    DEBUG CRSRXY, 0, 1, CLREOL
    IF mode = Mode24 THEN
      DEBUG CRSRXY, 6, 0, "2 dig hour, 00-23", CLREOL
      DEBUGIN HEX2 hrs                    ' Set Hours (24 Hour Mode)
    ELSE
      DEBUG CRSRXY, 6, 0, "2 dig hour, 01-12", CLREOL
      DEBUGIN HEX2 hrs                    ' Set Hours (12 Hour Mode)
    ENDIF
    DEBUG CRSRXY, 12, 0, "min, 00-59", CLREOL
    DEBUGIN HEX2 mins                     ' Set Minutes
    IF mode = Mode12 THEN
      DEBUG CRSRXY, 6, 0, "0=AM 1=PM", CLREOL
      DEBUGIN HEX1 am_pm               ' Set AM/PM Flag
    ENDIF
    secs = $00
    control = 0 ' disable SQW output
    IF mode = Mode12 THEN
      hrs = hrs | %01000000     '$40   'set bit 6 high for 12 hour mode
    ENDIF
    IF am_pm = PM THEN
      hrs = hrs | %00100000   '$20   'set bit 5 high for PM
    ENDIF
    GOSUB Set_Clock ' block write clock regs
    ' -----[noparse][[/noparse] Program Code ]----------------------------------------------------
    Skip_set:
    #IF UseLCD #THEN
      SEROUT LCD,InvBaud,[noparse][[/noparse]onBkLight] ' Clear the screen.
      SEROUT LCD,InvBaud,[noparse][[/noparse]clrLCD, noCurs] ' Clear the screen.
    #ENDIF
    DEBUG CLS,
    "DS1307 Demo", CR,
    "-----------"
    Main:
    GOSUB Get_Clock              ' read DS1307
    IF secs = 128 THEN GOTO SetClock    'clock not set
    DEBUG CRSRXY, 0, 5, BIN8 hrs
    mode = hrs.BIT6              'get 12/24 hour bit
    IF mode = Mode12 THEN
      am_pm = hrs.BIT5             'get am_pm bit (HIGH = p.m.)
      hrs = hrs & %00011111 '$1F   'zero bits 5 and 6
    ELSE
      hrs = hrs & $3F
    ENDIF
    DEBUG CRSRXY, 0, 6, BIN8 hrs
    DEBUG CRSRXY, 0, 2,
      HEX2 hrs, ":", HEX2 mins, ":", HEX2 secs
    IF mode = Mode12 THEN
      IF am_pm = PM THEN
        DEBUG " p.m", CR
      ELSE
        DEBUG " a.m.", CR
      ENDIF
    ELSE
      DEBUG CR
    ENDIF
    'BCD to decimal conversion: decVal =(bcdVal.NIB1 * 10) + bcdVal.NIB0
    'decimal to BCD conversion: bcdVal = (decVal / 10 <<4) + decVal // 10
    'DEBUG DEC (hrs.NIB1 * 10) + hrs.NIB0, " ", DEC (mins.NIB1 * 10) + mins.NIB0, " ", DEC (secs.NIB1 * 10) + secs.NIB0, CR
    DEBUG HEX2 month, "/", HEX2 date, "/", HEX2 year, CR
    #IF UseLD #THEN
      SEROUT LCD,InvBaud,[noparse][[/noparse]posCmd,"46 ", HEX2 hrs, ":", HEX2 mins, ":", HEX2 secs]
      IF am_pm = PM THEN
        SEROUT LCD,InvBaud,[noparse][[/noparse]" P.M.", carRet]
      ELSE
        SEROUT LCD,InvBaud,[noparse][[/noparse]" A.M", carRet]
      ENDIF
      SEROUT LCD,InvBaud,[noparse][[/noparse]posCmd,"26 ", HEX2 month, "/", HEX2 date, "/", HEX2 year, carRet]
    #ENDIF
    GOSUB Print_Day
    PAUSE 100
    GOTO Main
    ' -----[noparse][[/noparse] Subroutines ]-----------------------------------------------------
    Print_Day:
    IF day > 7 THEN RETURN
    pntr = DayNames + ((day - 1) * 3) ' point to 1st char
    #IF UseLCD #THEN
      SEROUT LCD,InvBaud,[noparse][[/noparse]posCmd,"20 "]
    #ENDIF
    FOR idx = 0 TO 2 ' print 3 letters
      READ (pntr + idx), char ' read letter
      DEBUG char ' print it
      #IF UseLCD #THEN
        SEROUT LCD,InvBaud,[noparse][[/noparse]char]
      #ENDIF
    NEXT
    RETURN
    #SELECT $STAMP
    ' -----[noparse][[/noparse] BS2 Subroutines ]-------------------------------------
      #CASE BS2, BS2E, BS2SX
      ' Do a block write to clock registers
    Set_Clock:
    GOSUB I2C_Start                  ' send Start
    i2cWork = slvAddr & %11111110    ' send slave ID (write)
    GOSUB I2C_TX_Byte
    IF (i2cAck = Nak) THEN Set_Clock ' wait until not busy
    i2cWork = 0                      ' point at secs register
    GOSUB I2C_TX_Byte
    FOR idx = 0 TO 7                 ' write secs to control
      i2cWork = secs(idx)
      GOSUB I2C_TX_Byte
    NEXT
    GOSUB I2C_Stop
    RETURN
    ' Do a block read from clock registers
    Get_Clock:
    GOSUB I2C_Start                  ' send Start
    i2cWork = slvAddr & %11111110    ' send slave ID (write)
    GOSUB I2C_TX_Byte
    IF (i2cAck = Nak) THEN Get_Clock ' wait until not busy
    i2cWork = 0                      ' point at secs register
    GOSUB I2C_TX_Byte
    GOSUB I2C_Start
    i2cWork = slvAddr | %00000001    ' send slave ID (read)
    GOSUB I2C_TX_Byte
    FOR idx = 0 TO 6                 ' read secs to year
      GOSUB I2C_RX_Byte
      secs(idx) = i2cWork
    NEXT
    GOSUB I2C_RX_Byte_Nak            ' read control & send NAK to end read
    control = i2cWork
    GOSUB I2C_Stop
    RETURN
     
    ' -----[noparse][[/noparse] Low Level I2C Subroutines]----------------------------------------
    ' *** Start Sequence ***
    I2C_Start: ' I2C start bit sequence
    INPUT SDA
    INPUT SCL
    LOW SDA
    Clock_Hold:
    DO : LOOP UNTIL (SCL = 1) ' wait for clock release
    RETURN
    ' *** Stop Sequence ***
    I2C_Stop: ' I2C stop bit sequence
    LOW SDA
    INPUT SCL
    INPUT SDA
    RETURN
    ' *** Transmit Byte ***
    I2C_TX_Byte:
    SHIFTOUT SDA, SCL, MSBFIRST, [noparse][[/noparse]i2cWork\8] ' send byte to device
    SHIFTIN SDA, SCL, MSBPRE, [noparse][[/noparse]i2cAck\1] ' get acknowledge bit
    RETURN
    ' *** Receive Byte ***
    I2C_RX_Byte_Nak:
    i2cAck = Nak ' no Ack = high
    GOTO I2C_RX
    I2C_RX_Byte:
    i2cAck = Ack ' Ack = low
    I2C_RX:
    SHIFTIN SDA, SCL, MSBPRE, [noparse][[/noparse]i2cWork\8] ' get byte from device
    SHIFTOUT SDA, SCL, LSBFIRST, [noparse][[/noparse]i2cAck\1] ' send ack or nak
    RETURN
    
    ' -----[noparse][[/noparse] BS2P Subroutines ]------------------------------------
    #CASE BS2P, BS2PE, BS2PX
    ' 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
    #ENDSELECT
    
  • IceFireProIceFirePro Posts: 86
    edited 2010-06-15 17:40
    Wow!Thanks! That would take quite a bit to figure out [noparse]:)[/noparse]
Sign In or Register to comment.