|
|
|||||||
| Register | Links | Tutorials | Data Center | Blogs | Gallery | FAQ | Members List | Social Groups | Calendar | Search | Today's Posts | Mark Forums Read |
| Home | Submit Tutorial | What's New | What's Popular | Search |
| AVR Basics |
|
|
|||||||
|
|||||||
| Difficulty: | Beginner | ||||||
|
AVR Basics
The AVR microcontrollers from Atmel are extremely popular among hobbyists. They are a low cost general purpose micro-controller, with an excellent open-source, fully-featured C compiler. There are several dozen available AVR chips, all with a common core, but some small differences. For this tutorial, we will focus on the ATMEGA168, which is often found in the Arduino project, and is one of the best supported chips in the hobbyist arena. Microcontrollers in General A microcontroller is sort of like a small computer, but also sort of isn't just a small computer. So what sets a microcontroller apart from a regular desktop computer?
First off, we need some way to compile the code we write into a form that will run on the AVR microcontroller, and a way to install the code onto the AVR, since it lacks things like a keyboard, monitor, or floppy drive. The AVR-GCC compiler is a special version of GCC, the GNU C Compiler, targeted at the AVR architecture. It includes an assembler specifically for the AVR, so that we can compile C and assembly code into the instructions the AVR undertands. A second piece of software, called AVRDUDE is used to actually download the code into the chip. Installation is quite simple:
An alternative to using an ISP is to have a bootloader on your AVR. A bootloader is simply a piece of code that runs when the AVR is reset. It takes data from a serial connection and writes the program into it's own FLASH memory. A bootloader has several downsides:
Our basic connections for the AVR are quite simple: we need a power source, a system clock, and an ISP connection. There are a number of good AVR boards out there, which are designed to house all the necessary equipment while making the AVR breadboard compatible. The power source is quite simple: we need a 5V regulator. If you have a battery or wall-wart with voltage > 7V, you can just use a 7805 (with the appropriate capacitors and stuff). The AVR chip has multiple voltage and ground pins we need to connect. You should also try to place an 0.1uF decoupling capacitor as close as possible to each VCC pin. You'll notice we also connected the AREF pin to 5V, which is quite typical, this sets the reference voltage for the Analog-to-Digital Converter. While the AVR chips have an internal 8MHz, we often want to use an external clock source so we can have a higher clock speed. When you buy a new AVR from the factory, it will have it's fuse bytes programmed such that it is using the internal 8MHz, divided by 8. <discuss fuse bytes?>. In the schematic below, we have labeled the "optional external XTAL", which is simply a full-swing crystal oscillator, the crystal requires two capacitors to connect it to ground. The 6-pin ISP header is standard equipment on all AVR boards these days. You may also see a 10-pin version which is the older, and now less popular version. A reset switch is quite useful. The reset pin is internally pulled high, the switch should be between reset and ground. The AVR Architecture There are two main branches of AVR chips: the ATTINY and ATMEGA branches. Chips are named such that the name conveys the family and how much FLASH the chip has. For this tutorial we will focus on the ATMEGA168 (you'll see why shortly), this chip has 16k of FLASH ROM, denoted by the 16 in the part number, and is code-compatible with the ATMEGA88 and ATMEGA328. The compatibility is denoted by the last number, the ATMEGA88 is an 8k version, and the ATMEGA328 is a 32k version. RAM and EEPROM size typically scale with the FLASH size. There are a couple very commonly used families out there:
With most of our hardware, we interact in a fairly consistent way. There are typically a few configuration registers to set up the hardware the way we want it to work, a register to put data into the device, and a register to get data back from the device. We'll look at the implementation of the digital I/O ports to start our study of this. External pins on the AVR are group together into ports of 8 pins. Our device has a port called port B, composed of the 8 digital I/O pins, PB0 through PB7. We have 3 registers related to the port, in which pin PB0 would correspond to bit 0, or the least-signifigant bit each register:
The analog to digital converter is similar to the digital pins in setup: there is a register for setting the clock speed of the converter, a register to set which pin to read and do a conversion from, and a register for reading back the result. Setting up a timer/counter is also very similar. Each of the AVR's timers have several related registers: registers for setting up the clock speed and interrupt style of the timer, registers for setting the output width of the comparision channels, and a register where the actual count is stored. So where do you find information about all these registers? Atmel publishes massive datasheets for each AVR it produces. These datasheets list every register in the AVR, and what the individual bits mean. They discuss, in depth, the setup of the chip. There are also a number of application notes that discuss in depth a particular use of the processor. Unfortunately, the language is often designed for embedded engineers, however google + forums can help with that. Your First Program Using the AVR is quite simple. We'll make a program that will blink an LED on the ATMEGA168, when connected between PB0 and ground. For now, we'll assume we are using the internal 1MHz oscillator, which is the default. First, we need to create a source file in C, we'll save the following as example.c: Code:
// example.c -- blinks light on PB0 of an AVR.
#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
int main(){
// Make PB0 an output
DDRB = 0x01;
// loop forever
while(1){
// led on, wait 1/2 sec
PORTB = 0x01;
_delay_ms(500);
// led off, wait 1/2 sec
PORTB = 0x00;
_delay_ms(500);
}
}
make all make program Your LED should start blinking after the download is complete. If you know make, a quick look through the makefile, and the AVRDUDE manual, will get you going in no time. The Arduino Approach Many people out there use the Arduino. An Arduino is just a slightly special AVR, in fact it is just an ATMEGA168 programmed with a bootloader. The Arduino IDE makes developing with the AVR slightly simpler as you won't have to worry about makefiles and such, or ISP devices. The Arduino software installs very easily and you won't have to learn much about the underlying AVR architecture to do lots of simpler things. That said, understanding the underlying architecture, and being able to use it effectively, allow you to do a lot more than you can with just the Arduino libraries. For instance, the Arduino libraries include an interrupt library, but it only extends to the 2-3 dedicated external interrupts. There is no library for using the newer pin interrupts -- you have to understand the AVR architecture, and how to manipulate registers, to be able to use this powerful feature. The Arduino libraries can do a lot, but real Arduino power-users will understand the other features of the AVR as well. Note also, that you can use an ISP with the Arduino environment. In fact, I use the Arduino environment on most projects simply because it is easier to share them with others, but I don't use bootloaders on hardly any of my chips. Resources AVRFreaks The Seattle Robotics group has some excellent example code for a workshop they run using the ATMEGA16 Slides and code from a series of workshops I ran during summer 2009 are available on my site. Although our focus was on the Arduino, we typically went deeper into the AVR architecture while discussing the Arduino. |
|||||||
| Tags: | None | ||||||