and a USBTiny programmer) type with careful
attention to spaces:
“-c usbtiny -p m328p -v -v -v -U
d. Initial Directory: This is the path to AVRDude
e. Check “Use Output Window.”
4. Click on OK.
Figure 11 shows the settings for a USBTiny external
Once you’ve set up the external programmer, to
upload the code simply click on the configuration you’ve
created under the Tools menu and the program will be
uploaded. Messages will appear in your Output window,
and it should end with “avrdude.exe done. Thank you.”
Step 7: Watch It Blink
If you’ve found your way through all these steps,
disconnect your programmer’s USB cable from your PC
and connect a power source. You’ll now see the steady
reassuring blink of your LED. Give a long contented sigh!
When you snap out of the LED-induced trance, move
on to the next section to see how Atmel Studio
understood the code.
Dissecting the Code
Let’s wrap up by connecting the earlier discussion on
registers and bitwise operations with the actual Atmel
Studio code. I’ve added line numbers into the listing to
make the lines easier to reference (refer to Figure 12).
Line 13: F_CPU defines the speed that the clock is
operating at in Hz. We’ve added six zeros for a value of
16 MHz, as well as a “UL” to specify that the type is an
unsigned long. F_CPU is
commonly used in
libraries; for our purposes
here, we need it for the
(which resides in the
Line 15: This is
included by default in new
projects, and defines
(among others) the
“friendly” names for all the
registers (ports and pins)
we use here (remember
registers are actually
memory locations). These
names are microcontroller-specific of course.
Line 16: This is a library of utilities to perform delays.
We use the _delay_ms() function from this library later in
Line 19: Wait! Where have the setup() and loop()
structures gone? Well, there aren’t such things outside of
the Arduino IDE; just a main() function. Inside the main
function, you need to perform all your setup steps before
you enter an infinite while(1) loop. The while(1) is
equivalent to the loop() structure in the Arduino.
Line 22: DDRx is a “direction register” for port x — it’s
similar to the pinMode() function. So, DDRB sets the
direction for each of the pins on port B. A 1 means an
output and a 0 means an input. The DDB0 refers to pin 0.
Left-shifting (<<) a 1 by 0 positions actually leaves the 1 in
its original position — i.e., 0b00000001.
By ORing this with
the DDRB register, we are
simply setting bit 0 to a 1.
In other words, we’re
telling the DDRB register
that pin PB0 is an output.
Note that even though the
left shift here doesn’t
actually shift the bit to the
left (as it’s a zero), it’s
good practice to use this
syntax so as to be
Line 24: This while(1)
infinite loop is the same
as the loop() function in
the Arduino IDE.
Line 27: PORTx is the
April 2015 45
FIGURE 12: Dissecting the source code.
FIGURE 11: Configuring the USBTiny as an
external programmer within Atmel Studio.