SMILEY’S WORKSHOP
CylonEyes.c into CylonEyes.hex. If
you raise the hood on WinAVR, you
would see a massively complex set of
software that has been created over
the years by folks involved in the
open software movement.
When you have questions about
WinAVR — and you will — check out
the forums at www.AVRFreaks.net,
especially the GCC forum, since
WinAVR uses GCC to compile the
C software. Try searching the forums
before asking questions since
someone has probably already asked
your question and received good
responses. Forum helpers tend to get
annoyed with newbies who don’t do
sufficient background research before
asking questions.
■ FIGURE 11. Base board with CylonEyes.
Joe Pardue ( nv@smileymicros.com) has a BSEE and operates www.smileymicros.com from the shadows of
the Great Smokey Mountains in Tennessee. He is author of Virtual Serial Port Cookbook and C Programming
for Microcontrollers.
The Main() Thing
All C programs must have a ‘main’ function that
contains the code that is first run when the program
begins.
understand they do make working with microcontrollers
easier, but for now, just humor me. The program tests
the while(1) and finding it true, proceeds to the ‘for’
statement, which is also true and passes to the line:
PORTD = i;
int main (void)
{
// Do something
}
Listing 2 shows what CylonEyes has. In this function,
we leave C for a moment and look
at things that are
specific to the AVR microcontroller.
The line:
Listing 2
int main (void)
{
DDRD = 0xFF;
int i = 0;
// set PORTD for output
DDRD = 0xFF;
sets the microcontroller Data
Direction Register D to equal 0xFF.
This tells the microcontroller that
port D pins, which are hooked up to
our LEDs, are to be used to output
voltage states (which we use to turn
the LEDs on and off). We use the
hexadecimal version, 0xFF, of 255
here because it is easier to
understand what’s happening.
You disagree? Well, if you persist
with these workshops, you’ll be using }
hexadecimal numbers like a pro and
}
while(1)
{
}
}
This causes the microcontroller to set the port D
pins to light up the LEDs with the pattern made by the
value of i.
Say what? Okay, ‘i’ starts off equal to 1, which in
binary is 00000001. This provides +3V on the rightmost
LED, and leaves the other LEDs
unlit at 0V.
The first ‘for’ loop runs eight
times, each time moving the lit LED
to the left, then it exits. In the next
‘for’ loop, the -= operator subtracts
i/2 from i and sets i equal to the
results causing the LED to move to
the right. When it is finished, the
loop runs again ... for how long?
Right! Forever! Or at least until
either the universe ends or you
unplug the Butterfly. We skimmed
over a lot that you’ll see in detail
in later workshops. You now know
just enough to be dangerous and
I hope the learning process hasn’t
caused your forehead to do too
much damage to your keyboard.
NV
for(i = 1; i < 128; i = i*2)
{
PORTD = i;
_delay_loop_2(30000);
for(i = 128; i > 1; i -= i/2)
{
PORTD = i;
_delay_loop_2(30000);
September 2008 59