Shop OBEX P1 Docs P2 Docs Learn Events
Programming the DS1302 — Parallax Forums

Programming the DS1302

NavNav Posts: 23
edited 2004-11-09 00:17 in BASIC Stamp
Can anyone point me in the direction of a new-ish (PBASIC 2.5) program to access the DS1302 ?

I have a BS2 and have been trying it out with several other parts.· I've succesfully written several programs for the DS1620 and the MAX7219.· I recently wrote a program that alternates between displaying the temperature from the DS1620 onto a numeric 7-segment LED·display and BPI-216 Serial LCD.· To my not-so-great surprise, the clock was very inaccurate and so I decided to use a DS1302.

I found the DS1302 AppKit code very difficult to understand so tried the StampWorks Experiment#30.· Unfortunately, I found this code difficult to follow too.· It is written in PBASIC version 2.0 and is not very well structured.· It is a good example of 'spaghetti' code.· It is not too well commented either, for that matter·!· I was wondering if anyone knows of a similar program written in PBASIC 2.5 that takes advantage of the new structured features of the language (DO...LOOP, GOSUB...RETURN, etc.) to make the program easier to follow - and also, therefore, easier to adapt and maintain !· The other thing that made the StampWorks program difficult to use is that the time is displayed on the screen using the HEX addition to DEBUG.· This is okay on screen but does not lend itself to being displayed on a Numeric 7-segment LED display.· For instance, if the contents of the·'mins' variable is sent·to the LED display it displays the wrong time.· How do you 'un-hex' it to send to the display ?

I would be very grateful for anyone's advice on this matter.

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-11-05 16:08
    "Unhexing" is pretty easy to do once one has a grip on numeric formats; a skill that is important when using small micros. Here are the conversions:

    decValue = (hexValue.NIB1 * 10) + hexValue.NIB0
    hexValue = (decValue / 10 << 4) + (decValue // 10)

    Since I'm responsible for StampWorks I've attached an update to the DS1302 experiment.

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


    Post Edited (Jon Williams) : 11/5/2004 6:39:44 PM GMT
  • NavNav Posts: 23
    edited 2004-11-06 00:23
    John, thanks for your prompt reply and also for the prompt update of the DS1302 experiment.· I·saw at the end of your reply that you said you were going to update the program.· A short while later, when I logged in to reply to you, I noticed you had already posted the updated program !· That was quick !· Thank you very much.· I've looked through the updated·program and it is much easier to follow than the previous version.· Thanks again.

    I tried out the conversions you gave in your post and have updated my program with them and it works perfectly !· I have some previous experience in electronics and programming·but, as you said, it is important to have a good understanding of numeric formats when working with microcontrollers.· With that in mind, I was wondering whether there are any Parallax resources/texts designed to teach those skills ?· I have read a number of your Nuts & Volts articles and other resources on the Parallax website over the last few weeks.· It is heartning to see that Parallax provides so much support to its user community.· I got my BS2 a few weeks ago and am having great fun learning !
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-11-06 01:46
    We're glad you're having fun. I'm going to add a section to·StampWorks called "Crunching the Numbers" that will go into all the nitty-gritty of the various numeric formats that the BASIC Stamp uses and how to get the most from them.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • NavNav Posts: 23
    edited 2004-11-07 05:07
    Thanks John, I'll look out for that.
  • lrohnlrohn Posts: 33
    edited 2004-11-07 18:56
    "update to the DS1302 experiment"

    Hello Jon,

    I have been going through the code in the new experiment you uploaded trying to understand how the DS1302 works. I also have to add how much myself and I'm sure many others appreciate all the support we get from you and others in these forums.

    I have a question about the constants. I like how the registers are addressed in hex rather then binary as they are in the AppKit, it just makes it easier for me to follow.

    My question is about the write protect register. You have the WPr1 (' set write protect) constant set to $80 which is the same register used to write seconds to the DS1302.

    Can you explain how this works and the relationship between the both?

    Thanks for your help,
    Luke
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-11-07 20:43
    Use the force, Luke.... oops, wrong Luke. The idea behind StampWorks is that I wrote the code, YOU examine the component docs to make sense of it. That said, I'll give you a couple hints (I still expect you to crack open the DS1302 doc and read it).

    The control register address is $8E for a write operation (figure 4 in the docs show this). When you set bit 7 ($80) of this register the write protect is on; clearing bit 7 ($00)of this register clears the write protect (and you must clear the write protect to update clock variables). The other bits are not used and will always read back as zeros -- writing anything to them does nothing.

    Make more sense now?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • lrohnlrohn Posts: 33
    edited 2004-11-08 05:49
    Hello Jon,

    I'm sorry, I wish it did make more sense but I am still lost. I think it would help if we were both on the same page. I have been using the DS1302 Complete Data Sheet that I downloaded from http://www.maxim-ic.com/quick_view2.cfm?qv_pk=2685 over a week ago. I thought they were the same, now I notice there is a difference from what is available on your website.

    What revision of the Data Sheet are you looking at and where can I get it? I just downloaded the complete zip file of the Stamp Works Data sheets and the DS1302PDF addresses all the registers in decimal. I think this is the reason I am having trouble understanding. You said "The control register address is $8E for a write operation (figure 4 in the docs show this)". I am looking at figure 4 and don't see $8E anywhere. Figure 4 in my DOC's is labeled Register Address/Definition and shows all the registers addressed in decimal.

    Thanks for your help and understanding,
    Luke - still in search of the FORCE rolleyes.gif
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-11-08 14:15
    Okay, I'm now using the same doc you are. On page 6 is the discussion about the Write Protect Bit. On page 8 you'll find Table 2 that shows the Read and Write addresses (in hex) for the various registers. If you look toward the bottom of that table you'll see that address $8E is where we write the value for Write Protect (WP).

    To set the Write Protect we need to make bit 7 of that register 1 -- so we can write $80 (%10000000) to that register to do it. If we want to clear the Write Protect bit (which we need to do to change the clock), we write 0 to that register (no other bits in that register are used).

    I hope this clears things up. As Obi-wan told the other Luke, patience is important. Work through the code and the docs slowly ... it will come.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • lrohnlrohn Posts: 33
    edited 2004-11-08 23:11
    Thanks for the help Jon, I am finally beginning to understanding now. My next hurdle is to figure out the shiftin shiftout commands. I'll play with them for a while to see what I can accomplish.

    Luke
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-11-09 00:17
    You might want to download our "What's A Microcontroller?" text -- it's really good and going through it will save you hours of furstration.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
Sign In or Register to comment.