DS1302 Demo Code [BS2]

posted in: Tutorials 0

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.

DS1302 Demo Connections

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.

Revised (04-26-2004)

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.

Leave a Reply