ATMEL HOWTO
Note: these notes are tailored to the Arduino Mega board with an atmega2560 processor. There are complementary
notes for the atmega8.
Step 1: Installation
See Installing the AVR Tools
Step 2 (AVR Studio): Creating and compiling your program
For class, the following files can be found in your svn tree. If you are not using this tree, then grab a current version of OUlib: libou_atmega2560.a and
oulib.h and place it in your project directory (or place a shortcut to these files in your project directory). Also download oulib_serial_buffered.h and buffer.h.
See the AVRstudio discussion (NOTE: AVAILABLE SOON).
In creating your C program you may start with this sample program:
led_flash.c
Common Problems
With AVRstudio (version 6), it is not uncommon for things to be working, and then
suddenly stop working (with respect to compiling and downloading)
- text region full: Make sure the "optimization" flag is
set (see above). You will probably also need to re-add "F_CPU"
- Oulib error: F_CPU not defined. Re-add "F_CPU" as
above. After this, perform a "build clean" and a "rebuild all"
- Many things undefined, such as DDRB and PORTB. Re-set your
device to "atmega2560"
Step 2 (non-AVR Studio): Creating and compiling your program
- Create your C program (you can start with this sample program:
led_flash.c). You will need a text editor of some form (e.g., xemacs)
- Create a makefile (start with this sample
makefile)
- For class, the following files can be found in your svn tree. If you are not using this tree, then grab a current version of OUlib: libou_atmega2560.a and
oulib.h and place it in your project directory (or place a shortcut to these files in your project directory). Also download oulib_serial_buffered.h and buffer.h.
- Modify the makefile so that it points to your source file:
Change the TARGET = led_flash.c line so that it
contains the name of your source file.
- Compile the your program:
- Use your shell
- Use the "cd" command to change directory to the location
of your makefile and source file.
- Type make. If there are no errors in your
program, you will have a variety of files, including:
- led_flash.lss: a file
that shows both your program and the
corresponding assembly code
- led_flash.hex: a file that contains the program
information that will be downloaded to your
processor
- From your program directory, type: make program
- If you run into device permission problems, you can do this as root as a temporary measure (to configure for standard user use, you must configure udev properly). To fix this permanently, see the installation page.
- Setting the programmer frequency: (if you run into trouble)
- Enter interactive mode:
avrdude -c avrispmkII -P usb -p m8 -F -u -t
- parms will give you the current clock period
- sck [period] will set the clock period
Use 1 for high speed programming (not possible unless you are using the external 16 MHz crystal)
Use 10 for low speed programming (compatible with the internal clock)
- quit
- Configuring for use with the external crystal: (again, use if you run into trouble)
- Set programmer frequency for low speed programming (above)
- Use make setfuse (make sure you have the latest copy of the makefile)
- Set programmer frequency back to high speed programming
Step 3: A Basic Circuit
You may use the original mega2560/first_circuit.sch
file if you wish (this is an Eaglecad file).
Notes:
- L1 and L2: LEDs used in the sample program. Note that the
polarity of the LEDs is critical.
- You need a resistor in series with each of the LEDs (1K ohms will do).
Hints
- It is always a good idea to flash a LED in some unique way
at the beginning of your program. This way, you can be sure
that your processor is receiving power and that the power
supply is stable
- You can find other mega 2560 examples in the examples_2560 directory
- You can find other examples in the examples directory (note that some of these are specific to the atmega8 processor, though they will often be compatible with the 2560)
- You can also download the oulib source and examples: oulib_2012_02_14.tar.
- AVR Freaks has a tutorial
on bit manipulation in C
- Suha has a tutorial on manipulating ports on the Atmel (note that this is a slightly different processor than the mega8, but the principles are the same).
Atmel Mega2560 Details
All:
Unix:
andrewhfagg [[at]] gmail.com
Last modified: Thu Jan 22 11:37:33 2015