Piconomic Logo www.piconomic.co.za

Atmel AVR quick start guide

This is a quick start guide for the person who wants to switch to the brilliant 8-bit Atmel AVR microcontroller and learns by example.

The following free software tools are needed:
  • WinAVR 20100110 : a suite of executable, open source software development tools for Atmel AVR microcontrollers hosted on the Windows platform. It includes the GNU GCC compiler for C and C++.
  • Atmel AVR Studio 4.18 SP2 (build 700) : the Integrated Development Environment (IDE) for writing and debugging AVR applications on Windows.
  • Tera Term 4.66 : Free terminal emulation software to upload new firmware to bootloader using serial port and XMODEM protocol.
Reference documentation:
Sections:
Tutorials:
/tutorials : Atmel AVR tutorials
Examples:
/examples : Atmel AVR examples

Short introduction to AVR instruction set

The AVR core has an advanced RISC architecture with most of the instructions being executed in a single clock cycle. The AVR uses a Harvard architecture with separated access to program and data. A load/store assembler instruction set is implemented with 32 general purpose registers (R0 to R31). The instructions are divided into the following categories:
Arithmetic and Logic Instructions
   add Rd,Rr // Add without Carry : Rd = Rd + Rr
Branch Instructions
   rjmp k // Relative Jump : PC = PC + k + 1
Data Transfer Instructions
   mov Rd,Rr // Copy register : Rd = Rr
Bit and Bit-test Instructions
   sbi P,b // Set bit in I/O register : I/O(P,b) = 1
Tip: the quickest way to learn the assembler instruction set is to refer to the Compiled Help file of Atmel AVR Studio:
Help > AVR Tools User Guide > AVR Assembler > Parts > ATmega128/1280/1281 and AT90CAN128 Instruction Set
The Status Register(SREG) contains flags that convey information about the most recently executed arithmetic instruction. Bit 7 (I) is different, as it is the flag that enables/disables interrupts globally:
avr_sreg.png

AVR SREG Register


Introduction to AVR peripheral access

Tip: this section appears daunting, but will become essential knowledge on your way to master the AVR. Skim through it now, and return to read it in depth after working through /01_Port_IO.
All of the AVR peripherals are manipulated by writing to and reading from the Peripheral Control Registers. Refer to "Register Summary" of the ATmega128 datasheet (p.365).
Here is a condensed visual representation of the ATmega128 memory map to highlight the Harvard architecture and access to the Peripheral Control Registers:
avr_memory.png

AVR Memory Map

The memory map will make more sense after working through the tutorials, but it is displayed here to point out a specific mental stumbling block on the GCC / AVR Libc learning curve ("SFRs - Special Function Registers").
The data memory load/store instructions provide a different method to access the the general purpose registers (R0 to R31) and the I/O memory (0x00 to 0x3F). Thus the following assembler instructions are equivalent (but not optimal!):
   mov R16,R17 <--> lds R16,0x0017</b> // R17 can also be accessed at address 0x0017 in data space
   
   in R19,0x00 <--> lds R19, 0x0020</b> // "PINF" is mapped to 0x00 in I/O space and address 0x0020 in data space
To access the other Peripheral Control Registers that do not fit into I/O space (which have optimal bit manipulation instructions), data space load/store instructions must be used.
Tip: luckily, the C compiler decides to use the optimal assembler instruction so that you don't have to.
Here is an example to demonstrate the difference between I/O space and Data space access:
I/O space (DDRB register at address 0x17)
   // C version
   DDRB |= (1<<6); // Set I/O pin PB6 to output
   
   // Equivalent assembler version
   __asm__ __volatile__("sbi 0x17,6 \r\n"::);
Data space (DDRF register at address 0x61)
   // C version
   DDRF |= (1<<5); // Set I/O pin PF5 to output
   
   // Equivalent assembler version
   __asm__ __volatile__("lds R24,0x61 \r\n"
                        "ori R24,32   \r\n"
                        "sts 0x61,R24 \r\n" ::);
From this example, you can see that access to Data memory mapped peripherals is not as efficient as I/O memory mapped peripherals. More assembler instructions are needed that also take longer to execute.

How to open an existing project in AVR Studio

Tip: AVR Studio offers a complete integrated development environment: editor, build system, simulator, debugger, programmer,...
All of the tutorials, bootloader and examples are provided with a pre-configured AVR Studio project. External Makefiles are referenced, instead of AVR Studio's build system, to support non-Windows users.
An existing project can be opened by navigating to the AVR Studio menu "Project>Open Project" and selecting the "*.aps" file, e.g. "tutorials\01_Port_IO\PortIO.aps"
avr_studio_open_project.jpg

AVR Studio Open Project screen


How to create a new project in AVR Studio using the AVR GCC plugin

Here are the steps to create a new AVR GCC project in AVR Studio:
1. Navigate to the AVR Studio menu "Project>New Project" and select "AVR GCC" as project type.
2. Type a project name, e.g. "PortIO" and create an initial C file, e.g. "PortIO.c"
3. Select a location, e.g. "C:\Tutorials" and create a folder.
avr_studio_new_project.jpg

AVR Studio New Project screen

4. Select "Next>>" and choose "AVR Simulator" and "ATmega128" as the device.
avr_studio_simulator.jpg

AVR Studio Simulator setup screen

Tip: you can change this choice at a later stage by navigating to "Debug>Select Platform and Device..."
5. Select Finish. Your new C file will now be created and open for editing.
6. The build options, which changes an AVR Studio generated Makefile, is selected by navigating to "Project > Configuration Options". Set the frequency to 7372800 (7.3728 MHz) and optimization to -Os (optimized for size) and select OK.
avr_studio_project_options.jpg

AVR Studio Project Options

7. The source code can be compiled by selecting "Build > Build (F7)"

How to simulate a project in AVR Studio

Tip: If you do not have access to a JTAG debugging tool (e.g. AVR JTAGICE mkII) , AVR Studio's simulator is an invaluable development tool that should be used vigorously to simulate the code and verify it's correctness, before downloading it to the target.
This section assumes that the code has been built successfully.
First enable cycle accurate timing information with "Debug > AVR Simulator Options (Alt+O)", set the clock frequency to "7.3728 MHz" and "OK". This setting is saved with the project and needs only to be done once.
Select "Debug > Start Debugging (Ctrl+Shift+Alt+F5)". You can now single-step, set breakpoints,...
Tip: select and expand the I/O View in the right-hand pane to view the status of the processor and the peripherals.
avr_studio.jpg

AVR Studio


How to create a new Makefile if not using AVR Studio

A Makefile is used to automate the process of compiling and linking the source code of a project.
A TCL/Tk script called "Mfile" is bundled with WinAVR that automates the process of creating a new Makefile. Alternatively, you can copy and modify an existing Makefile.
avr_mfile.jpg

MFile

Generated on Fri Aug 13 16:50:36 2010 for Piconomic Firmware Library by doxygen 1.6.3