Homebuilt underwater robot

 

AVR processors

General description

I will focus on ATMega8 micro controller, but the description is valid for other AVR micro controllers as well.

AVR is a family of 8-bit micro controllers. They're based on RISC core, it means: most instructions are executed in one clock cycle, large number of general registers, most operations can have any register(s) as arguments. They integrate FLASH memory for program storage, SRAM memory, and non volatile EEPROM. Below is a list with some informations:

  • FLASH – 8 kB, endurance 10,000 write/erase cycles,
  • SRAM – 1 kB,
  • EEPROM – 512 B, endurance 100,000 write/erase cycles.
As you can see the amounts are suitable for most, even sophisticated purposes. Note, that one instruction occupies 16 bits (2 bytes), in 8kB of memory you can fit 4k instructions.
Here are listed peripherals integrated in the micro controller:
  • internal RC oscillator – there's no need for crystal oscillator (which may be needed in case of ISP programming),
  • advanced timers (one 8 bits and two 16 bits) which are used for PWM, real time counters, and other,
  • 10 bits, 6 or 8 channel analog-digital converter,
  • analog comparator,
  • two wire interface (=I2C by Phillips),
  • USART (popular serial port),
  • master/slave SPI interface (used in some AD or DA converters),
  • watchdog,
  • five sleep modes.
The micro controller, depending on it's version, can be clocked from 0 MHz to 16 MHz (ATMega8) or 8 MHz (ATMega8L). Operating voltages: 4.5–5.5V and 2.7–5.5V in case of ATMega8L. Power consumption at 4 MHz, 3V, 25°C: 3.6mA (active mode), 1.0mA (idle), 0.5µA (power-down).

ISP

ISP is abbreviation of In-System Programmable, ATMega has additional feature – In-System Self-Programmable it simply means that the FLASH (and EEPROM) memory can be programmed without desoldering or ejecting the chip from the socket. During design you must remember to add 10 pins connector and make proper connection with ATMega8. If the AVR is not set for use with internal RC oscillator you have to attach some clock source (the one for which the microcontroller if configured for - for example crystal oscillator). Clock source can be configured using fuse bits, which are set up during the MCU programming. There's also other possibility - the micro controller can program itself using (programmed earlier) boot code – code can be downloaded ex. through serial port or two wire interface (I2C).

Design guidelines

Simply add this connector to your design:

ISP connector for ATMega8

Below is a closer look on the connector (sometimes known as IDC10 or ML10), it's stk200 starter kit compatible. You can use any other connector, just remember about all signal wires (RESET, CLK and so on) and the plug which must be the same as in your programmer. There are number of plug "standards" which can be found on Internet, I think that stk200 is the best choice because it's the same as in Atmel starter kit and many programmers support it.

ISP connector wires

The third pin is described as NC (not connected), but you can connect a LED (on the AVR side) to indicate programming process.

The ISP interface can coexist with devices attached to the same MCU port lines, as long as they're used as outputs, not inputs. Imagine, if an external device pulls down one of the signals, the ISP interface won't work.

You can also make use of bootloader code, which you must write on your own, or use a precompiled one. In that case you don't need to have the ISP interface on your PCB, but remember - the MCU must have some external interface (serial, I2C) to download the code, additionaly you need to program the bootloader code first. Good: save some space on PCB, free up some port pins, no need to have a direct access to the board - MCU firmware updates can be made externally; bad: can be slower, bootloader takes up some FLASH space, you must write a bootloader.

Programmer device

What's great about ISP is that you don't need sophisticated programmer. I will describe here a simple LPT (parallel port) programmer. There are simpler designs (just wires), but I recommend this one – TTL 74HC244 will protect the parallel port from being damaged. Here's a schematics (based on schematics on PonyProg site):

stk200 programmer

If you use SMD parts the programmer will fit in DB-25 casing. To program the device connect the programmer with your PCB one to one cable. To program you need some tools, read below.

Tools

Another important issue with AVR processors is that there are a lot of free tools available. I'll list them here with short description:

  • avrdude – this is a programming tool which can be used with stk200 programmer. It's available for Linux and Windows. This is command line tool,
  • PonyProg – Available programming tool with GUI. Personally I prefer avrdude. Available for Windows and Linux,
  • gcc – gcc is a C and C++ compiler which supports AVRs, it's good because it's free and generates good code. Available for Linux and Windows,
  • AVR libc – this is a collection of standard C routines which can be used in your programs. Available for Linux and Windows,
  • AVR Studio 4 and AVR Studio 3.5 – Atmel tools (Integrated Development Environments) for developing AVR programs in assembler. AVR Studio 3.5 can be used with avr gcc,

JTAG

Atmel ATMega16 (but not ATMega8) has built in JTAG support. This allows on chip debugging. To use this very usefull feature you need a special interface. It is a bit more complicated to built than ISP interface. The following picture is JTAG interface designed by Aquaticus team (based also on ATMega16 chip). What is JTAG and how to build simple interface is available in JTAG for AVR processors.

Aquaticus JTAG interface

More help

It's always hard to start, if you get familiar with the AVR architecture everything be easy. The "bad" thing about AVRs is that, on the beginning, it's hard to learn all the quirks with different issues (like programming) – the CPU is highly configurable, it makes it more flexible but harder to learn. That's my opinion of course. Below are some links where you can find tutorials and where you can ask for help. The knowledge base for you is AVR data sheet, it's a bit technical, but has a lot of examples (in assembler and C) how to use AVR peripherals. You can find data sheets on Atmel site.
Here are, the earlier mentioned, links:

  • AVR freaks – comprehensive knowledge base. It has some tutorials how to learn programming in AVR assembler and c. To access some areas (tutorials) you need free registration,
  • avr libc documentation – explains how to use libc library, also gives guidelines how to use other tools (like avrdude). It explains a lot of issues related to AVR programming.

There is also one more possibility. If you do not like C or assembler, you can use BASIC. There is a commercial product BASCOM – perfect for small projects. Has many high level functions and is very easy to use. You can compile up to 4kb of code for free. I highly recommend BASCOM for beginners.