you intended. What if you really
meant to have the operations
performed in the order listed? Then
you would write:
x = ((((50 + 10) / 2) – 20) 4);
which would make x = 40. The
parentheses can get mighty
confusing, but not nearly as
confusing as their absence.
Getting Started with C
Libraries
Libraries: avr-libc library
Software libraries are repositories
of functions that have been
precompiled and stored as object
modules that the compiler/linker can
find and put into the code when
you want to use them. These library
functions are defined in a header
ile (filename ends with an .h suffix),
usually with the same name as
the library and have some
documentation that tells you how
to use the function (
avr-libc-user-manual.pdf which you can find at
the unlikely location C:\WinAVR-
20071221\doc\avrlibc\ if you
followed instructions and installed
WinAVR in the default location). You
saw an example of a header file in
PortIO.c from Workshop 3:
// PortIO.c
#include <avr/io.h>
Also, we are talking a lot about
functions here and haven’t really
addressed what a function is yet
other than the cursory preview given
in Workshop 2. We won’t get into
details for a few Workshops, but
briefly, a function encapsulates a
computation; it may return a value
and it may require input parameters.
The really great thing about
library functions is that you don’t
have to know how they do their job
and you never have to look at the
code that does it. In object-oriented
programming, this concept is called
encapsulation and carries with it the
idea that the less you can get your
hands on, the less you are likely to
64 November 2008
screw up. Now as insulting as that
may seem, it is nonetheless a very
good software engineering principle.
If it works and you can’t get at it,
then you can’t break it.
Let’s apply this by using the
library libsmws4.a to get the Butterfly
shouting some math at the PC.
This library will do things in the
background that will allow you to
send and receive data over the UART
without having to know a thing about
how it works.
We will also use two standard C
libraries: stdio and stdlib. From stdio,
we will use the standard C function
printf() and a special AVR modified
function printfP(PSTR()) which
allows us to store strings in Flash
memory which we have a lot of
rather than RAM (which we have
much less of). From stdlib, we will
use atoi() to convert an ASCII string
to an integer. Look at these functions
in the avrlibc manual and try not
to freak out too much over the
complexity since our job here is to
learn enough over time so that the
manual will make sense, eventually,
more or less.
Is Anybody Out There?
Communicating With a PC
Most microcontrollers are buried
deep in some device where they run
in merry isolation from the rest of the
world. Their programs are burned
into them and never change. But
there are many instances when we
might want to communicate with a
microcontroller, this being one of
them. The Butterfly uses a joystick
and an LCD, which is fine for its
built-in menu based applications.
For anything more complex — like
changing the microcontroller
software — nothing beats using the
PC’s serial communications port
to communicate with the
microcontroller.
What we need is a method to
send commands and data from the
PC and receive responses from the
Butterfly. In this section, we will
develop a generic command
interpreter skeleton that we will reuse
in later programs. In this project, we
will use this skeleton to build a
demonstration that lets the PC ask
the Butterfly to do some simple
math. Hey, bet you never thought
you’d be training a Butterfly to add,
subtract, multiply, and divide!
Writing MathCommunicator.c
Let me repeat: The
MathCommunicator files we are
about to use have many things in
them that are well beyond our C
training at this point, so just use them
and don’t think too hard about it yet.
We will revisit each function in later
Workshops as we increase our
knowledge.
Before creating the
MathCommunicator project in
AVRStudio, read the Smiley’s
Workshop 4 – Supplement: Adding
Libraries to Projects pdf file that
you can get from Nuts & Volts
( www.nutsvolts.com) or Smiley
Micros Workshop4.zip download
that also contains the AVRStudio
project.
#include <avr/io.h>
#include <stdio.h>
#include <stdlib.h>
// include the special library
// for Smiley’s Workshop 4
#include “smws4.h”
// define the parseCommand
// fuction
void parseCommand(char *,
uint8_t);
int main(void)
{
char b = 0;
char s[6];
uint8_t count = 0;
// this is in libsmws4
initialization();
// the _P and PSTR weirdness
// allows you to store a
// string in flash rather than
// wasting space in SRAM
printf_P(PSTR(“Math
Communicator at your
serv-ice!\n”));
while(1)
{
while(b != ‘=’)
{
// this is in smws4.a
b = (char)receiveByte();
s[count++] = b;
}