Some time ago on the Parallax Support / Discussion Forums 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 DS1302 in both 12 and 24 hours modes, as well as intuitive access to the on-board SRAM. 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 easy and useful chip for keeping time, supporting 12 and 24 Hour modes, as well as having 31 bytes of SRAM 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 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 SRAM. All this is done in an easy manner, with explanations as to how the commands are formed. 12 and 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. DEBUG commands use large amounts of program space. The code is intended to be easily integrated into your own applications via the template and use your particular input / output devices. The demo code is intended to familiarize you with the various functions only.
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 battery 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 Parallax 2×16 Serial LCD Display @ 19,200 bps.
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.
Released under MIT License
Copyright (c) 2004 Chris Savage – Savage///Circuits.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.