Atmel AVR Microcontrollers

Summary

This article contains useful information and resources for the development of embedded applications using the ATMEL AVR 8-bit RISC microcontrollers. If you are completely new to AVR Development, you should start by reading the Newbie’s Guide to AVR Development from AvrFreaks (you will have to register in order to access the guide).

1.  Tools

Note: You will need a 12VDC, 500mA power supply for the STK500 AVR Starter Kit. You can get a wall transformer with the proper specifications from DigiKey (part# MT7165-ND).

1.1  Hardware

Note: Starting in version 4, AVR Studio provides direct access to WinAVR’s C libraries. Thus, installing both packages is highly recommended.

1.2  Software

  • AVR Studio (Windows Only). The AVR Studio includes an assembly code compiler, an emulator, and tools to program your AVR devices.
  • WinAVR (Windows Only). WinAVR provides C libraries and compiler for AVR devices. It also includes additional programming tools.

2.  Tools, the “Free” alternative

  • This alternative should not cost more than 10 dollars, including the programmer hardware, a C code compiler, a debugger, a software programer and even the microcontroller!. Is based in open source tools and the In-System Programing capabilities of some AVR microcontrollers. I used an ATMEGA48, but others should work as well.

2.1  Hardware

  • A parallel programming interface that you can make yourself with:
    • 6 - wires
    • 1 - male parallel port connector
    • 7 - 220 ohm resistors

In-System AVR programmer

2.2  Software

  • An open source compiler, like avr-gcc (or the windows version WinAVR), a good tutorial on how to install it and use them is found here
  • An open source software programmer, that will allow you to upload your compiled hex code to the flash memory of your microcontroller, alternatives are either AVRDude or sp12. Any of them will provide you with a makefile template, so you can just call a command that will upload the program to your microcontroller (after modifying the template indicating what type of processor you are working with).

2.3  Patience

  • Is important. As with any open alternative, you are not getting technical support, but there are several forums and documentation maintained by the community that will help. I had my hardware, compiler and programmer running an a linux computer and even my first program running on the ATMEGA48 in less that two days. Good luck! :)

3.  Assembly Code

Note: You will need to change the .INCLUDE and .DEVICE directives in the code below according to the particular microcontroler you are using.

3.1  Basic Port Control. Target: ATtiny13.

The following code should drive pins 0, 2, 4 and 6 in PORTB to HIGH.

.INCLUDE "tn13def.inc"
.DEVICE ATtiny13

	rjmp	RESET

CONFIG:	ldi	R16, $FF
	out	DDRB, R16	;Configure PORTB as output
	ldi	R16, $55
	out	PORTB, R16	;Set pin values in PORTB

MAIN:	rjmp	MAIN		;Loop infinitely

RESET:	rjmp	CONFIG

3.2  Single ADC Conversion. Target: ATtiny13.

This code should perform a single analog to digital conversion of any signal fed to PIN[3] in PORTB.

4.  C Code

4.1  Basic Port Control. Target: ATtiny2313.

The code below should make all pins in PORTB toggle between HIGH and LOW. The embedded delay helps to make the changes in the port easily noticeable.

#include <avr/io.h>

typedef unsigned char byte;
typedef unsigned int word;

void delay(byte i) {		//Delay function
	word j;
	while(i--) {
		j=50000;
		while(j--);
	}
}

int main(void) {

	DDRB = 0xFF;		//Configure PORTB as output

	while(1) {
		PORTB = 0xFF;	//Set all pins in PORTB
		delay(10);
		PORTB = 0x00;	//Clear all pins in PORTB
		delay(10); 
	}
	return 0;
}

4.2  Servo Motor Control. Target: ATtiny2313.

This code will configure the 16-bit timer to allow control of two servomotors. When connected to PINB[3] and/or PINB[4], the servomotors scan through their usable range.

4.3  Serial Data Logging. Target: ATmega168.

This code will configure the analog-to-digital converter (ADC) in free-running mode to perform continuous 8-bit conversion of an analog signal fed to PINC[0]. It will also configure the microcontroller’s serial port for RS232 (asynchronous, 38400bps, no parity, 1 stop bit) transmission of converted data through PIND[1].

5.  Links

6.  Glossary

  • HIGH: The voltage level representing a binary “1″. Usually Vcc = 5 Volts.
  • LOW: The voltage level representing a binary “0″. Usually GND = 0 Volts.