ELECTRONS.PSYCHOGENIC.COM NEWS   RAVES & RANTS   GAMES   CONTACT US    
HOME ACCOUNT PRIVATE MESSAGE  
Main Menu

Login

Electrons :: Articles :: AVR :: Intro To AVR Microcontrollers
Intro To AVR Microcontrollers
Description: This portion of the site provides some detailed information on the Atmel AVR series of microcontrollers, beginning with this introduction.

  • 1. AVR Microcontrollers
AVR Microcontrollers

Welcome to the cAVeRn: AVR Microcontroller Section

This portion of the site provides some detailed information on the Atmel AVR series of microcontrollers.

An entire section is devoted to information on creating, building and managing AVR uC projects using our favorite development platform: Linux. Get started on the AVR Projects with Linux page. Most of the information therein also applies to other Unix platforms and lots of it, such as the AVR Programmer HOWTO, has a great deal of (development) platform-independent information.

TOC

AVR Advantages

Microcontrollers will often allow an optimal solution, combining complex functionality with reduced part count. A whole section of this site has been devoted specifically to the AVR microcontroller. Its advantages include:

  • Lots of bang for the buck. These chips pack lots of power (1 MIPS/MHz, clocks up to 16MHz) and space (up to 128K of flash program memory and 4K of EEPROM and SRAM) at low prices.
  • HLL Support. Programming in high level languages, like C, helps increase reuse and reduce turn-around/debug time/headaches.
  • In-System Programmable flash--can easily program chips, even while in-circuit.
  • Many peripherals. A whole bunch of internal and external interrupt sources and peripherals are available on a wide range of devices (timers, UARTs, ADC, watchdog, etc.).
  • 32 registers. The 32 working registers (all directly usable by the ALU) help keep performance snappy, reducing the use of time-consuming RAM access.
  • Internal RC oscillators can be used on many chips to reduce part count further.
  • Flexible interrupt module with multiple internal/external interrupt sources.
  • Multiple power saving modes.

AVR Microcontroller Primer

This mini tutorial focuses on the AVR microcontroller architecture. It is meant as an introduction to the AVR platform in particular but will also (very briefly) introduce a few microcontroller-related topics--be warned that it may be a little abrupt for the absolute microcontroller newbie, but it should be a good intro for everyone nonetheless.

8535 AVR Architecture block diagram This represents the architecture for the AT90S8535 chip but many of the elements are common to the entire AVR family.

The AVRs are 8 bit RISC platforms with a Harvard architecture (this means that program and data memory are separate). They are microcontrollers: basically, complete computers on one integrated circuit. This means that they combine, on a single chip, a number of the elements you might find separately within a microcomputer. These include:

  • a microprocessor (CPU) core
  • memory
    • program memory
    • long-term memory (EEPROM)
    • volatile memory (SRAM)
  • I/O interface for input and output
  • numerous peripherals
CPU

The processor core, depicted here towards the center of the image on the left side of the data bus, includes elements to read the program memory, decoding and executing the instructions within. The CPU can also fetch and store data to and from the EEPROM, SRAM and the 32 registers. The registers act as extremely efficient storage for 8 bit values (1 byte) and the ALU (arithmetic/logic unit) can operate on each of the 32 registers directly.

The AVR processor features a real life stack and its instruction set was designed and optimized for use with high level languages--it is easy to program these chips using C. Note that certain implementations don't provide any SRAM, so the stack is actually hardware based (limiting the stack depth to three).

It is interesting to note that most instructions only take a single clock cycle to execute and there is no internal clock division. Also, the CPU will fetch and decode the next instruction as it is executing the current instruction (i.e. in parallel). All this means that AVRs will reach performances of nearly 1 MIPS (Million Instructions Per Second) per MHz--they are fast and efficient. Another advantage of reaching comparable performance at reduced clock rates (e.g. an AVR at 4 MHz will execute about as many instructions as a PIC microcontroller running at 16MHz) is reduced power consumption and lower electro-magnetic noise from high frequency signals.

Memory

The program memory is a contiguous block of flash memory. It may be programmed and reprogrammed numerous times easily. In fact, using the In-System Programming, you can design your system to allow chip firmware upgrades in-place. Program memory is 16 bits wide and can usually be erased/re-written at least 1000 times.

All AVRs have some EEPROM and most have SRAM available. Both are 8 bits wide. The EEPROM is programmable during execution (to retain data even without power) or directly during programming (which is very useful for things like production line calibration), see our AVR Programmer HOWTO for details. Using EEPROM is slower than storing data in SRAM but, unlike the volatile RAM, once written the data will remain available indefinitely (even when the power source has been removed). The EEPROM is said to withstand 100,000 erase/write cycles.

