Shop OBEX P1 Docs P2 Docs Learn Events
DS1302 12 Hour Mode Demystified! — Parallax Forums

DS1302 12 Hour Mode Demystified!

lrohnlrohn Posts: 33
edited 2005-03-29 01:22 in BASIC Stamp
First off, I wanted to thank all of you in the group who took the time to help me out in learning to understand the DS1302 and the code around making it function properly. I would like to thank Dave G, Sid, Chris and Jon to name a few.

I have only started to learn the Stamp a few months ago when I stumble across the "What's a Microcontroller" package that Radio Shack is now carrying. Let me say I am having a blast and have all kinds of project that I want to create using microcontrollers. Programming and Microcontrollers are extremely new to me so it nice to have a support forum like this, I'm sure many other will agree with me. When I started, I knew nothing about the basic programming language. Thanks to Parallax I am starting to understand and appreciate it.

Not knowing much of anything about the different numerical formats other then the common decimal format didn't help matters either, so I had to spend the last week scouring the web to get up to speed with the rest of you. Now I all I will ever want to know about binary, hexadecimal and binary coded decimal (BCD).

The latter (BCD) being very critical to understanding and making the 12 hour mode work properly with the DS1302. The HEX formatter makes it extremely easy to read and write to the DS1302 and exploit the BCD format that's used in this Real Time Clock.

Before I go on, you should understand that Binary Coded Decimal (BCD) stores a value as two nibbles, the 10's digit of a value in the upper nibble of a byte, the 1's digit in the lower nibble of a byte. The Lower nibble (Bits 0 - 3) in 12 hour mode will count from 0-9 in binary any other value is invalid. The same for the upper nibble. The HEX format is also nibble oriented and that's why it's used to exploit the BCD format of the DS1302.

As an example a 9 in BCD or HEX is 0000 1001 but a 10 is 0001 0000, 11 = 0001 0001 and 12 = 0001 0010. This works perfect fine in 12 hour mode. The reason in poses a problem in 12 hour mode is the fact that two BITS (BIT 5 and BIT 7) have to be modified for 12 hour mode.

The problem we have all be running into is that BIT 7 of the hours register needs to be a logic high (1) to put the DS1302 into 12 hour mode and BIT 5 is the AM/PM bit with a logic high (1) being PM. So 11 AM would be 1001 0001 and 12 PM would be 1011 0010. If you try to read this value in HEX you would get B2 for 12 PM and 91 for 11 AM.

The way to work through this is to mask the upper three bits by only reading BIT 4 of the upper nibble an entire lower nibble. I have ran the DS1302 for a week straight now and it has worked perfectly for the entire 7 days.

Below is a sample of my code that I run to display the time in the debug terminal. Because I am still fairly new I was hoping someone would look over it and maybe help me come up with a more efficient code that uses less memory:

FYI, you should know that CHmode is BIT 4 of the hours variable (CHmode VAR Hours.BIT4)
and also that AMPM is BUT 5 of the hours variable (AMPM VAR Hours.BIT5)

**************** CODE SAMPLE ********************

Update_Display:

IF CHmode = 0 THEN

DEBUG HOME, "The Time is: " , HEX hours.LOWNIB,":",HEX2 minutes ,":" ,HEX2 seconds

IF AMPM = 0 THEN

DEBUG " AM"

ELSE

DEBUG " PM"

ENDIF

ELSE

DEBUG HOME, "The Time is: 1" , HEX hours.LOWNIB,":",HEX2 minutes ,":" ,HEX2 seconds

IF AMPM = 0 THEN

DEBUG " AM"

ELSE

DEBUG " PM"

ENDIF


ENDIF

RETURN

********************** END OF SAMPLE CODE ********************


I hope this clears up anyone's question on how to run the DS1302 in 12 hour mode. If you still have some questions, I will be happy to try and help, please message me and I will see what I can do.

I also hope someone can help me write the code above in a more compact and effecient manner.

Thanks in advance and I love microcontrollers and I am proud to be part of this group!

