Shop OBEX P1 Docs P2 Docs Learn Events
DS1307 and BS2 — Parallax Forums

DS1307 and BS2

ArchiverArchiver Posts: 46,084
edited 2004-04-12 23:06 in General Discussion
If I want to set the time on a DS1307 using my BS2pe I write:

Wr1307 CON %11010000

I2COUT SDA, Wr1307, 0, [noparse][[/noparse]secs, mins, hrs]

If I wanted to use my BS2, could I write:

sda pin 0
scl pin 1

shiftout sda, scl, wr1307, 0, msbfirst, [noparse][[/noparse]secs, mins, hrs]

and achieve the same result?

Sid


[noparse][[/noparse]Non-text portions of this message have been removed]

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2004-04-04 18:22
    No. I2C has a specific protocol that includes a "start" condition,
    clocking data, receiving an acknowledge bit, and generating a "stop"
    condition. It can be done with the BS2, but requires a little more code
    and you'll have to send just one byte at a time.

    For details on I2C interfacing with a BS2 (or BS2e or BS2sx) this
    article will help:

    http://www.parallax.com/dl/docs/cols/nv/vol3/col/nv85.pdf

    As you'll see after you read the article, the BS2p and BS2pe I2C
    functions are really quite nice as they handle a bit workload for us.

    -- Jon Williams
    -- Applications Engineer, Parallax
    -- Dallas Office


    Original Message
    From: Newzed@a... [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=lOYC3xYu-xbnaEFbz26ev6fSW6w3v72B6g2KV5DZswn6glF6qJgXPwvTrpphRRGCnKUNPw]Newzed@a...[/url
    Sent: Sunday, April 04, 2004 9:12 AM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] DS1307 and BS2


    If I want to set the time on a DS1307 using my BS2pe I write:

    Wr1307 CON %11010000

    I2COUT SDA, Wr1307, 0, [noparse][[/noparse]secs, mins, hrs]

    If I wanted to use my BS2, could I write:

    sda pin 0
    scl pin 1

    shiftout sda, scl, wr1307, 0, msbfirst, [noparse][[/noparse]secs, mins, hrs]

    and achieve the same result?

    Sid
  • ArchiverArchiver Posts: 46,084
    edited 2004-04-04 19:30
    For those that are interested I have updated a demo program for the BS2
    that uses I2C. The device demo is the PCF8574A that is included on the
    NX-1000-24/40 lab board. The program includes low-level I2C subroutines
    that can be used with any device, and a couple high-level routines for
    writing to and reading from the PCF8574A. With this code and the data
    sheet for the PCF8574A you'll see that I2C on the BS2 isn't that tough
    and opens up a whole lot of opportunities.

    I'll post the code in the files section.

    -- Jon Williams
    -- Applications Engineer, Parallax
    -- Dallas Office


    Original Message
    From: Jon Williams
    Sent: Sunday, April 04, 2004 12:23 PM
    To: basicstamps@yahoogroups.com
    Subject: RE: [noparse][[/noparse]basicstamps] DS1307 and BS2


    No. I2C has a specific protocol that includes a "start" condition,
    clocking data, receiving an acknowledge bit, and generating a "stop"
    condition. It can be done with the BS2, but requires a little more code
    and you'll have to send just one byte at a time.

    For details on I2C interfacing with a BS2 (or BS2e or BS2sx) this
    article will help:

    http://www.parallax.com/dl/docs/cols/nv/vol3/col/nv85.pdf

    As you'll see after you read the article, the BS2p and BS2pe I2C
    functions are really quite nice as they handle a bit workload for us.

    -- Jon Williams
    -- Applications Engineer, Parallax
    -- Dallas Office


    Original Message
    From: Newzed@a... [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=A8cauD-gG_ZcOFK3NYKj7a8vjWWzRJspbpxvvYZDQx4ZFZm8wK2xIgzC4i6YnsBASX08fvsR]Newzed@a...[/url
    Sent: Sunday, April 04, 2004 9:12 AM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] DS1307 and BS2


    If I want to set the time on a DS1307 using my BS2pe I write:

    Wr1307 CON %11010000

    I2COUT SDA, Wr1307, 0, [noparse][[/noparse]secs, mins, hrs]

    If I wanted to use my BS2, could I write:

    sda pin 0
    scl pin 1

    shiftout sda, scl, wr1307, 0, msbfirst, [noparse][[/noparse]secs, mins, hrs]

    and achieve the same result?

    Sid
  • ArchiverArchiver Posts: 46,084
    edited 2004-04-05 13:57
    When writing code for the BS2 and the DS1307, I still have to convert my
    clock parameters to BCD before transmitting to the clock - correct?

    Sid


    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2004-04-05 16:17
    Yes. According the the DS1307 docs, time elements are stored in BCD.
    Decimal to BCD is easy with the BASIC Stamp:

    bcdVal = (decVal / 10 << 4) + (decVal // 10)

    To get from BCD to decimal:

    decVal = bcdVal.NIB1 * 10 + bcdVal.NIB0


    -- Jon Williams
    -- Applications Engineer, Parallax
    -- Dallas Office


    Original Message
    From: Newzed@a... [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=AMkamV5GTTx5W8GcSA0kteU35JOc0sKucHm4yG9qXTpib72IjfvqEWp0rgfdA34DA-DWGmRhwWes]Newzed@a...[/url
    Sent: Monday, April 05, 2004 7:58 AM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] DS1307 and BS2


    When writing code for the BS2 and the DS1307, I still have to convert my

    clock parameters to BCD before transmitting to the clock - correct?

    Sid
  • ArchiverArchiver Posts: 46,084
    edited 2004-04-05 16:53
    >When writing code for the BS2 and the DS1307, I still have to convert my
    >clock parameters to BCD before transmitting to the clock - correct?
    >
    >Sid

    Yes, packed BCD. Two BCD clock digits packed into each byte.

    E.g., $45 is 45 seconds.

    If you have seconds in decimal, you have to convert it to packed BCD
    before you set the clock:

    secondsBCD = (seconds / 10 * 16) + (seconds // 10)

    or another way to do it:

    secondsBCD.nib1=seconds/10
    secondsBCD.nib0=seconds//10

    -- Tracy
  • ArchiverArchiver Posts: 46,084
    edited 2004-04-05 18:58
    I am trying to convert the BS2 I2C code for the PCF8583 for operation with
    the DS1307.

    The 8583 sends the address for seconds as "2". If I read the data sheet for
    the 1307 correctly, the address of the seconds register is 00, minutes 01,
    and so on. Since I can only send one byte at a time I would send 00, seconds,
    01, minutes, and so on. Is this correct?

    Sid


    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2004-04-05 20:49
    I've written I2C code for the BS2 that allows one to write a byte value
    to a specific address. Here's a snippet from code adapted for the
    PCF8583:


    ' Write i2cData to i2cReg

    Write_Byte:
    GOSUB I2C_Start
    i2cWork = Wr8583 ' send device address
    GOSUB I2C_TX_Byte
    i2cWork = i2cReg ' send reg number
    GOSUB I2C_TX_Byte
    i2cWork = i2cData ' send data
    GOSUB I2C_TX_Byte
    GOSUB I2C_Stop
    RETURN


    Notice that two values are passed: i2cData holds the value, i2cReg is
    the register to write to. To set the seconds for the PCF8583, one would
    do this:

    i2cReg = 2
    i2cData = 15
    GOSUB Write_Byte


    This code will work for the DS1307 -- you need to change the Wr8583 to
    Wr1307 (and change the value to match the 1307) and then use the correct
    registers as they are different from the PCF8583.

    -- Jon Williams
    -- Applications Engineer, Parallax
    -- Dallas Office


    Original Message
    From: Newzed@a... [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=GoyVoKKPoqj3359o19w9-97dij2ZRgJGRYr9Aglre7qvqppxzPko9HRWlkNpjEr8dv5C3A]Newzed@a...[/url
    Sent: Monday, April 05, 2004 12:58 PM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] DS1307 and BS2


    I am trying to convert the BS2 I2C code for the PCF8583 for operation
    with
    the DS1307.

    The 8583 sends the address for seconds as "2". If I read the data
    sheet for
    the 1307 correctly, the address of the seconds register is 00, minutes
    01,
    and so on. Since I can only send one byte at a time I would send 00,
    seconds,
    01, minutes, and so on. Is this correct?

    Sid
  • ArchiverArchiver Posts: 46,084
    edited 2004-04-05 21:04
    Hi Sid,

    Yes, the time registers in the DS1307 start at zero with seconds.
    But if you have the time variables arranged in an array, with
    seconds, minutes, hours, day-of-week, day, month, year, you can send
    all 7 bytes as a single command with this format on the BS2p or
    BS2pe...


    I2COUT 0,$D0,0,[noparse][[/noparse]STR seconds\7]
    | | | |
    | | | 7 bytes
    | | starting adrs
    | slave ID
    pin

    Or you could lay it out like this....

    I2COUT 0,$D0,0,[noparse][[/noparse]seconds,minutes,hours,....,year]

    I realized you say this is for the BS2, without the I2C commands.
    There is no need to restart and send the address each time. The 7
    bytes can be send in succession ...
    start, slaveID, zero address, 7 bytes, stop.

    -- Tracy







    >I am trying to convert the BS2 I2C code for the PCF8583 for operation with
    >the DS1307.
    >
    >The 8583 sends the address for seconds as "2". If I read the data sheet for
    >the 1307 correctly, the address of the seconds register is 00, minutes 01,
    >and so on. Since I can only send one byte at a time I would send 00, seconds,
    >01, minutes, and so on. Is this correct?
    >
    >Sid
  • ArchiverArchiver Posts: 46,084
    edited 2004-04-11 23:43
    I hope someone out there can help.

    Trying to run an I2C DS1307 on a BS2. I get this far and it locks up:

    I2C_Start: ' I2C start bit sequence
    INPUT SDA
    INPUT SCL
    LOW SDA ' SDA -> low while SCL high

    Clock_Hold:
    debug "Holding", cr
    if (Ins.LowBit(SCL) = 0) THEN Clock_Hold ' device ready?
    RETURN

    I stuck the debug in so I could tell where the program was hanging up - its
    hanging up in the Clock_Hold routine.

    SDA is Pin 0, SCL is Pin 1. Isn't "Ins.LowBit" the same as IN0?

    Is this enough information?

    Sid


    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2004-04-12 05:13
    Hi Sid,

    Do you have the required pullup resistors on both sda and scl?

    When the program executes the INPUT scl command, the clock line
    should definitely by high.

    I don't understand why you are using the array syntax for this simple
    debug? Why don't you just use In1, instead of Ins.LOWBIT(scl)?

    If you use the PBASIC 2.5 declarations,
    sda PIN 0
    scl PIN 1
    then PBASIC will interpret all the commands as needed
    INPUT scl ' turns it into an input
    IF scl=0 THEN Clock_Hold ' reads it as an input

    But the main thing is, there is no excuse for scl to be low at that
    point unless something is amiss in the hardware, i.e. missing the
    pullup.

    -- Tracy

    >I hope someone out there can help.
    >
    >Trying to run an I2C DS1307 on a BS2. I get this far and it locks up:
    >
    >I2C_Start: ' I2C start bit sequence
    > INPUT SDA
    > INPUT SCL
    > LOW SDA ' SDA -> low while SCL high
    >
    >Clock_Hold:
    >debug "Holding", cr
    > if (Ins.LowBit(SCL) = 0) THEN Clock_Hold ' device ready?
    > RETURN
    >
    >I stuck the debug in so I could tell where the program was hanging up - its
    >hanging up in the Clock_Hold routine.
    >
    >SDA is Pin 0, SCL is Pin 1. Isn't "Ins.LowBit" the same as IN0?
    >
    >Is this enough information?
    >
    >Sid
  • ArchiverArchiver Posts: 46,084
    edited 2004-04-12 23:06
    Actually I have another question about the 1307. I am trying just to
    write the current date and time and read it later for data collection
    time stamping. I tried the line :

    I2COUT SDA, Wr1307, 0, [noparse][[/noparse]$4f, $27, $56, $01, $0c, $04, 04]

    with a modification of the program that parallax sends with the
    DS1307, but cant seem to jump into the calendar. Any clues what I am
    doing wrong?

    --- In basicstamps@yahoogroups.com, Tracy Allen <tracy@e...> wrote:
    > Hi Sid,
    >
    > Do you have the required pullup resistors on both sda and scl?
    >
    > When the program executes the INPUT scl command, the clock line
    > should definitely by high.
    >
    > I don't understand why you are using the array syntax for this
    simple
    > debug? Why don't you just use In1, instead of Ins.LOWBIT(scl)?
    >
    > If you use the PBASIC 2.5 declarations,
    > sda PIN 0
    > scl PIN 1
    > then PBASIC will interpret all the commands as needed
    > INPUT scl ' turns it into an input
    > IF scl=0 THEN Clock_Hold ' reads it as an input
    >
    > But the main thing is, there is no excuse for scl to be low at that
    > point unless something is amiss in the hardware, i.e. missing the
    > pullup.
    >
    > -- Tracy
    >
    > >I hope someone out there can help.
    > >
    > >Trying to run an I2C DS1307 on a BS2. I get this far and it locks
    up:
    > >
    > >I2C_Start: ' I2C start bit
    sequence
    > > INPUT SDA
    > > INPUT SCL
    > > LOW SDA ' SDA -> low while
    SCL high
    > >
    > >Clock_Hold:
    > >debug "Holding", cr
    > > if (Ins.LowBit(SCL) = 0) THEN Clock_Hold ' device ready?
    > > RETURN
    > >
    > >I stuck the debug in so I could tell where the program was hanging
    up - its
    > >hanging up in the Clock_Hold routine.
    > >
    > >SDA is Pin 0, SCL is Pin 1. Isn't "Ins.LowBit" the same as IN0?
    > >
    > >Is this enough information?
    > >
    > >Sid
Sign In or Register to comment.