B4R Tutorial RTC DS1302 Example

Hi,

this is an example of using the Real Time Clock DS1302 (from sunfounder).
There are two versions:
1. Manual: Set the time manual befor uploading. After uploading the time is logged every seconds by a timer.
2. Commands: Various commands are accepted via the serial line (using Asyncstream):
  • set: Set the RTC time year, month , dayofmonth , hour , minute , second , dayofweek
  • get: Get the RTC timeformat YYYY-mon-dayofweek hour:minute:second dayofweekname
  • getbytes: Get the RTC time as an array of bytes with year, month , dayofmonth , hour , minute , second , dayofweek
  • start / stop: Start & Stop Logging
Wiring
B4X:
DS1302 ---- Arduino (Wirecolor)
SCL   ------- 7 yellow
SDA  ------- 6 brown
RST  ------- 5 white
VCC  ------- 3.3v red
GND ------- GND black

upload_2016-6-2_13-1-59.png


2016.06.2 First Version
  • The DS1302 library is included. Copy to the Arduino IDE Libraries in folder ds1302.
  • The code uses Inline C with ds1302.h included.
  • Have not been successful with the attempt to wrap the library DS1302, therefor solved using Inline C.
  • A type is used for the timestamp.
  • Overall, must say, was not easy to get started - several wrapping attemps not successful, then finding best way to handle timestamp went back and forth ... but anyhow this is now working fine.
  • Next: when the I2C LCD arrives will amend with a clock.
  • Source code attached and documented.
2016.06.03 Update
  • New library rDS1302 wrapping DS1302.
  • Build two versions Manual and Commands using the rDS1302 Library.
  • Attachment updated which contains:
  • Example using Inline C with the DS1302 Library (rtcds1302-inlinec-command, rtcds1302-inlinec-manual).
  • Example using the wrapped library rDS1302 (rtcds1302-lib-command, rtcds1302-lib-manual).
  • Examples tested with an Arduino UNO and Sunfounder DS1302.
  • The new rDS1302 is in the attachment folder Library rDS1302.
B4R Serial Connector example for setting the date & time. A must tool for testing.
upload_2016-6-3_17-34-45.png
 

Attachments

  • B4RHowToRTCDS1302.zip
    347.2 KB · Views: 1,247
Last edited:

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi All,

based on previous posts, redeveloped the library as confirmed time updates in cases not working when a device got disconnected and connected again.
Tested the new library with an Arduino UNO and running OK.

Notes
  • changed the function CurrentTime to GetTime.
  • new examples settime and gettime.

Note: Have not updated #Post #1 yet as any feedback appreciated if library is working on other devices as well.

Download and Reference .
 

Mostez

Well-Known Member
Licensed User
Longtime User
first, thanks for updates. In this sub, last line may generate error because day of week in DS1302 and DayOfWeekArr do not have same base index (DS1302 is base 1, and day of week array is base 0)
B4X:
Sub GetRTCTime
    'The current time must be defined in the sub. The global can not be used.
    Dim currenttime As DSTime = ds1302.GetTime
    Log("RTC Time ", _
        NumberFormat(currenttime.years,4,0), " ", NumberFormat(currenttime.months,2,0), "-", NumberFormat(currenttime.dayofmonth,2,0), _
        " ", _
        NumberFormat(currenttime.hours,2,0), ":", NumberFormat(currenttime.minutes,2,0), ":", NumberFormat(currenttime.seconds,2,0), _
        " ", _
        DayOfWeekArr(currenttime.dayofweek))
End Sub

the line should look like this:

B4X:
DayOfWeekArr(currenttime.dayofweek - 1))
 

rwblinn

Well-Known Member
Licensed User
Longtime User
last line may generate error because day of week in DS1302 and DayOfWeekArr do not have same base index (DS1302 is base 1, and day of week array is base 0)

Thanks for the hint.

Have changed the size of the array in the examples to cater for library Sunday = 1, Monday = 2...
Snippet
B4X:
' The first entry is a dummy because the library uses sunday as 1, monday as 2 etc.
Public DayOfWeekArr() As String = Array As String("", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
...
ds1302time.DayOfWeek = 5   'Sunday = 1, Monday = 2 etc.
...
 DayOfWeekArr(currenttime.dayofweek))
 
Top