Listing 1
// CylonEyes.c
#include <avr/io.h>
// The last character is a lower case ‘L’ not a 1 (one)
#define F_CPU 1000000l
#include <util/delay.h>
int main (void)
{
int i = 0;
// set PORTD for output
DDRD = 0xFF;
while(1) {
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);
}
}
return 1;
}
Download CylonEyes to the Butterfly
In Workshop 1, you hooked the Butterfly to a RS-232
cable and downloaded your name. Hook it up again and
access the Butterfly bootloader by turning the Butterfly
off, then pressing the joystick button to the center, and
Expressions, Statements, and Blocks
Expressions are combinations of variables, operators,
and function calls that produce a single value.
For example:
PORTD = 0xFF – counter++
This is an expression that sets the voltage on pins on
port D to +3V or 0V based on the value of the variable
‘counter’ subtracted from 0xFF (a hex number — we’ll
learn about these and ports later). Afterwards, the
counter is incremented.
Statements control the program flow and consist
of keywords, expressions, and other statements.
A semicolon ends a statement. For example:
TempInCelsius = 5 * (TempInFahrenheit-32)/9;
This is a statement that could prove useful if the
Butterfly’s temperature readings are derived in Fahrenheit
but the user wants to report them in Celsius.
SMILEY’S WORKSHOP
holding it pressed while
turning the Butterfly
back on.
Back to the AVR
Studio ... Open the Tools
menu and WHILE
JOYSTICK BUTTON
PRESSED, click the ‘AVR
Prog…’ menu item. In the
AVRProg window, browse
to find the CylonEyes.hex
file. Click on the ‘Flash’
‘Program’ button. You
should see the progress
bar zip along and
AVRProg will say:
‘Erasing Programming ■ FIGURE 8. AVR Prog.
Verifying OK.’
If instead of the
window shown in Figure
8, you get the dreaded
‘No supported board
found’ window shown in
Figure 9, then you will
need to look at the ‘Using
AVRProg with the AVR Butterfly’ pdf file in the ■ FIGURE 9
Workshop 2 downloads at www.nutsvolts.com and
www.smileymicros.com. Don’t feel too bad, lots of folks
have trouble getting over this hurdle, but once you get it
working it is smooth sailing from here on out. (Okay,
that’s a lie; this stuff is always hard. So be careful, patient,
and persistent.)
Blocks are compound statements grouped by open
and close braces: { }. For example:
for(i = 1; i < 128; i = i*2)
{
PORTD = ~i;
_delay_loop_2(30000);
}
This groups the two inner statements to be run,
depending on the condition of the ‘for’ statement which
will be explained next.
Flow Control
Flow control statements dictate the order in which a
series of actions are performed. For example: ‘for’ causes
the program to repeat a block. In CylonEyes, we have:
for(i = 1; i < 128; i = i*2)
{
// Do something
}
September 2008 57