Luke smilewinkgrin.gif

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2004-12-24 17:25
    Congrats Luke...

    You hit that problem like a Pit Bull!· I'm glad we could all help in our own way...As for your code, there's not enough there for me to figure out how you're getting the AM/PM info, but Jon Williams is the code-formatting wizard, so I will leave it to him, or someone else.· smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Tech Support
    csavage@parallax.com

    Post Edited (Chris Savage (Parallax)) : 3/24/2005 4:33:12 AM GMT
  • lrohnlrohn Posts: 33
    edited 2004-12-25 11:53
    Hello Chris,

    Thanks for your reply and Merry Christmas to you and yours.

    To get the AM/PM info I look at BIT 5 of the hours variable.

    I created another variable called AMPM (AMPM VAR Hours.BIT5)

    The following code displays AM or PM do the debug terminal (if BIT 5 is 0 then AM is displayed and if BIT 5 is a 1 then PM is displayed and the end of the line).

    ************ code snippet ***************

    IF AMPM = 0 THEN

    DEBUG " AM"

    ELSE

    DEBUG " PM"

    ENDIF

    ELSE

    ********** end of code snippet ************


    Luke smilewinkgrin.gif
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-03-24 04:28
    Luke,

    ·· Ran into this thread by accident searching for something un-related.· I totally forgot that we were working on the DS1302 12 hour problem.· But it's interesting because I had just spent the last few nights working on this issue myself, and I have some nice clean code for the DS1302's 12 Hour Mode.

    ·· I am using the code in a home-brew Digital Alarm Clock that I decided to build about a week ago, and I had been struggling with the issues you went through.· But I had existing code that I had been using in 24hour mode and converting for LCD displays.· The change was required for using the MAX7219.· That is how I will be driving the 4 7-Segment Displays.

    ·· The clock has Dual-Crescendo Alarms (Remembers 2 alarm times), Auto-Dimming of the display, battery backup of the clock, settings and alarm times (Using DS1302's RAM), and of course, it's run on a BS2.· More info to follow.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Tech Support
    csavage@parallax.com
  • lrohnlrohn Posts: 33
    edited 2005-03-24 05:19
    Hello Chris,

    Thanks for writing. I would like to see your code and the other info when you have the time.

    Thanks for thinking of me and keep up the great work in the forums!

    Luke
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-03-24 05:51
    Luke,

    ·· No problem...Right now the code is running on my NX-1000 board, and what I did was loaded my code from the Digital Thermostat and hacked it up a bit to handle output to the MAX7219.· Right now it's kind of a mess, but it works.· The fact is, it's still sending the Date/Time to the LCD, so I more or less hacked in the MAX7219 code without disturbing the LCD output too much.· This wasn't easy, sicne the LCD was running off DS1302 code for 24 hour format.· Needless to say, it's displaying on both a Serial LCD and the 4-digit display on the NX-1000.

    ·· The interesting thing with the MAX7219 code is that since it's going to end up in a Digital Alarm Clock, I needed extra LEDs for the colon, AM/PM indicator and ALARM indicators (2).· So I am actually using 6 digits from the MAX7219, but only 4 are visible.· Of the 6, the first 2 are only using the DP line for each ALARM LED.· Of the remaining 4 visible digits, the DP lines are running to 4 more discrete LEDs, of which 2 are the colon, and 2 are the AM/PM indicator.· In this manner I can change the display brightness with one command, and it affects ALL the LEDs and the 7-Segment displays.

    ·· What I need to do is leave it running until probably tomorrow when I get home, at which time I will make a stand-alone version which uses the DEBUG window.· This way it will be easier for people to adapt it to their specific application.· In the past when I posted my code, people found it difficult to figure out what I was doing in the display output (SEROUT) because of all the proprietary commands being sent to my serial LCD backpack.· So I want to standardize things a bit.· I will probably post both 12 and 24 hour code just so everyone can get it.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Tech Support
    csavage@parallax.com
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-03-26 02:49
    Luke,

    ·· One thing I forgot to mention is the code I post (Tonight or sometime this weekend) will include routines for easy access to the on-board RAM in the DS1302.· This is another area that seems to baffle people.· Working on it right now...· wink.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Tech Support
    csavage@parallax.com
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-03-29 01:22
    Hello Everyone,

    ·· Please see the projects fourm for the DS1302 Demo Code that was uploaded today.· Thanks!



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Tech Support
    csavage@parallax.com
Sign In or Register to comment.