What is a microcontroller?

The best way to explain what a microcontroller is, is to start with your computer. Your desktop computer
(or laptop) is comprised of multiple parts, a CPU (such as a Pentium or Celeron), some RAM, a hard
disk, a keyboard and mouse and a monitor screen. Programs are stored on the hard disk and run on the
CPU, with temporary data stored in RAM. You can run multiple programs at a time by having one
'master program' called an operating system (such as Linux, Windows or Mac OS X) and that master
program keeps track of things for you.
The AVR chip has components, too. It has a CPU, some flash storage, some RAM and some EEPROM,
all in one little chip!. The CPU is just like the one in a computer, but its much simpler and not nearly as
fast (what do you expect for $2.50?) The flash storage is just like the flash storage in your mp3 player or
digital camera card, except its used to store programs. Its kinda like the hard disk of the microcontroller,
except you can only read from it. The RAM is just like computer RAM. The EEPROM is kinda like
flash except you cant run a program from it, but its used as long term storage. The EEPROM doesnt get
erased when the chip loses power.
So, to recap: The AVR chip runs whatever program is stored in the flash, uses the RAM for temporary
storage and the EEPROM for longer term storage.
Most computers have a 32-bit CPU running at 1GHz, with 1GB of RAM and 100 GB of storage. The
kinds of microcontrollers discussed here run at 10MHz, have 1KB of RAM and 10KB of storage. (On
the order of) However, their small size, lower power consumption and low cost make them an excellent
choice for many projects!
How to do something
OK so now that its clear what microcontrollers do, how do you get it to do something?
1. Well first off you have to write a program (or have someone write one) that tells the chip what to do.
2. Then you have to compile it, that is, turn the program description into machine code.
3. Next you program the chip using a programmer, which will trasfer the machine code to the device
4. Test, debug, repeat!
First step is setting up your computer for programming, so follow the setup steps for Mac or Windows to
get that done.
Comments? Suggestions? Post to the forum!
http://www.ladyada.net/learn/avr/whatisit.html

Introduction
So you have this chip, and its programmable. But how do you get your program onto that chip?
Comments? Suggestions? Post to the forum!
What is a program?
When code is compiled into a windows program, its turned into "binary" which means its not human-
readable (which is tough for machines to understand), instead it is machine-readable. Under windows,
these programs (often calledapplications or executables) are often named ending with .exe (for
example notepad.exe or winword.exe), on Macs, they are often named ending with .App (although the
Finder hides it).
For microcontrollers, binary files end in .hex (short for Intel Hex Format) There are other formats
possible, but this is pretty much standard.
Your compiler will generate a .hex file from code, and then all you have to do is transfer
that .hex program to the chip!
What is flash?
As discussed in What is it?, the AVR chip has a small amount of flash memory in it. That memory is
where the program is stored. When the chip starts up (you give it power) it starts running whatever
program is in the flash. So all we have to do is figure out how to write to that flash
A good way to think about flash is to examine it as you probably are familiar with it. Flash storage is
also used in MMC/SD cards, which are commonly used in MP3 players or digital cameras. If you look at
the bottom of a card, you'll see little gold fingers. These are the interface pads.

