circuitcellar.com
Magazine Support   Digital Library   Products & Services   Suppliers Directory 
 
 





 

Issue 127, February 2001
Working with AVR Microcontrollers


by Stuart Ball

Start The Hardware The Software Tools Design Considerations The Bottom Line Sources PDF

The Hardware

The AVR parts share many common hardware features. The program memory in most of the devices is stored in flash memory. This permits you to erase and reprogram the devices in-circuit. Unlike EPROM, no UV light source is required to erase the flash memory . All AVR devices can be erased or programmed using a serial interface; the larger parts (20 pins or more) also support a parallel programming mode.

All AVR devices have some EEPROM, ranging from 64 bytes to 4 KB. This memory can be written by the software and retains its contents even if power is removed. Writing to the EEPROM isn’t like writing to the internal RAM. The EEPROM requires a specific sequence of events to prevent spurious writes when the power is changing or the state of the CPU is otherwise unstable.

The EEPROM can be programmed externally, using the same interface as the flash memory. External programming enables you to do things like add compensation values for voltage references when the circuit is tested on the production line.

Another common feature of AVR devices is 32 internal 8-bit registers. For some of the smaller parts, this is the only RAM the devices contain. Unlike many microcontrollers with a dedicated accumulator (or working) register, the AVR architecture will let you perform almost any operation on any of the 32 registers.

That "almost" is an important consideration. The register set is broadly broken into two halves; the upper 16 registers support any register-based operation. The lower 16 support only some of these operations. Some of the registers can be paired to do double duty as 16-bit pointers. These paired registers can be used as general-purpose registers if the pointer function is not needed (see Figure 1).

2102027-f1.gif (9258 bytes)
Figure 1—Operations such as add, subtract, AND, OR, complement, compare, increment, decrement, XOR, and load/store (direct and indirect) are restricted to the upper 16 registers. The first six registers are used in pairs as pointer registers for indirect addressing. Some 16-bit arithmetic can be performed on these six.

AVR registers are implemented as a block of SRAM. Along with the 32 registers, most AVR devices have additional SRAM ranging from 128 bytes to 4 KB located in a contiguous block at the end of the register space.

The AVR processors execute one instruction per clock cycle. Although this may not seem like an important architectural consideration, it is. Many processors divide the external clock to produce the necessary internal phases for the CPU, resulting in an instruction clock that is a submultiple of the input clock. Microchip’s PIC processors, for example, divide the external clock by four, so a 20-MHz clock is required to achieve a 5-MHz instruction rate. The original 8031 divides its clock by 12. The AVR processors execute one CPU clock per input clock. A 10-MHz input clock equals a 10-MHz instruction rate.

Like all microprocessors, some AVR instructions take longer than one clock cycle to execute. Immediate instructions take two cycles to execute, branch instructions take one to three cycles, and so on. But the one-clock-per-cycle feature can reduce EMI in your system, because you don’t need that 20-MHz clock to get a 5-MHz instruction rate.

All microprocessors require a reset after powerup to ensure that the hardware is in the correct state. AVR processors have a reset input, but they also have internal reset logic that monitors the power supply voltage and ensures that the chip is correctly reset. External reset components aren’t required.

When enabled, the AVR processor’s watchdog timer generates a reset if the CPU doesn’t refresh it before it times out. The watchdog timer runs from a separate 1-MHz internal oscillator. It can be programmed to produce timeouts from 16 ms to 2 s.

The simpler AVR devices have an 8-bit timer. More sophisticated parts may have an additional 8-bit timer as well as 16-bit timers. The 16-bit timers support input capture (capturing the value of a free-running counter when an input pulse occurs), output compare (generating an output or interrupt when a free-running counter reaches a certain value), and PWM output capability.

In some versions, internal UARTs provide serial communication to the outside world or to other processors.

Multi-channel ADCs are available on some AVR devices. These use a single ADC and an input multiplexer to convert multiple input channels.

Some AVR devices provide an internal oscillator to eliminate the need for an external crystal. This also frees two pins for use as general-purpose I/O. The internal oscillator is not as precise as a crystal, so some of the parts provide a means to adjust the clock when the flash memory is programmed.

Most AVR general-purpose I/O pins can sink up to 20 mA, so they can be used to directly drive LEDs or other devices that need high sink current. The pins cannot be driven beyond the positive supply (Vcc), so they can’t be used to drive high-voltage devices. If a pin is used to directly drive a relay or solenoid coil, be sure to add a zener or clamp diode so the fly-back voltage doesn’t destroy the AVR.

Using a direction register, you can choose on a bit-by-bit basis whether a pin is to be used as an input or output. There is one direction register for each I/O port and one bit in the register for each I/O pin.

When you read a port, the AVR architecture lets you read either the state of the output register or the state of the port pins. Execute an OUT instruction to write data to a port. Execute an IN instruction to read data from a port. IN Portx reads data from the output register and IN Pinx reads the external port pins.

Many AVR devices include an on-chip analog comparator. This circuit compares two analog voltages and sets the output high if the positive input is higher than the negative input. The output is low if the negative input is higher than the positive.

The output of the analog comparator can be directly read by the AVR software or it can generate an interrupt. The interrupt can be programmed to occur when the comparator output goes high, low, or changes state. On AVR devices with timers that have input capture capability, the comparator can be programmed to trigger the input capture function.