DS1302 Demo Code (Updated)
Chris Savage
Parallax EngineeringPosts: 14,406
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
·· 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
Comments
Thanks
·· 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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
···· 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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
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
·· 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
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 . 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
Edit: hah! I got it -
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!
AHHHH the joy of discovery!
Aloha,
Robert
Post Edited (Robert@HCC) : 1/19/2006 9:16:01 AM GMT
·· 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 Savage
Parallax Tech Support
csavage@parallax.com
made my day. gonna work on this all day.
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.
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
Thanks for the quick reply.···························································································································· I'll try that.
Regards,
Mike D.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
M.D.
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
·
·
·
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··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
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 Savage
Parallax Tech Support
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··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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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
Take care, 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
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).
·
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.
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Engineering
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.........