I/O and Peripherals

Peripherals are where the members of the AVR family distinguish themselves. They all have the same core and support a basically uniform instruction set, which means you can easily develop for all of them. However each has its own particular blend of peripherals and extras.

All AVRs, from the tiny 8 pin DIPs to the 44 pin Megas, have at least one data port. Data ports allow for input or output of logic level (binary, HIGH or LOW) data. The AVR ports are bi-directional, allowing you to set them for input or output on a pin-by-pin basis. The AT90S8535 depicted here has four 8-bit ports available. Often, the external pins which make up a port will serve dual (or triple!) purposes, and how the pins are used will depend on how you've configured the controller. For instance, you can see from the diagram that all pins for port A (upper left) are also used as separate channels to the ADC (analog to digital converter) while certain pins of port D (lower right) are used for communication through the UART.

Other peripherals which communicate with the outside world are, as discussed above, the ADC and UART. The Analog to Digital Converter included with some AVRs is normally multiplexed; this means that you can monitor multiple analog values, through different pins, one at a time. The UARTs allow asynchronous serial communication, directly with other chips or with computers systems using an RS-232 interface. Analog comparators and other peripherals are available on certain chips, check the relevant datasheets for details.

Internal peripherals include timers and counters (8 and 16 bit, with optional overflow interrupts), a watchdog timer (to automatically reset the chip after a preset delay of 16ms to 2s), RC oscillators and more.

Programming

AVR microcontrollers may be programmed using assembly or a higher level language, such as C.

Learning to code assembly is a good idea, as it gives you in depth understanding of the process under the hood. With this type of "bare metal" programming, you manipulate the CPU registers directly and can optimize their use according to your specific needs. The process can, however, become tiresome--at least for programmers accustomed to letting a compiler handle all the dirty work.

Using a high level language can allow you to code much more efficiently. Here is a very simplistic program in C:

#include 

void main (void)
{
        uint8_t i;
        uint16_t j=0;
        for (i=0; i<100; i++)
        {
                j += i;
        }
}

All it does is add all the integers between 0 and 99 and store the result in the variable j. Here is part of the same program, as transformed into AVR assembly by the AVR-GCC compiler (corresponding C code has been inserted):

#include 

void main (void)
{
  52:	cc e5       	ldi	r28, 0x5C	; 92
  54:	d2 e0       	ldi	r29, 0x02	; 2
  56:	de bf       	out	0x3e, r29	; 62
  58:	cd bf       	out	0x3d, r28	; 61
	uint8_t i;
	uint16_t j=0;
  5a:	1a 82       	std	Y+2, r1	; 0x02
  5c:	1b 82       	std	Y+3, r1	; 0x03
	for (i=0; i<100; i++)
  5e:	19 82       	std	Y+1, r1	; 0x01
  60:	89 81       	ldd	r24, Y+1	; 0x01
  62:	84 36       	cpi	r24, 0x64	; 100
  64:	08 f0       	brcs	.+2      	; 0x68
  66:	0d c0       	rjmp	.+26     	; 0x82
	{
		j += i;
  68:	89 81       	ldd	r24, Y+1	; 0x01
  6a:	28 2f       	mov	r18, r24
  6c:	33 27       	eor	r19, r19
  6e:	8a 81       	ldd	r24, Y+2	; 0x02
  70:	9b 81       	ldd	r25, Y+3	; 0x03
  72:	82 0f       	add	r24, r18
  74:	93 1f       	adc	r25, r19
  76:	8a 83       	std	Y+2, r24	; 0x02
  78:	9b 83       	std	Y+3, r25	; 0x03
  7a:	89 81       	ldd	r24, Y+1	; 0x01
  7c:	8f 5f       	subi	r24, 0xFF	; 255
  7e:	89 83       	std	Y+1, r24	; 0x01
  80:	ef cf       	rjmp	.-34     	; 0x60
	}
}

Each line here is composed of, in order:

  • line number in hex, e.g. 80:
  • machine code instruction, e.g. ef cf
  • assembly language version, e.g. rjmp .-34
  • generated comment (line number or hex value translation)

Whichever language you choose to use, the next step is to write and build your program (compiling, linking and translating it to a suitable format) and finally transfer it to the microcontroller using a suitable programmer.

Level: Article
Additional Article Data
Level: Article

Comments
The comments are owned by the poster. We aren't responsible for their content.

Jump to section
News

All contents are Copyright (C) 2004-2005 Psychogenic Inc -- All rights reserved