Overview of Blink Arduino Diecimila LED
Software Download: EB62E441-1C1B-43AC-9CEA-FF0AE8753833

This is a “Hello, World!” type of program that blinks the indicator LED on an Arduino Diecimila. The software consists of a GNU makefile and C-language source code. This is C programming in the UNIX tradition, using the AVR-GCC toolchain and AVR Dude. The Arduino integrated development environment (IDE) is not used.

Arduino is an open-source electronics prototyping platform. There are several versions of Arduino available. Each incorporates an ATmel megaAVR microcontroller. One popular Arduino variant is the Arduino Diecimila. The Arduino Diecimila incorporates an ATmel ATmega168 megaAVR microcontroller.

Arduino Diecimila prototyping board

To be useful, the microcontroller must be programmed with instructions. The writing and programming of instructions is called software development.

To develop software for the Arduino Diecimila you need three pieces of equipment: a PC, an Arduino Diecimila, and a USB cable. Software for the Arduino is written on the PC. The Arduino is connected to the PC by USB. Software is copied from the PC to the Arduino. The microcontroller on the Arduino executes the software.

Ordinary people develop software for the Arduino using the open-source Arduino IDE (integrated development environment). The Arduino IDE is software for your PC that implements the Processing/Wiring language. Persons write instructions in the Processing/Wiring language. This language is translated by the IDE into instructions the microcontroller can execute.

Persons who know the C programming language may prefer to write software for the Arduino in C. The C language also can be translated into instructions the microcontroller can execute. Concise documentation about how to develop software for the Arduino in C has been lacking.

To develop software for the Arduino in C, you need to install software on your PC.

Install the AVR-GCC toolchain and the AVR Dude loader on your PC. The toolchain converts C, C++, and Assembly language source code into instructions the microcontroller can execute. The loader copies the instructions from your PC onto the microcontroller by USB.

The AVR-GCC toolchain can be obtained free of charge from ATmel or the official Arduino website. The Arduino IDE and ATmel's AVR Studio IDE rely on the AVR-GCC toolchain. The easiest way to install the AVR-GCC toolchain on your PC may be to install one of the IDEs which rely upon it. In these instructions, you will not use an IDE. In these instructions, you interact with the AVR-GCC toolchain directly. The AVR-GCC toolchain is available for Mac OS X, Linux, and Windows operating systems.

The AVR Dude loader can be obtained free of charge from its authors. ATmel's AVR Studio IDE includes AVR Prog, a loader similar to AVR Dude. You may use AVR Prog instead of AVR Dude. However, AVR Prog is not the subject of these instructions.

After you install the toolchain and AVR Dude on your PC, download and unzip the Blink Arduino Diecimila source code. Inside the BlinkArduinoDiecimila folder you will discover two files: Makefile and blink.c.

The blink.c file contains C language instructions commanding the Arduino Diecimila to blink its on-board indicator LED (light emitting diode).

The Makefile contains GNU Make language instructions commanding the toolchain. The makefile commands the toolchain on your PC to translate C language instructions into ATmel ATmega168 microcontroller instructions. The makefile also commands AVR Dude to copy the microcontroller instructions to the Arduino Diecimila's microcontroller.

Plug an Arduino Diecimila into one of your PC's powered USB ports. Edit Makefile to reflect the configuration of your PC and Arduino, per the comments at the top of the makefile.

You command the makefile. Use the command-line of your PC. The command-line is sometimes called the terminal. Invoke make clean; make; make arduino from the command-line.

If the toolchain and AVR Dude are properly installed on your PC, and the Makefile is properly configured, and the Arduino is connected to a powered USB port of your PC, the systems respond affirmatively:

The toolchain compiles and links the C-language instructions into microcontroller instructions. AVR Dude loads the Arduino Diecimila with the microcontroller instructions. The indicator LED on the Arduino Diecimila blinks rhythmically when AVR Dude is finished. The rhythmic blinking is governed by the C-language program (blink.c).

$ make clean
    o Cleaning

$ make
    o Compiling blink.c
    o Linking blink.elf
    o Creating blink.hex

$ make arduino
    o Programming Arduino

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x123456
avrdude: erasing chip
avrdude: reading input file "blink.hex"
avrdude: input file blink.hex auto detected as Intel Hex
avrdude: writing flash (3160 bytes):

Writing | ################################################## | 100% 2.59s

avrdude: 3160 bytes of flash written
avrdude: verifying flash memory against blink.hex:
avrdude: load data flash data from input file blink.hex:
avrdude: input file blink.hex auto detected as Intel Hex
avrdude: input file blink.hex contains 3160 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 2.37s

avrdude: verifying ...
avrdude: 3160 bytes of flash verified

avrdude: safemode: Fuses OK

avrdude done.  Thank you.
//
// Blink twice, pause. Repeat.
//

#include <avr/io.h>
#include <util/delay.h>

void stall(double milliseconds)
    {
    _delay_ms(milliseconds);
    } 
 
int main(void)
    {
    const int blinksPerBurst = 2;
    const double blinkDurationMilliseconds = 150;
    const double pauseDurationMilliseconds = 1000;

    // The Arduino Diecimila LED is connected to Port B of the ATmega168.
    // Configure Port B for output.

    DDRB = 0xFF;

    // Emit a burst of blinks, then pause.
    // Continue indefinitely.

    while (1)
        {
        for (int i=0; i<blinksPerBurst; ++i)
            {
            PORTB = 0xFF; stall(blinkDurationMilliseconds); // On
            PORTB = 0x00; stall(blinkDurationMilliseconds); // Off
            }

        stall(pauseDurationMilliseconds);
        }

    return 0;
    }
#
# This is a GNU-style Makefile.
#
# Set your shell's PATH environment variable to include AVR Dude and AVR-GCC binaries.
#
# Set the F_CPU variable to the frequency of the Arduino's microcontroller clock.
# Specify your device's name on the AVR Dude command-line (e.g. /dev/whichever).
# The command-line options for AVR Dude differ from one version to the next. Adjust
# them accordingly.
# 
# usage: make clean; make; make arduino
# 

CC      =       avr-gcc
CCFLAGS =       -g -Os -std=c99 -Wall -mmcu=atmega168 -mcall-prologues -D F_CPU=16000000L
ECHO    =       echo
LD      =       avr-gcc
LDFLAGS =       -g -Os -Wall -mmcu=atmega168 -mcall-prologues -D F_CPU=16000000L -Wl,--gc-sections -Wl,--relax
OBJCP   =       avr-objcopy
ATPRG   =       avrdude
TARGET  =       blink.hex

.PRECIOUS:      %.hex %.elf %.o
.SUFFIXES:      .hex .elf

%.hex:  %.elf
        @$(ECHO) "    o Creating $@"
        @$(OBJCP) -R .eeprom -O ihex $< $@

%.elf:  %.o
        @$(ECHO) "    o Linking $@"
        @$(LD) $(LDFLAGS) -o $@ $<

%.o:    %.c
        @$(ECHO) "    o Compiling $<"
        @$(CC) $(CCFLAGS) -c -o $@ $<

all:    $(TARGET)

arduino:        $(TARGET)
        @$(ECHO) "    o Programming Arduino"
        @$(ATPRG) -P /dev/tty.usbserial-XXXXXXXX -b 19200 -p atmega168 -c arduino -e -U flash:w:$(TARGET)


tidy:
        @$(ECHO) "    o Tidying"
        @$(RM) *.elf *.o

clean:
        @$(ECHO) "    o Cleaning"
        @$(RM) *.hex *.elf *.o

SOFTWARE OVERVIEW · 2011-12-23 05:53 · Brian Cálves