Ds1302
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
·
··· 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
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
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:
Joe
Also do you have any schematics of how to connect it up?
Joe
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.
DISPLAYComPort can be any I/O port; mine is
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
Does this help? Let me know.
Joe
Thanks again!
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!
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
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 Savage
Parallax Tech Support
Chris is correct. Sorry I missed that - to specify a value greater than nine, try:
To show seconds and add space between the time and date, change WriteTimeToDisplay to:
Joe
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