Shop OBEX P1 Docs P2 Docs Learn Events
Ds1302 — Parallax Forums

Ds1302

dbjdbj Posts: 75
edited 2007-04-01 02:47 in General Discussion
I have be looking for some code to display time date mounth year, all I have found is a demo that works with Hyperterminal, I would like to output to a BPI-216 serial·Lcd·module, and srore time data on SX28.

··· The serial data is read threw the I/Os how would you configure the pins would it be something like this,
Clk···· ·· VAR···· RC.0
Dta······ VAR···· RC.1
RTCCS·· VAR···· RC.2

when you get the data it is in BCD can you convert it to binary?
····················································· Thanks
·

Comments

  • Sparks-R-FunSparks-R-Fun Posts: 388
    edited 2006-12-06 18:41
    dbj,

    A byte value in Binary Coded Decimal notation will typically represent two decimal digits, the smallest in the lower nibble, the highest in the upper nibble. For example, the decimal number ninety-five (95) would look like %10010101 in a BCD byte.

    To extract the digits five (5) and nine (9) from the BCD byte and place them in their own separate bytes, you could do the following.

    For the lower digit perform a logical AND operation with the hex value $0F and the BCD byte. The result is the lower digit, in this case five (5).

    For the upper digit perform a logical AND operation with the hex value $F0 and the BCD byte. Then divide the result by 16. This should leave a binary representation of the second digit, nine (9) in this case.

    I hope this helps.

    - Sparks
  • JDOhioJDOhio Posts: 72
    edited 2006-12-07 02:12
    I did a project like this where I could edit the time and date (BCD to Binary and Binary to BCD), and displayed time and date on an LCD.

    The time and date are loaded into an array from the DS1302. I convert it to binary in the same array using BCDToBinary. When I needed to convert it back, I used BinaryToBCD. When I wanted to display time and date, I used WriteDataToDisplay.

    SendByte is:

    SendByte:
        temp1 = __PARAM1                ' save the byte
        IF __PARAMCNT = 2 THEN            ' "count" specified?
            temp2 = __PARAM2                ' yes, save
        ELSE
            temp2 = 1                    ' no, set to 1
        ENDIF
        DO WHILE temp2 > 0
            SEROUT DISPLAYComPort, DisplayBaud, temp1    ' transmit
            DEC temp2
        LOOP
    RETURN
    
    



    Joe
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-03-24 14:25
    Can I assume your program is for a DS1302?

    Also do you have any schematics of how to connect it up?
  • JDOhioJDOhio Posts: 72
    edited 2007-03-27 01:52
    The program is for the DS1302. Here is the schematic of how I connected a DS1302 to the SX-48.

    Joe
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-03-27 02:12
    JDOhio,

    This is pretty much what I am trying to do - except to display the information out to my 16x16 LED matrix on my SX52. I can display 16x16 and 8x16 and 16x8 information and do some scrolling but I am almost out of RAM space. I can stream line it down somewhat.

    I see in a previous post that your code is being sent to an LCD but I don't see any reference to the LCD output in your posted code. Has this been updated and can you repost the latest and provide a little more detail to the design and code.

    This is the closest I have seen to a DS1302 interface for the SX chips to date.

    I would like anything you can share about your design and code.

    Thanks so much.
  • JDOhioJDOhio Posts: 72
    edited 2007-03-28 00:26
    I am using the 2x16 Parallax display which has a serial interface. It accepts ASCII characters for display and for control. So, the SendByte subroutine (repeated here) provides for sending a byte to the LCD.

    SendByte:
        temp1 = __PARAM1                ' save the byte
        IF __PARAMCNT = 2 THEN            ' "count" specified?
            temp2 = __PARAM2                ' yes, save
        ELSE
            temp2 = 1                    ' no, set to 1
        ENDIF
        DO WHILE temp2 > 0
            SEROUT DISPLAYComPort, DisplayBaud, temp1    ' transmit
            DEC temp2
        LOOP
    RETURN
    
    



    DISPLAYComPort can be any I/O port; mine is

    DISPLAYComPort    VAR        RE.7
    
    



    DisplayBaud is T19200.

    The output of the DS1302 is binary coded decimal. This is pretty easy to convert as described by Sparks-R-Fun, and implemented in an attachment of one of my posts. My previous attachment would probably have to have the SendBytes subroutine above included in order to fully operate (my code that I am using now has not changed since my original post).

    In the previous attachment:

    SetTimeAndDate
    Fill the array named TimeDate before calling this. It places the information in TimeDate into the DS1302.

    GetTimeAndData
    Retrieves the complete time and date from the DS1302 and places it into the TimeDate array.

    ToggleBatteryBackUp
    The provides for turning the charging circuit off and on.


    For the following subroutines, I recommend setting the cursor to the far left so that the display accomodates the information.
    WriteTimeToDisplay
    This writes the hour and minute to the display.

    WriteDateToDisplay
    This writes the date to the display.

    WriteDayToDisplay
    This writes the day of the week to the display.

    WriteDataToDisplay
    This is a single subroutine that provide for sending information to the display. The information is delivered in arguments.


    So, to display the date and time might go something like

    'set up TimeDate array
    SetTimeAndDate
    
    DO
       GetTimeAndDate
       IF TimeDate(Secs) <> temp1 THEN
          WriteTimeToDisplay
          WriteDateToDisplay
       ENDIF
       temp1=TimeDate(Secs)
    LOOP
    
    



    Does this help? Let me know.

    Joe
  • T&amp;E EngineerT&amp;E Engineer Posts: 1,396
    edited 2007-03-28 01:01
    I have a few Parallax's LCD displays that I can try this out later this week. You have been so helpful and I thank you for this like others reading this I'm sure are too (as very little exists on this for the SX to interface to a DS1302). I will let you know my outcome.

    Thanks again!
  • T&amp;E EngineerT&amp;E Engineer Posts: 1,396
    edited 2007-03-28 18:51
    JDOhio,

    I tried to clean up the code a bit as a few items were not there. However, I am still geting some compiling errors that I think you should look over if you·have time.

    Thanks for the support!
  • JDOhioJDOhio Posts: 72
    edited 2007-03-29 00:58
    Glad to help! The code compiles but I was not able to test it. I removed a few things, changed a few things, and added some code.

    Joe
  • T&amp;E EngineerT&amp;E Engineer Posts: 1,396
    edited 2007-03-29 11:37
    Joe,

    This looks great. I will try it out over tonight or over the weekend. Thanks for supporting this effort!

    I will let you know my findings.


    Post Edited (T&E Engineer) : 3/29/2007 2:07:55 PM GMT
  • T&amp;E EngineerT&amp;E Engineer Posts: 1,396
    edited 2007-03-31 12:21
    Joe,

    The code you sent is really close to being perfect. However, what I have seen is if any of the date or time information is set to anything higher than 09 (e.g. 10, 28, etc.) than the second digit is the wrong character.

    For example:

    TimeDate(Secs)=00
    TimeDate(Mins)=06
    TimeDate(Hrs)=09
    TimeDate(Date)=09
    TimeDate(Month)=03
    TimeDate(Day)=3
    TimeDate(Year)=07
    TimeDate(Ctrl)=0
    SetTimeAndDate

    produces this:

    09:0603/09/07_

    However if the "Mins" or "Date" for example is changed to anything greater than 09 such as "Mins" is 10 and Date is "28" then:

    TimeDate(Secs)=00
    TimeDate(Mins)=10
    TimeDate(Hrs)=09
    TimeDate(Date)=28
    TimeDate(Month)=03
    TimeDate(Day)=3
    TimeDate(Year)=07
    TimeDate(Ctrl)=0
    SetTimeAndDate

    produces this:

    09:0:03/1</07_

    I will look through the code, but if you see what the issue is, it would be helpfull. No one is better to see a problem than the one that wrote the program I would say.

    It would also be nice to have a space between the time and the date too.

    Let me know if you know what the problem is and I will also look and see if I can find what it is too. Adding seconds would be cool too.

    Like this:·· 09:35:57· 03/31/07



    Great job!!!
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-03-31 21:03
    Since the values in the DS1302 are stored in BCD, the closest thing would be to specify them in HEX. When you specify decimal 10 that is an invalid BCD number. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • JDOhioJDOhio Posts: 72
    edited 2007-03-31 23:44
    Thanks!

    Chris is correct. Sorry I missed that - to specify a value greater than nine, try:

    TimeDate(Date)=$28    'note the dollar sign ahead of the value - this indicates a hex value to the compiler
    
    



    To show seconds and add space between the time and date, change WriteTimeToDisplay to:
    WriteTimeToDisplay:
        WriteDataToDisplay TimeDate(Hrs),":"
        WriteDataToDisplay TimeDate(Mins),":"
        WriteDataToDisplay TimeDate(Secs)," "
        SendByte " "
    RETURN
    
    



    Joe
  • T&amp;E EngineerT&amp;E Engineer Posts: 1,396
    edited 2007-04-01 02:47
    IT IS DONE !!!!! AND LOOKING GREAT.

    It is the first SX28/48/52 to a DS1302 Real Time Clock with an LCD interface (at least that I have seen on this forum).

    Everything is sent to a 2x16 Serial LCD. I modified and cleaned up the code to make it easy to follow. It also runs in either 12 or 24 mode formats. I have done a great deal of testing and I believe it is ready for anyone that wants to build it.

    It uses RA.0 - RA.3 leaving RB, RC (RD and RE on SX48/52) free.

    The LCD displays are centered and smart in it's display scheme.

    If 12 hour format is choosen it looks like this:

    ·· 11:59:50 PM

    ···12/31/07 Mon

    ·or in 24 hour format:

    ··· · 23:59:50

    ·· 12/31/07 Mon



    I think you will like it alot - like·I do!

    Complements should go to the original·author "JDOhio" (Joe)·(from which I modified his code) !!



    Also thanks to Chris for suggesting the HEX character usage instead of BCD conversion.· I tried some routines from JonnyMac but could not get them to work (BCD2DEC and DEC2BCD). However, simple HEX characters did the trick.

    ENJOY !!!

    I will next try to use this to go out to my 16x16 LED matrix display I made·and display the·date and time (scrolling would be·nice too).

    However, this project was the first step in making this into a·reality.


    Post Edited (T&E Engineer) : 4/1/2007 2:59:22 AM GMT
Sign In or Register to comment.