with this project, but when we start getting into low
power projects this becomes critical.
One of the disadvantages to using the
MCP79400 is that it doesn’t come in a nice
breadboard-friendly DIP package — meaning that you
need a breakout board. This was a compromise I was
prepared to make based on a combination of the
price and the voltage range — not many other RTCs
offer the range at the price (for example, the very
popular Maxim DS1307 only operates at 5V).
Additionally, a breakout board for an RTC is a pretty
useful thing — you’ll see that the RTC needs a number
of components which can be nicely wrapped up into
a breakout board.
Figure 6: Breadboard build: the EEPROM module.
Onto the connections! Again, the SDA and SCL
lines have pull-up resistors; although this time, they’re
2.2K resistors. If you take a look at the datasheet,
you’ll see that a range of pull-ups will work, although
do affect the possible operating speed. However, this
is a longer discussion for another time!
Then Include It
Just as it is good coding practice to separate your code
out into modular functions, it is good practice to group similar
functions into distinct files. For example, in this article’s
project, there are code files containing functions relating to
UART communication, accessing the EEPROM,
communicating over I2C, etc. These files have been given
meaningful names that reflect their purpose.
A watch crystal/tuning fork crystal (i.e., rated at
32.768 kHz) is connected to X1 and X2. This is needed to
provide accurate timekeeping, along with a couple of 8. 2
pF capacitors — similar to the configuration for the crystal
on your main microcontroller. VBAT is for the backup
battery, but here is attached to GND as we don’t have
one connected. Finally, the MFP (multi-function pin) is
kept high with a 10K pull-up resistor — this pin can be
used to indicate when alarms are triggered, to output a
square wave (let’s say you want an LED to flash as a
second indicator), or even be an additional GPIO.
Separate Your Code,
I’d be doing a poor job if I tried to get into a discussion
on the best coding practices for your projects — this is a
whole field in itself and not one that I can claim to be an
expert in; a search on the Internet will throw up a number of
academic and practical books on the subject. However, for
our purposes, I’m comfortable that we’ll see the benefits as
enthusiasts. Separate files mean that it’s easier to find
specific functions without wading through a single lengthy
program file. By testing a file completely and making sure it
can stand alone, you’re able to use this in other projects
without re-testing. You know that your (for example) UART
functions are sound and can be included in any future
projects for “plug-in” functionality.
Whew! That was a quick overview!
We’ve covered some of the theory and the physical
layout. Now, you can put down your breadboard and fire
up Atmel Studio. Let’s get coding!
Download the project from
the article link and open it up in
You’ll have noticed that there are two files: a header file
(e.g., “UART.h”) and a source file (e.g., “UART.c”). The header
file should not include any functioning code — only
definitions, macros, function prototypes, etc. The source file
needs to “include” the header file — for example, “UART.c”
has a statement at the start:
Atmel Studio. Then, take a look at
the solution explorer tree; you’ll
see something like Figure 7.
You probably noticed that
there are more files than before:
I2C.c, I2C.h, UART.c, and so on.
As this project is getting large,
This brings the header file and its definitions into the
source code file.
Additionally, all the header files need to be included in
the main code file. Look at the start of the main “c” file for the
project and you’ll see all the “#include” statements there.
We’ll probably touch on header files at a later stage, but
this will give you enough background to get started.
I’ve separated out various
groupings of functionality into
separate files — this makes our
lives a great deal easier. Look at
the sidebar, “Separate Your
Code, Then Include It” for some
more detail. Take a quick look
September 2015 57