The following program can be used to test all of the available functionality of the SD Plus data logging shield including: reading and writing to a DS1307 Real Time Clock (RTC) using the I2C protocol; writing date and time to the onboard SD card slot.

The imported files used (for example, SD and DS1307) are all part of the Firewing compiler install, so all you need to do is build the test program shown below and you should be good to go. The program will record date and time to a comma delimited file every minute, assuming an microSD card is plugged in. If using an SD card, enure it is correctly formatted for FAT 32 before using. In addition, the program will output the time and date to a PC via the Firewing serial to USB connection.

When using the sample code for the first time, you will need to set the real time clock. To do this, ensure a backup battery is installed on your board and then enter the time you wish to set using the subroutine SetRTC(). You would normally set this a few minutes into the future, as we will need to synchronise the time later. Also, make you you remove the comments that disable the call to SetRTC(). For example,

' set the clock, if required...  

You now need to

  • Build and then program your sample code into the main Firewing development board.
  • Wait until a clock or watch shows the same time you previously programmed, then press the reset button. This will synchronise the real time clock time to real world time.
  • Put the comment back in before the call to SetRTC().
  • Build the program again and reprogram. This will ensure that next time your code runs, the real time clock value is not changed.

The SD output will look like this:


and the console output will look like this:

 10:47:00 MON 22 APR 2013
 10:48:00 MON 22 APR 2013
 10:49:00 MON 22 APR 2013
 10:50:00 MON 22 APR 2013
 10:51:00 MON 22 APR 2013

The full source code can be download from here.

Program Listing

' file imports...
imports DS1307                       ' import RTC module
imports SD                           ' secure digital (SD) module

' program variables...
private Time As RtcTime                  ' time variable
private Date As RtcDate                  ' date variable

' main program entry point...
sub Main()
   dim sdAvailable as boolean = SD.Init() = errOK 
   dim canWriteToSD as boolean = true

   ' set the clock, if required...  
   ' SetRTC()   
   Console.Write("sdAvailable = ", cstr(sdAvailable), 13,10)  
   while true

      ' read time and date...
      Time = DS1307.ReadTime()
      Date = DS1307.ReadDate()  

      ' write to SD card...  
      if Time.Second > 0 then canWriteToSD = true
      if sdAvailable and canWriteToSD and (Time.Second = 0) then   

         ' output to PC...
         Console.Write(Str(Time.Hour, 2),":",Str(Time.Minute,2),":",Str(Time.Second,2))        
         Console.Write(" ", GetDay(Date.DayOfWeek))
         Console.Write(" ", Str(Date.Day,2))
         Console.Write(" ", GetMonth(Date.Month)," 20",Str(Date.Year,2),13,10)  

         canWriteToSD = false
         if not SD.FileExists("log.csv") then
         end if
         if SD.Append("log.csv") = errOK then 
            SD.Write(Str(Date.Day,2), "/")
            SD.Write(Str(Date.Month,2), "/")
            SD.Write(Str(Time.Hour, 2),":",Str(Time.Minute,2))
         end if             
      end if 
   end while
end sub

' return the day of the week as a string value...
Function GetDay(index As Byte) As String
   if (index < 1) or (index > 7) then return "XXX"
   Const days(7) As String = {"MON", "TUE","WED", "THU", "FRI", "SAT", "SUN"}
   return days(index - 1)
End Function

' return month as a string value...
Function GetMonth(index As Byte) As String
   if (index < 1) or (index > 12) then return "XXX"
   Const months(12) As String = {"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"}
   return months(index - 1)
End Function         

' set RTC... 
Sub SetRTC()

   ' set the time...   
   With Time, Time.Mode
      .Hour = 15
      .Minute = 06
      .Second = 0
      .Is12Hour = false
      .PM = false
   End With

   ' set the date...
   With Date
      .Day = 1
      .DayOfWeek = 1
      .Month = 4
      .Year = 13
   End With
End Sub