B4R Tutorial RTC DS1302 Example

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))
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…