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
- 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
- Branch Instructions
- Data Transfer Instructions
- Bit and Bit-test Instructions
- 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 Register
- Bit 7 I: Global Interrupt Enable
- Bit 6 T: Bit Copy Storage
- Bit 5 H: Half Carry Flag
- Bit 4 S: Sign Bit
- Bit 3 V: Two's Complement Overflow Flag
- Bit 2 N: Negative Flag
- Bit 1 Z: Zero Flag
- Bit 0 C: Carry Flag
- 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 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>
in R19,0x00 <--> lds R19, 0x0020</b>
- 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)
DDRB |= (1<<6);
__asm__ __volatile__("sbi 0x17,6 \r\n"::);
- Data space (DDRF register at address 0x61)
DDRF |= (1<<5);
__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.
- 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 screen
- 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 screen
- 4. Select "Next>>" and choose "AVR Simulator" and "ATmega128" as the device.
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
- 7. The source code can be compiled by selecting "Build > Build (F7)"
- 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
- 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.
MFile