Shop OBEX P1 Docs P2 Docs Learn Events
I2C timer (or other I2C device) on BS2, BS2SX, etc. — Parallax Forums

I2C timer (or other I2C device) on BS2, BS2SX, etc.

Carl HayesCarl Hayes Posts: 841
edited 2008-11-02 15:43 in BASIC Stamp
Many devices (such as the Gravitech I2C-RTC real time clock) communicate by the I2C protocol.· Only the most advanced Stamps (BS2P, BS2PE, BS2PX) support this natively in firmware.· I thought it would be nice if I could avoid that expense by running I2C devices on a BS2SX.· Also I wanted to be able to put the I2C device on any pins I chose, not just 0-1 or 8-9.· I've tested on a BS2, BS2SX, and BS2P24.

It took about a day to develop the code to do this, and I thought others might be interested.· I have attached a copy of the code that does this for me.· It will, I think, work with almost any I2C device, but in particular it is designed to work with the Maxim (Dallas Semiconductor) DS1340 clock chip that is used in the Gravitech real time clock.· It would require modification for use with other I2C devices, but the mods would be easy.

With the DS1340, it will (1) set the clock, (2) read the clock, (3) adjust the clock to run slower, and (4) adjust the clock to run faster.· I've included a little utility, as a demo, that can be used just as provided here, to do these things through DEBUG.· It's kind of nice to be able to adjust the speed of the clock, for those things are often a little off, right out of the box.

I hope this will be useful to someone besides me.



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
· -- Carl, nn5i

Comments

  • AmaralAmaral Posts: 176
    edited 2008-10-31 17:29
    I guess I can say for all .. THANK YOU for sherring your code with us ... BUT and there is aways a BUT..

    on STAMP WORK it has been explained and in a shorter code wich you can adapt for anything else..


    anyway .. thank you
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-10-31 18:13
    Thank you for your sunny and vitalizing response.· I'm not surprised that it has been done before, but I'll a little surprised if it is significantly more compact.· Can you point me to the prior work you mention?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i
  • $WMc%$WMc% Posts: 1,884
    edited 2008-10-31 22:41
    Mr.Hayes

    The Code in StampWorks is Experment.#33 pg.188 using the DS1307 and not the DS1340. I'm pretty sure StampWorks is a Free download
    and a ?ed copy can be pur$$. It can be found on the Parallax web-site.

    The code for the DS1307 is a bit long for My liking w/ a I2C bus. I'll check out the code you posted for the DS1340. Thanks for sharing Your code... Mr.Hayes

    ________________________________$WMc%________________________on the D.L._________

    Post Edited ($WMc%) : 11/2/2008 7:25:08 PM GMT
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-10-31 22:56
    You're welcome.··My code is a little long -- as posted it is a complete utility that nearly fills 2048K on a Stamp.· More than half of that is the demo routine at the front, with its many DEBUG statements (DEBUG statements are inordinately space-filling).· If you need only to read the RTC, my code uses only about 23% of the storage in a Stamp -- which admittedly is still a lot.· I'll have a look at the reference you cite and see if they've got a more compact solution.· Thanks!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-10-31 23:48
    I couldn't find Stampworks as a download in English.· I found it in Spanish, and perhaps a few other languages, but not English.· The Spanish version is quaint -- some chapter headings escaped the translator and are still in English -- but it did point me to an Internet search, by which I found a snippet of code that reads a DS1302 time chip.· The DS1302 doesn't have all the capabilities of a DS1340, so it takes less code to handle it.· They use a SHIFTIN where I use about 24 lines of code to do the same thing (in my RTCreadbyte and RTCsendbyte suboutines).· I'll study that, and probably be able to shrink my stuff significantly, learning a little meanwhile.

    Your comments and information have been extremely helpful (unlike that other fellow, who was only dismissive), and I am very grateful to you.· So, simply -- Thanks!· smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-11-01 02:38
    Amazing. Substitution of a single SHIFTIN instruction for 24 instructions in my RTCreadbyte subroutine, and SHIFTOUT for 24 instructions in RTCSENDbyte, resulted in savings of 261 bytes. That's significant. I then experimented further, by substituting the SHIFTin or SHIFTout wherever a GOSUB called these subs, and eliminated the subs. This made the program larger again, as I expected, because a SHIFTIN or SHIFTOUT uses more storage than a GOSUB.

    But you with the strange name of $WMc% have (1) taught me something, and (2) enabled me to improve my code. May your days be long, and may you people the earth with your get.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i
  • RDL2004RDL2004 Posts: 2,554
    edited 2008-11-01 04:43
    The code overhead for I2C is pretty heavy. If you need a lot of devices to be controlled by the Stamp it's probably worth it. If you only have a few then SPI is good. Of course, it really all depends on what protocol the device you want to use supports.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Rick
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-11-01 19:35
    RDL2004 said...
    The code overhead for I2C is pretty heavy. If you need a lot of devices to be controlled by the Stamp it's probably worth it. If you only have a few then SPI is good. Of course, it really all depends on what protocol the device you want to use supports.

    Aha, another chance to learn, merely by admitting I need it.· What's SPI?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i
  • RDL2004RDL2004 Posts: 2,554
    edited 2008-11-02 15:31
    I think SPI is maybe the only reason the Stamp's SHIFTOUT and SHIFTIN instructions exist.

    The communications is by 3 or 4 lines, Chip Select, Clock, Data In and Data Out.

    There must be one unique Chip Select line to each device, all others can be bussed. This is it's big disadvantage vs I2C. SPI can run at many MHz vs 400KHz (I think) for I2C, this is maybe it's biggest advantage over I2C.

    There is much more info here:

    en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Rick
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-11-02 15:43
    Thank you. That's very illuminating. I think I have some SPI devices (LCD displays) without having known there was a name for that interface. I wish they were I2C instead -- fewer pins.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i
Sign In or Register to comment.