rDS1302

Additional Open Source B4R Libraries.
rwblinn
Site Admin
Posts: 195
Joined: 07 Oct 2017, 12:16

rDS1302

Postby rwblinn » 21 Nov 2017, 15:24

rDS1302 - Library for the Real Time Clock.
Developed in C++.

Objectives
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:
Image

Wiring
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)

Download
Library with B4R source code including examples.

ToDo
• Library completed (unless bugs are reported)

Changelog
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

Sub Process_Globals
   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
End Sub

Private Sub AppStart
   Serial1.Initialize(9600)
   Log("AppStart")
   'Init the DS1302
   ds1302.Initialize(5,6,7)
   'Set the RTC time
   SetRTCTime
   'Set the timer reading the RTC time every second. 1sec 0 1000millisec.
   Timer1.Initialize("Timer1_Tick", 1000)
   Timer1.Enabled = True
End Sub

'Set the RTC time by using a DSTime var with values assigned.
'Ensure to update with actual date & time prior uploading.
Sub SetRTCTime
   Dim ds1302time As DSTime
   ds1302time.Initialize
   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.
   ds1302.SetTime(ds1302time)
End Sub

'Get the RTC time
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

'Log the time every second as set by the Timer Init
Sub Timer1_Tick
   GetRTCTime
End Sub

Return to “Libraries”

Who is online

Users browsing this forum: No registered users and 1 guest