Developed in C++.
To connect a Real Time Clock (RTC) to an Arduino and set or get the time.
The library is wrapped from a C++ library based upon this open source project (many thanks to the author).
Tested with device "Sunfounder RTC DS1302" connected to an Arduino UNO:
DS1302 = Arduino (Wirecolor) - Other pin decsription
SCL = 7 (yellow) - SCLK
SDA = 6 (brown) - IO
RST = 5 (white) - CE
VCC = 3.3v (red)
GND = GND (black)
Library with B4R source code including examples.
• Library completed (unless bugs are reported)
v1.1.0 (20171122) - Redeveloped as updates in cases not working when a device got disconnected and connected again.
v1.0.0 (20160602) - First version.
B4R Example with set & get time
Code: Select all
Public Serial1 As Serial
' 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")
Public ds1302 As DS1302
Public Timer1 As Timer
Private Sub AppStart
'Init the DS1302
'Set the RTC time
'Set the timer reading the RTC time every second. 1sec 0 1000millisec.
Timer1.Enabled = True
'Set the RTC time by using a DSTime var with values assigned.
'Ensure to update with actual date & time prior uploading.
Dim ds1302time As DSTime
ds1302time.Years = 2017
ds1302time.Months = 11
ds1302time.DayOfMonth = 21
ds1302time.Hours = 11
ds1302time.Minutes = 25
ds1302time.Seconds = 0
ds1302time.DayOfWeek = 5 'Sunday = 1, Monday = 2 etc.
'Get the RTC time
'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), _
" ", _
'Log the time every second as set by the Timer Init