Back of Multi Media Card. By Andrzej Barabasz.
If you look up in the MMC card specification, you'll find out that these are what the 7 pins are for.
(SD/high speed cards may have extra pads, but that standard was added later because the existing
standard was considered too slow for big megapixel cameras)
Pin # Pin Name Pin Function
1 #CS Chip Select (Activate)
2 SI Serial In
3 GND Ground
4 VCC Power
5 SCK Data Clock
6 Not connected Not connected
7 SO Serial Out
The power and ground pins are used to connect power to the chip. The Chip Select pin is used to tell the
chip that it should wake up, Serial In pin is used to send data to the card, Serial Out is how data is sent
from the card and Data Clock is how we tell the card how fast we want data to be stored or retrieved.
Data is pushed in an out of the chip one bit at a time, so if the clock rate is 100KHz that means that
100,000 bits are transferred per second.
When you want to retreive or store data on a flash card all you have to do is stick the card into a flash
card reader. The reader then has a driver on your computer which lets you simply drag files onto a
picture on your screen and it does all the hard work of sending the data to the card.
OK, so what does this have to do with a programmer? Well, the microcontroller is like the flash card and
the card reader is like the programmer. When you stick the chip into the programmer (or, more likely,
the programmer cable into the circuit board that contains the chips) it allows the software on your
computer to talk to the chip through the programmer. Usually you can just give a simple command like
'send this program over' and the programmer will do all the hard work of programming and verifying the
data
In-System Programming interface
Every AVR has a set of pins that are the programming pins, you just have to connect the programmer to
those pins in the right order and presto you are ready to program it. Unfortunately, nearly every chip has
a different pinout so its imperative that you look in the datasheet for the correct pins.
For example, I'm a big fan of the ATtiny2313 chip, so lets open up the 'summary' datasheet (its only a
dozen pages long instead of the standard 250. The first page has all sorts of information about what is in
the chip.
Quick Quiz : How much EEPROM does the chip have? How about flash storage? RAM?
The second page has pinouts. We will be dealing with the PDIP package (Plastic Dual In-line Package)
which is the kind that looks like this:

The pinout is reproduced here

The chip has 20 pins. Look carefully and you'll notice 6 of these pins are equivalent to the SD/MMC
card pins. There is a power pin (VCC) and a ground pin (GND), there is also an SCK pin (its
called UCSK for some reason) a Serial Out pin (MISO - Master In Serial Out ) and a Serial In pin
(MOSI - Master Out Serial In). The only thing is doesnt have is a /ChipSelect pin. Instead it has
a /RESET pin, which acts in a similar way. When the /RESET pin is at a postive voltage (the same
voltage as VCC) the chip runs the program, when the /RESET pin is at ground (the same voltage
as GND) then the chip stops and listens for programming instructions on the programming pins.
All SD cards have the same fingers in the same place so that when you slide it into the card reader, the
right pads are connected. AVR programming works in a similar way. Instead of pads, the chip is often
placed in a circuit board which has header pins that the progammer plugs into, the header pinout is
standardized so that any programmer can be used once the header is wired up correctly.
There are two standards for AVR in-system programming:

The left is the 6-pin header standard, the right is the 10 pin standard, the headers look like this:

Here is an example photo showing what it looks like when the chip is wired up with a 6-pin header


Photo courtesy EvilMadScientistLabs
The black mark on the PCB and header indicate pin #1. You can trace the wires to correllate the pins of
the chip to the pins of the header and verify for your self that this breadboard is correct.

Exercise: Verify the pinout for this target board
To learn how to make a 'target board' that lets you program AVRs (in particular, said ATtiny2313)
please visitEvilMadScientistLab's tutorial. Its quite easy and will cover some of the same material.
Now that you know how programming works, its time to chose your weapon programmer.

