Shop OBEX P1 Docs P2 Docs Learn Events
DS1302 Demo Code (Updated) — Parallax Forums

DS1302 Demo Code (Updated)

Chris SavageChris Savage Parallax EngineeringPosts: 14,406
edited 2017-02-07 17:27 in Robotics
Hello Everyone,

·· Some time ago I posted a message about getting together some code for the DS1302 which would endow the average user with the necessary information and code to make full use of the chip's 12 & 24 hours modes, as well as intuitive access to the on-board RAM.· So far this seems to have been a difficult thing to achieve easily.· By searching some 17 different threads on problems using the DS1302 in the last couple of years, it seemed like a good thing to cover.

·· The DS1302 is a very useful chip for keeping time, supporting 12 & 24 Hour Modes, as well as having 31 bytes of RAM available.· All of this can be backed up by either a battery, or SuperCap, and the chip even provides a trickle-charge circuit which is programmable, but not covered here.· The datasheet for the DS1302 can be found at the link below.

http://www.parallax.com/detail.asp?product_id=604-00005

·· The attached programs are the culmination of weeks of testing, debugging, simplifying, and re-testing fragments of my code for the DS1302.· The code is heavily documented.· More so than you're probably used to, but for those who were intimidated by the DS1302, or didn't fully understand how many of the functions worked, I felt this was necessary.· Use of both 12 and 24 Hour modes is implemented, as well as using the on-board RAM.· All this is done in an easy manner, with explanations as to how the commands are formed.· 12 & 24 Hour modes are handled within subroutines, making it easy to switch between them.

·· The Demo programs use the DEBUG screen for most input/output, which makes the code appear rather large, but remember, this is demo code, and you wouldn't necessarily have the comments, menus and functions implemented as they are here.· The code is intended to be easily integrated into your own applications and use your particular input/output devices.

·· A schematic is attached showing the connections used in the demo code.· Feel free to use whatever I/O pins you want, just remember to change them in the I/O Definitions.· A 3V Backup Battery is also shown.· I have used 2AA batteries in my demo, but you could easily use a 3V Lithium such as the CR2032.· This keeps the time going and the RAM backed up whenever power is lost.

·· The demo is also running on a BS2, but you could use any BASIC Stamp Microcontroller you want.· The only thing you may have to change is the Baud rate in the LCD version, which is currently setup to use the new Parallax 2 X 16 Serial LCD Display @ 19,200bps.

http://www.parallax.com/detail.asp?product_id=27976· (Non-Backlit)
http://www.parallax.com/detail.asp?product_id=27977· (Backlit)

·· It is my sincerest hope that our customers will benefit from this demo code.· Please post any comments, suggestions and/or feedback here or via e-mail.· Enjoy!

UPDATE: I recently optimized a couple of areas I missed when I compiled this code which made a few routines redundant.· I also attached a template file (DS1302_Template.bs2) since many people didn't understand that the demo was supposed to be very verbose and uses up almost all the program space to demonstrate various functions.· In reality only a very small amount of memory is used for various functions and even that can be decreased is the user doesn't use all these functions.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com


Post Edited (Chris Savage (Parallax)) : 4/27/2006 12:43:42 AM GMT
«134

