Chapter 6. The AVR Microcontrollers

A really useful engine . . .

W. V. Awdrey

In this chapter, we’ll look at the ATMEL AVR processor. Like the PIC, this processor family is a range of completely self-contained computers on chips. They are ideally suited to any sort of small control or monitoring application. They include a range of built-in peripherals and also have the capability of being expanded off-chip for additional functionality.

Like the PIC, the AVR is a RISC processor. Of the two architectures, the AVR is the fastest in operation and arguably the easiest for which to write code, in my personal experience. The PIC and AVR both approach single-cycle instruction execution. However, I find that the AVR has a more versatile internal architecture, and therefore you actually get more throughput with it. If I were looking for a processor for a small-scale embedded application, the AVR would be my first port of call.

In this chapter, I will look at the basics of creating computer hardware by designing a small computer based on the AVR, the ATtiny15. We’ll also see how you can download code into an AVR-based computer and how it can be reprogrammed in-circuit. From there, we’ll go on to look at some larger AVR processors, with a range of capabilities.

Later in the chapter, we’re going to look at interfacing memory (and peripherals) to a processor using its address, data, and control buses. For most processors, this is the primary method of interfacing, and therefore the range of memory devices and peripherals available is enormous. You name it, it’s available with a bus interface. So, knowing how to interface bus-based devices opens up a vast range of possibilities for your embedded computer. You can add RAM, ROM (or flash), serial controllers, parallel ports, disk controllers, audio chips, network interfaces, and a host of other devices.

Most small microcontrollers are completely self-contained and do not “bring out” the buses to the external world. In this chapter, we’ll take a look at the ATMEL AT90S8515 processor. It is the only processor of the AVR family that allows you access to the CPU’s buses. But first, let’s take a look at the AVR architecture in general.

The AVR Architecture

The AVR, developed in Norway, is produced by the ATMEL Corporation. It is a Harvard-architecture RISC processor designed for fast execution and low power consumption. It has 32 general-purpose 8-bit registers (r0 to r31), six of which can also act as three 16-bit index registers (X, Y, and Z) (Figure 6-1). With 118 instructions, it has a versatile programming environment.

AVR registers

Figure 6-1. AVR registers

In most AVRs, the stack exists in the general memory space. It may therefore be manipulated by instructions and is not limited in size as is the PIC’s stack.

The AVR has separate program and data spaces and supports an address space of up to 8M. As an example, the memory map for an AT90S8515 AVR processor is shown in Figure 6-2.

ATMEL is very proud of the throughput of the AVR. The company gives the following sample C code, which it compiled and ran on several different processors:

int max(int *array)
{
  char a;
  int maximum = -32768;

  for (a = 0; a < 16; a++)
    if (array[a] > maximum)
      maximum = array[a];
  return (maximum);
}

Their results are interesting (Table 6-1).

ATMEL AT90S8515 memory map

Figure 6-2. ATMEL AT90S8515 memory map

Table 6-1. ATMEL’s comparison of processor speed and efficiency

Processor

Compiled code size

Execution time (cycles)

AVR

46

335

8051

112

9,384

PIC16C74

87

2,492

68HC11

57

5,244

This indicates that, when running at the same clock speed, an AVR is 7 times faster than a PIC16, 15 times faster than a 68HC11, and a whopping 28 times faster than an 8051. Alternatively, you’d have to have an 8051 running at 224MHz to match the speed of an 8MHz AVR. Now, ATMEL doesn’t give specifics of which compiler(s) it used for the tests, and results can certainly be tweaked one way or the other with appropriately chosen source code. However, my personal experience is that, with the AVR, you certainly do get significantly denser code and much faster execution. For most small-scale applications, the AVR is my first choice, and it is the processor architecture I will be concentrating on in this chapter. That the AVR is faster than a corresponding PIC may change with the introduction of the new dsPIC processor by Microchip, scheduled for release late in 2002. The dsPIC is an impressive architecture and should prove an extremely capable processor.

There are three basic families within the AVR architecture. The original family is the AT90xxxx. For complex applications, there is the ATmega family, and for small-scale use, there’s the ATtiny family. ATMEL also produce large FPGAs (Field-Programmable Gate Arrays), which incorporate an AVR core along with many thousands of gates of programmable logic.

For software development, a port of gcc is available for the AVR, and ATMEL provides an assembler, a simulator, and software to download programs into the processors. The ATMEL software is freely available on their web site. The low-cost ATMEL development system is a good way of getting started with the AVR. It provides you with the software and tools you need to begin AVR development.

The AVR processors at which we’ll be looking are the small ATtiny15, the AT90S8535/AT90S4434, and the AT90S8515.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.234.225