Introduction
Fuses are an extremely important part programming a chip, but are rarely explained thoroughly. You
only need to set them once, but if you don't do it right, it's a disaster!
Comments? Suggestions? Post to the forum!
What is a fuse?
You know about flash, eeprom and RAM as parts of the chip. What I did not mention is that there are
also 3 bytes of permanent (by permanent I mean that they stick around after power goes out, but that you
can change them as many times as you'd like) storage called the fuses. The fuses determine how the chip
will act, whether it has a bootloader, what speed and voltage it likes to run at, etc. Note that despite
being called 'fuses' they are re-settable and dont have anything to do with protection from overpowering
(like the fuses in a home).
The fuses are documented in the datasheets, but the best way to examine the fuses is to look at a fuse
calculator such as the avr fuse calculator from the palmavr project
Click on that link in a new window and select ATtiny2313 for the fuse calculations

We'll use the Quick Configuration so use those menus, not the checkboxes

Clock selection
The first option is how the chip is clocked. Every CPU uses a clock, The clock keeps track of time for
the chip, in general one assembly code instruction is run every clock cycle.The one in your PC has a
clock that runs at 1GHz or higher. This little chip runs much slower. If you look at the menu you'll see a
huge list of options, but looking carefully you'll see there are two groupings, the Clock Source,
the Clock Startup
The Clock Source can be either of the following:
External Clock, Internal 8MHz clock, Internal 4MHz clock, Internal 128KHz clock, External
Crystal (0.4-0.9 MHz), External Crystal (0.9MHz - 3.0MHz), External Crystal (3.0MHz -
8.0MHz) or External Crystal (8.0MHz +)
The Clock Startup can be either of the following:
14CK + 0 ms, 14CK + 4 ms, 14CK + 65 ms.
External Clock means that a square wave is being input into the CLOCK-IN pin. This is pretty rare
unless you have a clock generating chip. Don't use this unless you're sure you mean to
Internal Clock means that theres a little oscillator inside the chip, its not very precise but good for most
projects that dont have fine timing issues. The clock varies with temperature and the power supply
voltage. You can chose from a 8MHz, 4MHz or 128KHz clock. The 128KHz clock is for very low
power applications where running the chip very slowly helps conserve power. Having an internal
oscillator means we don't need to wire up a crystal and we can use the clock pins for our own nefarious
purposes.
External Crystal
If you need a special clock rate, like 3.58MHz or 12MHz or a high precision clock that won't drift with
the temperature, you'll want an external crystal or oscillator.
Crystals look something like this: Ceramic oscillators look like this:

Crystals come in multiple different pacakges, they may be cylindrical, or larger than the image shown. In
both cases the speed is printed on top or on the side, almost always in MHz.
The Startup Time is just how long the clock source needs to calm down from when power is first
applied. Always go with the longest setting 14CK + 65ms unless you know for a fact your clock source
needs less time and 65ms is too long to wait.
By default, chips that come from the factory have the Internal 8 MHz clock with 14CK + 65ms
Startup.
Clock Output
The next option is Clock Out on PortD2 which means, basically, that whatever the clock input is
(internal, external, crystal, etc) a square wave of the same frequency will appear on pin D2. This is
useful if you're debugging the clock rate, or if you want to use the clock to drive another chip.
By default, chips that come from the factory have this turned off.
Clock Divide
This fuse causes the chip to divide the clock rate by 8. So if the clock source is set to Internal
8MHz and you have this fuse set, then you'll really be running at 1MHz.
By default, chips that come from the factory have this turned on.
Reset Disable
This fuse turns the Reset pin into a normal pin instead of a special pin. If you turn this fuse on you cant
program the chip using ISP anymore. I would suggest you never set this fuse unless you really mean to.
By default, chips that come from the factory have this turned off (that is, Reset is enabled)
Brown-out Detect (BOD)
These fuses set what voltage to turn the Brownout protection circuitry on. A "brown-out" in this case is
the same as when the power-grid gets over-taxed by thousands of people running their air conditioners in
the summer: the voltage drops and your clock radio maybe stops working. A brownout for a chip means
that the power voltage is too low for it to run reliably at the speed of the clock.
For example, the attiny2313 can run as fast at 20MHz but only if the powervoltage is between 4.5V and
5.5V. If the voltage is lower than that, it may behave erratically, erasing or overwriting the RAM and
EEPROM. It may also start running random piece of the flash program. To keep it from doing that, set
the brownout voltage to 4.3V, then if the voltage dips, the chip will turn off until the voltage returns. It
will then reset and start over.
If the chip is meant to run at 5V, set the brown-out to 4.3V. If the chip can run as low as 3.3V you can
set the brown-out to 1.8V. If the chip is a 'low voltage compatible' chip such as the attiny2313V (which
can run as low as 1.8V if its clocked at 4MHz or less) then you can set the brownout to 1.8V. You can
read the speed and voltage grades on the front page of the datasheet.
By default, chips that come from the factory don't have brown-out detect however I suggest you set it if
you can. If you have a bootloader or are storing data in the EEPROM you must set the BOD!

http://www.engineersgarage.com/articles/avr-microcontroller?page=2
http://www.avr-tutorials.com/general/microcontrollers-basics