INTRODUCING DATE AND TIME
ON A MICROCONTROLLER
As you may remember, the Arduino uses some
standard C and C++ stuff based on the resources in
WinAVR. Unfortunately, one of those resources (avrlibc)
does not implement the standard C library time.c and
time.h files. This is because the functions in those files
require some sort of built-in timekeeping facility that
usually comes with an operating system, but are not
available in a raw AVR that is small and has no OS.
In order to use these functions, we have to build our
own custom timekeeping system. Since there are so
many ways to do this, the folks who did avrlibc thought it
best to leave it to the programmer of a particular system.
We've seen how this is done with regular C and an
operating system by using Pelles C. Now, we will look at
a way of using some similar concepts on the Arduino
using an external IC — the DS1307 real time clock — as
our arbiter of dates and times.
Our Windows Alarm Clock Software
We probably could have written something to
communicate with our Arduino on the PC using Pelles C,
but I find Visual C# much quicker and with more
attractive results. So, I wrote the PC side application for
the Arduino alarm clock in Visual Studio C# Express 2012
for Windows Desktop.
I also wrote several articles about using a virtual
serial port with C# that were published in the January,
February, and March 2010 issues. These might provide a
good introduction if you aren't already familiar with
creating PC applications with C#.
■ FIGURE 8. PC date and time.
Two lines of code. How simple is that? Darn simple,
actually, but it is a real bear to figure out what you really
need to use from the many Date Time functions available.
For all I know, it may not be the best way. However, it
does work, so I'll live with it.
You have a choice of becoming an expert in C# or
relying on your Google-Fu and finding working examples.
I prefer the latter technique.
Sending the Date and Time to the Arduino
So, how easy is it to send the date and time from the
PC to the Arduino? Just look at the following code:
TimeSpan _UnixTimeSpan = (DateTime.Now - new
DateTime(1970, 1, 1, 0, 0, 0));
richTextBoxReceive.Text += "unixtime = " +
serialPort1.WriteLine("P" + _UnixTimeSpan
.TotalSeconds.ToString() + '!');
Showing the Date and Time on a PC
Microsoft uses a Date Time value type that can
represent Gregorian calendar dates and times from
00:00:00 (midnight) January 1, 0001 to 11:59: 59 P.M.,
December 31, 9999. This is divided into 100 ns ticks,
allowing us to represent any 100 ns interval in the above
range as a single number — a very big number — but
that's what PCs are good at. So, let it worry about storing
and converting the number of ticks from the beginning of
the Common Era to this very moment.
It is very easy to use Date Time in C# once you know
how, but it can be very hard to figure out how to use it at
first. That may sounds like some nonsense, but look at
the following example. We create the date and time
displayed on the left of Figure 8 using the following two
lines of code:
labelTime.Text = DateTime.Now.ToLongTimeString();
labelDate.Text = DateTime.Now.ToShortDateString();
You just instantiate a TimeSpan object to this
particular 100 ns interval, then subtract the number of
100 ns ticks since the beginning of Unix time, and send
that number to the Arduino. Yup, it's another totally easy
thing to do. (We won't mention the hours of Google-Fuing around the Internet it took to find those three lines
of code, however.) Luckily, you can get the C# code for
the PC side of the Arduino alarm clock at the link for this
article, so you can reuse these concepts for your own PC
side application. Just be aware that the code I'm
providing (while working just fine) looks a bit like road-kill
under a magnifying glass, so I recommend squinting
when you use it.