Documentation:Getting Started with AVRs

From AVRFreaks Wiki

Jump to: navigation, search

This article is a stub. You can help the AVR Wiki by expanding it.

Contents

[edit] The Beginning

Getting started with the AVR doesn't have to be hard! To get started with a microcontroller, there are two main areas you have to know about: hardware and software. If you have experience building circuits with digital logic chips and soldering, you'll understand the hardware part easily. If you have experience programming computers, you'll understand the software part easily. If you have a great chilli recipe, that won't help you alot here.

There are five things you will need to think about when getting started:

[edit] "Target" Hardware

The microcontroller, its support circuitry and other electronics connected to it is called the "target". This is because it's where your software goes to execute or because making this gizmo work is your true objective. Although you can use software simulators to run your code, it's a lot more fun if you have a real physical target hardware system. You can buy development kits and prototype boards from Atmel or various other sources (called "third-party" suppliers, because they're not Atmel, the maker of the microcontroller, and they're not you, the user). Or, if you're up for it, you can build your own target hardware, either from your own design or someone else's.

[edit] A "Host" Computer

When you program a computer, you generally edit the text of the program and prepare it for execution on the same computer on which it will run. This is not how it is done when you program a microcontroller. You edit the program and compile it (if necessary) on a "host" computer and then transfer the result to the microcontroller to run it. This is called "cross" development (cross-assembly, cross-compiling, etc.). It may at first seem awkward as compared to "native" development, but it's necessary because you need that keyboard, big screen and disk storage to work with your programs and your microcontroller usually won't have anything suitable.

AVR cross development is possible on a PC running most versions of Windows or Linux and also on a recent model Mac. There are no special requirements, but serial ports can be very useful when working with microcontrollers, so choose a host computer that has them if at all possible, or find a good USB-to-serial adapter.

[edit] Software Development Tools

On your host computer, you will at least need a programmer's editor. If you have a target with a BASIC interpreter already in the microcontroller, this is all you will need. To program in assembly language, you will also need an assembler and a linker. To program in a high-level language, such as C, you will need a compiler as well. If you want to debug your program, you will also need a debugger. These tools can be obtained in integrated packages or you can mix and match.

Atmel provide a free integrated package consisting of an assembler, linker, programming and debugging software, project manager and various other tools. This is called AVR Studio and can be downloaded from the Atmel Web site. It does not include a compiler, but it does have a "plug in" for AVR-GCC and hides a lot of the complexity of this powerful tool from users not comfortable with Unix-style command-line tools. AVR Studio only runs on Windows. WinAVR is another package containing AVR-GCC that runs on Windows. Its component parts are all open-source (unlike AVR Studio) and although the package is not as seamlessly integrated as AVR Studio, it provides more flexibility and power for advanced users. These open-source tools can also be obtained to run under Linux.

[edit] Programming / Debugging Interfaces

When you have written and prepared your program on the host computer, you will need to get it into the microcontroller to run it. You can do this with a "bootloader" or a hardware programming interface. If you want to debug your program, that is control its execution so you can see step-by-step what is happening inside the microcontroller as it runs, then you will need a debugging interface.

There can be quite a difference in cost between these three solutions. With a bootloader, the only extra hardware you need is a serial cable, but you have to perform some action to start the bootloader each time and transfer of larger programs can take some time. Atmel microcontrollers support "In-System Programming", or ISP, which is faster and generally more convenient. You need a special connector on your target and an ISP programming interface, which you can buy or build for less than USD 20. With both the bootloader and ISP, if your program doesn't work you have to figure out how to tell what is going wrong and try again until you find and fix the problem. With a debugger, you can effectively see inside the microntroller and catch your program at the point at which it misbehaves. This makes fixing the problem faster, but a debugging interface will cost more than a simple programming one. Atmel's JTAG ICE Mk11 supports all the AVR devices, but costs USD 300. The AVR Dragon is only USD 50, but debugging is only possible with the smaller devices that have 32 KiB or less of program memory. Available at about USD 40 from third parties are "clones" of the older JTAG ICE are that have unrestricted debugging of a limited selection of AVR devices. Debugger interfaces function as programmers also.

[edit] Project Ideas

It helps when you are getting started to have some idea of where you want to go, at least in the near term. Having some goals in mind helps you make the right choices of target hardware and tools. Even if there isn't any particular "project" you want to accomplish and instead you're just getting your feet wet, you should try to think out what that means. Do you just want to see if you can blink an LED or make it turn on and off at the press of a switch? Do you want to play a tune on a speaker? Do you want to control the speed of a motor? Record light intensity during the day?

[edit] Hardware

If you have no experience with building electronics, you'll need to learn some basic electronics first. As an example if you are comfortable reading a schematic of a circuit with a few NAND gate chips, some buttons, and some LEDs you'll have enough experience to build your first microcontroller circuit.

Picking up a book on getting started in electronics will be the best way to get started. Forrest Mims' Getting Started in Electronics book is a great hands-on introductory text.

[edit] Software

An AVR needs to be told what to do, or programmed. There are a number of possible languages you can use to program the AVR in. The AVR itself understands only one language - machine language. This is a very low level language that tells the AVR to move pieces of data between registers, or add two registers. When you use assembly language, you are programming in a human-readable form of machine language. Machine language itself is just binary "opcodes", whereas assembly language has short forms for these opcodes. For example:


Opcode       Assembly Mnemonic         Description
0x9403       INC R0                    Increment Register 0 once

This means that assembly language is different for each processor. Other compiled languages (BASIC, C, Forth, Pascal, etc) take the code you write and run it through a program called a compiler. This compiler takes your program, and uses what it knows about the target proessor to make a binary using the machine opcodes that will run on the target.

[edit] Assembly

Assembly deals with the lowest level of hardware with the AVR. Using assembly will mean you understand *exactly* what the AVR is doing with each register or instruction. It is a popular starting language as understanding how exactly the AVR works is very useful.

[edit] BASIC

Basic is easy to learn and understand, having a syntax very similar to English. Note that many people associate BASIC with a slow language, and this connection is unfounded. Many systems (such as the BASIC Stamp) use interpreted BASIC. In this system the target processor directly reads the BASIC language, and then decides how it has to execute that instruction in it's native language. Compiled BASIC however does this ahead of time, which means compiled BASIC (as you will use on an AVR) can be as fast as any other language.

[edit] C

C language is one of the most popular languages. Almost every microprocessor has a C compiler available for it. Learning the C language is not something that will be regretted.

Personal tools