Comments

  • MrdavidjenkinsMrdavidjenkins Posts: 49
    edited 2005-04-13 01:10
    You might want to let folks know that the pin assignments will need to be changed and might not be sequental. That is, instead of using pins 0,1,2 I had to use pins 3,2,4 on my board. The demo runs fine.

    Thanks
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-04-13 02:32
    Hello,

    ·· Glad you got the code to work.· You're probably right.· Most people will notice the pin constants at the beginning of the code.· By simply changing these numbers to the pins you're using the code will work with those new pins.· No other changes are necessary to use different pins.· Just the 3 pin constants.· Take Care!


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com


    Post Edited (Chris Savage (Parallax)) : 4/16/2005 4:08:07 AM GMT
  • starbotstarbot Posts: 4
    edited 2005-04-18 19:01
    Yea, i'm using pins 12,9,14.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-07-31 02:29
    Nice work, you should post your own thread on it, and include the source code and schematic!· =)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • ShortShort Posts: 26
    edited 2005-08-02 03:20
    · Hello Chris

    ···· I had a time trying to upload this picture,·I have just purchased a new camera from Ebay for 17 dollars including shiping it was supposed to have
    not worked but it does. The picture is much clearer, I will make an explanation and finish the source for my own thread.

    · Thanks· Doug

    ···· The picture is attached
    1200 x 900 - 201K
    Gid.jpg 200.5K
  • siangangsiangang Posts: 1
    edited 2006-01-05 12:45
    i want to know the program pic16f877a to control ds1302 with picbasic pro
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-01-05 16:54
    Since this is a Parallax forum, we do not support, nor do we have any sample code for PIC products.· Please try a PIC support site/forum.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • gibbmangibbman Posts: 98
    edited 2006-01-05 23:25
    Thanks, Chris. Very, very, helpful.
  • Robert@HCCRobert@HCC Posts: 80
    edited 2006-01-08 11:22
    Chris,

    Thank you for your excellent work with the DS1302, and for sharing it with us. I am one of those who was a bit intimidated by the code I have looked at so far for this RTC. You have put together a clean and simple piece of work here ( if I can pretty much follow it, anyone can [noparse]:D[/noparse] ) that I believe will allow people like myself who are still at the early stages of knowledge with PBASIC, stamping and working with ICs to have a little fun, and learn some new stuff quite easily. I especially like the way you set up the functions to change/set the time without using buttons - this is something I really like ( and was trying to figure out), because now I can use it in my particular application, which needs to be autonomous - no allowances for pushing buttons, and no room for any either.

    Now I can set up a separate program to set/change time, load up my main program with the added timekeeping functions,apply some battery power and away I go..... first stop is to remove the routines I am using to get my time/datestamp from GPS - along with the added hassle of converting from UTC to local time! Not to mention I have not been able to convert the date yet... now, no need [noparse]:)[/noparse]

    Just waiting on a crystal and the RTC to be delivered so I can play [noparse]:)[/noparse]

    Thanks again, and excellent work!
    Yet another reason, among countless othere of why I am glad I chose to go with Parallax for our project.

    Robert
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-01-09 00:50
    Robert,

    ·· Thanks for your comments.· A few people thought the code was a bit bloated.· To some degree it is.· It's designed to be educational and clear at the expense of using a lot of room up to demo it.· Bottom line is the core routines take up very little room, and I was even able to re-use variables and I/O pins in several applications using this chip so its also very easy to add to a project.· Anyway, thanks again!· Enjoy!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Robert@HCCRobert@HCC Posts: 80
    edited 2006-01-19 06:14
    Aloha Chris,



    Got my DS1302 and crystal in the mail, and had some time to play today ...
    Demo works fine for me, and was able to put together a separate program to set time/date/day ect, then just incorporate the functions to get and display the time,ect in my main program. Was kinda wierd , tho - I hooked up 2 AA batteries to store the time, and noticed some wierd results in the display awile later, gibberish in the time display, or incorrect numbers. Of course, the last thing I tried was disconnecting the batteries and assumed the IC or crystal or connections were screwy rolleyes.gif . Disconnected the 2 AA's and everything works fine( think I just have a flaky connection between B.B. and bateries), except for the below...

    Just one thing I am having trouble with - and was wondering if you could toss me a hint on how to display everything ( Day, MM/DD/YY, HH:MM [noparse][[/noparse]SS] , AM/PM ) on a single line in debug window using SEROUT ?

    Trying to solve it myself, but aggggh I ...am...drowning.....in Cisco , electronics and Maths and my brain cant seem to grasp the solution turn.gif
    smilewinkgrin.gif

    Edit: hah! I got it rolleyes.gif -
    sorrry to mess up your thread, Chris . Took a bit of work to string it all together at the exact position I wanted in the debug, but it works wonderful now! The loop with the "day" and the AM/PM stuff threw me for a ..errr loop, but its all good.....
    It now starts at 0,5 in the debug, prints DAY mm/dd/yy hh[noparse]:mm:[/noparse]ss AM/PM

    Whoopee! smilewinkgrin.gif

    AHHHH the joy of discovery! rolleyes.gif

    Aloha,
    Robert

    Post Edited (Robert@HCC) : 1/19/2006 9:16:01 AM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-01-19 19:37
    Robert,

    ·· No problem...The forums are here to share knowledge.· If something I posted doesn't make sense then I need to correct it.· But I am glad you got it figured out.· As for the batteries, are you sure you have your connections right and that the batteries are good?· I always used 2 AA batteries in all prototyping and then later switched to a 3V Lithium Coin Cell (CR2032).· I never had any problems with this.· Bottom line is as long as the power outages are rare you're not really using the battery.· It's nothing more than a backup so I have never felt the need to use a rechargeable system.· Besides, in the alarm systems the main power was backed-up separately, so even a mains out didn't cause the CR2032 to kick in.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-04-27 00:44
    UPDATE: I recently optimized a couple of areas I missed when I compiled this code which made a few routines redundant.· I also attached a template file (DS1302_Template.bs2) since many people didn't understand that the demo was supposed to be very verbose and uses up almost all the program space to demonstrate various functions.· In reality only a very small amount of memory is used for various functions and even that can be decreased is the user doesn't use all these functions.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • robertoroberto Posts: 37
    edited 2006-04-27 15:33
    Thanks so much Chris this helps me understands the timer much more in dept than any tutorial outhere on this chip. roll.gif

    made my day. gonna work on this all day.
  • MichaelDeFalcoMichaelDeFalco Posts: 19
    edited 2007-07-10 16:27
    Chris Savage:

    I know this is an old thread but I am trying to use the DS1302 chip for the ram only.

    Can I just use the code you wrote in the (RAM_Mode[noparse]:)[/noparse] only?

    I only need to store & retrieve data·in a single register.

    Question is: do I need all the housekeeping code in the front end of your app?

    Thanks!

    Regards,

    M. D.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    M.D.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-07-10 17:35
    Michael,

    You could easily isolate the routines that deal with the RAM and just use those…You only need to include the constants that are used by those routines as well. I hope this helps. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • MichaelDeFalcoMichaelDeFalco Posts: 19
    edited 2007-07-10 17:50
    Chris:

    Thanks for the quick reply.···························································································································· I'll try that.

    Regards,

    Mike D.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    M.D.
  • MigsMigs Posts: 95
    edited 2007-07-27 19:54
    Chris:

    Would you solder the crystal directly to the IC pins? Is this crystal heat sensitive? (delicate?)

    Thanks, -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
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-07-27 20:38
    Migs
    ·
    ·
    ·
    Would you solder the crystal directly to the IC pins?


    I have done what you are talking about and it work very well BUT here
    is the Catch when you solder the crystal directly to the IC pins you can not return it for replacement Becacse i have ask this from Chirs before

    Is this crystal heat sensitive? (delicate?)

    When solder the crystal or the IC Chip·Do Not keep the heat on it too long

    If you use GOOD SOLDER you will not have to stay on it to long


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 7/27/2007 8:45:49 PM GMT
  • MigsMigs Posts: 95
    edited 2007-07-27 21:56
    Hi Sam:

    What is gold solder? Where do you buy it? I looked in Jameco, but it was sold by the "bar" so I'm wondering....

    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
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-07-27 23:08
    An engineer from Dallas once told me they highly recommended against soldering the chip to the IC. He gave me a couple of reasons but it stuck in my head ever since. I had already done it with a chip and so I use that once in awhile when I need a quick RTC for an experiment, but I would never do it on a project or final PCB. The most important things with the crystal is it needs to be 6pF and the layout of a PCB should be as per the datasheet, which gives strict guidelines. I hope this helps. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-08-04 21:16
    ·Migs

    What is gold solder? Where do you buy it? I looked in Jameco, but it was sold by the "bar" so I'm wondering....

    Here is where you buy some good·solder· http://www.howardelectronics.com/amtech/NCsolder.html


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 8/4/2007 9:22:21 PM GMT
  • Vern GranerVern Graner Posts: 337
    edited 2009-02-17 22:45
    So Chris, When will we see one of these sweet demos for the DS-1307..? tongue.gif

    Vern

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Vern Graner CNE/CNA/SSE    | "If the network is down, then you're
    Senior Systems Engineer    | obviously incompetent so why are we
    Texas Information Services | paying you? Of course,if the network
    http://www.txis.com        | is up, then we obviously don't need
    Austin Office 512 328-8947 | you, so why are we paying you?" ©VLG
    
    
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-02-18 16:02
    Vern,

    It’s been awhile since I’ve seen you on. =) The problem with the DS1307 is that it is not nearly as flexible as the DS1302. Being an I2C chip means that the BASIC Stamp 2 has to bit-bang the protocol to some degree. Since the DS1307 is 5V only it doesn’t interface directly to the Propeller, whereas the DS1302 has multiple voltages it runs at. The DS1302 also has a built-in charging circuit for batteries or Super-Caps and its time-keeping current consumption is lower than the DS1307. I guess the real question is, when are we going to get you converted to using the DS1302? =)

    In all fairness though, I have actually been looking at some of the newer SPI offiering for RTC chips from maxim. In particular the ones with integrated crystal that are listed as high-accuracy. These don’t tend to come in a DIP package, so we’ll see how far I get on that. I was looking at the DS3234 in case you’re wondering what my next experiment will be on. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • Pascal PPascal P Posts: 26
    edited 2009-03-05 16:09
    Thank you very much Chris for posting this. You've probably saved many people weeks of trouble and the·code is very elegant. Well done.

    Take care, Pascal
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-03-05 16:32
    Pascal,

    I’m glad you found it useful. As the DS1302 is so easy to interface I find it useful to add to many projects I didn’t ordinarily need an RTC in. Due to the wide operating voltage and ease of use, the DS1302 was included on the Propeller Professional Development Board. We should have an official object supporting it very soon. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • Engineer09Engineer09 Posts: 16
    edited 2009-03-23 17:36
    I ran the provided code and it works fine; I have a question on how to make logical decisions based on time:

    The idea is to have certain microcontroller actions performed at certain times or between certain times.

    I am using the 24 hr mode so if I wanted somthing to be done between 11am and 2pm, I would be looking between 11 and 14

    I tried to make use of the variable hrs but got nowhere (seemingly gibberish when used with DEBUG).

    I haven't spent much time with the sample code but I would much appreciate a quick example or suggestions on how to make logic cecisions based on time and also where such coe might be implemented (I was testing it inside· Show_time subroutine).

    ·
  • Thomas StickneyThomas Stickney Posts: 23
    edited 2009-03-23 22:02
    Make a word variable called rawtime
    assuming you have 2 variables

    long minutes
    long hours

    The RTC should be in 24 hour mode for this
    to work.
    and then after reading the RTC, use
    some code to convert BCD minutes & BCD hours
    to integer

    rawtime := hours * 60 + minutes.

    A 24 hour day will = 1440 minutes

    at midnight rawtime will = 0
    at 9:00 am = 540
    noon = 720
    at 15:00 = 900
    ...etc

    To do something between 10:00 am to 14:00

    (10:00) (14:00)
    if rawtime => 600 and rawtime <= 840
    'do some task

    hope this helps.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-03-24 18:09
    Hello,

    Using rawTime was something Jon Williams did with the DS1307 code in the Stamp Works manual. Some customers asked for a different way to handle time comparisons though because that method is not intuitive for everyone and is limited by a resolution of 1 minute.

    To compare values to the current time it is important to understand how the values are stored in the DS1302 memory, especially depending on 12/24 mode. The values in the registers are stored in BCD (Binary Coded Decimal) format. Essentially they are packed values, two nibbles stored in a byte. Since BCD uses values from 0-9 for each 4-bit nibble, when you treat the value like a decimal number it doesn’t work out. For example, in BCD 88 looks like 136 decimal, however in Hexadecimal it looks like $88.

    So one way you can compare hours, minutes and seconds is to use Hex values for the compare. So if you want to trap for 12:45:56PM you could say:

    IF hours = $12 AND minutes = $34 AND seconds = $56 THEN

    Whereas if you used:

    IF hours = 12 AND minutes = 34 AND seconds = 56 THEN

    It would not work. Again, it is the concept of two nibbles stored in the byte that make it this way. The second option for dealing with these BCD numbers, especially if you want to be able to increment and decrement them is to use two small subroutines to convert the value between BCD and Decimal. This can be accomplished quite easily with the following two routines. I hope this helps out. Take care.

    [color=#000099]BCD_Decimal:
      counter = (counter.NIB1 * 10) + counter.NIB0
      RETURN[/color]
    [color=#000099]                                ' Convert BCD To Decimal[/color]
    [color=#000099]Decimal_BCD:
      counter = (counter / 10 << 4) + (counter // 10)
      RETURN [/color]
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • whirlymarkwhirlymark Posts: 10
    edited 2009-07-01 23:41
    Chris

    Thanks for the code to help with the DS1302, made my life a lot easier. Just a note that your DS1302_Template file needs a line added. At the moment the time is displayed continually in DEBUG. So I inserted a DEBUG HOME at the start of the Show_Time subroutine and now it just displays the time on one line.

    e.g. from your code with DEBUG HOME inserted.

    Show_Time:
    ' This routine uses DEBUG to display the Date/Time information but you
    ' would customize the routine to display the data you want on the device
    ' you want. Not all information needs to be displayed.


    DEBUG HOME ' Time displayed on a single line in DEBUG window
    LOOKUP (day - 1), [noparse][[/noparse]Sun, Mon, Tue, Wed, Thu, Fri, Sat], work

    DO

    rest of Chris' magnficient code.........
Sign In or Register to comment.