Path: Home => AVR-EN => Absolute beginner    (Diese Seite in Deutsch: Flag DE) Logo

AVR for absolute beginners

This is what AVR means and how it can be used for absolute starters, that know near to nothing about those and want to have a brief intro on what these are and for what those can be utilized.

What does AVR mean?

The simplest answer: it means nothing. It is simply a trademark of ATMEL and has rather to be written as AVR®. Whatever other people in the net speculate: forget it, it means nothing.

Understanding the origin of the term AVR does not contribute to understanding the AVRs, so you have to dig a little deeper.

What is a microprofessor?

AVR controllers They are just looking like any other integrated circuit: with a black plastic package and more or less metallic pins. The smallest (top left, no this isn't a bug or a mini-spider that ran into the foto) has six of those legs, the second-smallest eight and the largest displayed has fourty. There are larger ones, but those are not suitable for a short-sighted homebrewer in an advanced age.

Microcontrollers are, by first view, electronic components just like transistors, diodes, resistors or capacitors are. They are supplied with an operating voltage via two of their pins and consume some current, but not much of it.

An integrated circuit (IC) is a collection of several single components (transistors, resistors, etc.) in one package. Unlike in other ICs the number of integrated components here is much larger than in a simple TTL or CMOS IC: many thousands are encased in this small black box, even in the smallest shown. Predominantly plastic plus metal pins, but those do not the work and are less interesting than what is inside.

I call those guys professors, because they are, by default, useful for near to nothing, just like an academic for mastering life. And: AVRs do not even look very impressive (which at least some professors do), as can be seen from this picture of some of those professors.

What does a microprofessor?

Electronists attach the external pins of their controller to the rest of their electronic device and the internals measure or check what happens on those pins. What the pins are (digital in- or outputs, analog inputs, etc.) and do has been defined in the program. The binary program has been written to the flash storage of the professor. What happens if a certain condition occurs, all is written in this program.

To make an example: the program reads the digital state of an input pin, copies this bit to another pin, and then restarts that loop. This converts to three steps in the program:
  1. read the state of the input pin,
  2. write the state on the output pin, and
  3. restart from the beginning.
As long as the controller has operating voltage, he executes those three steps, and does not get tired to run in that circle.

The program has to perform the following additional steps: Nearly all pins of the controller can be used for that, and be adviced to be in- or output. This makes such ICs more versatile than TTL or CMOS ICs (where in- and outputs are mostly fixed). Only the program decides, if the same pin is a digital in- or output or if it is an analogue input, or whatever else. Or the program switches the pin type in between, which can be made with classical IC logic but requires multiple devices for its control. Better do not try that with CMOS.

An urgent notice for bloody beginners: Do not buy a controller unless you are able to program it. Without that program the controller is as silly as straw: all pins are inputs and do absolutely nothing. The IC seems to be dead. Only after having programmed it, the dead IC comes to life and does something. Something means: doing something that you programmed it to do. Nothing yields nothing.

To the top of that page

For what can a microprofessor be used?

The above discussed program (copying the state of a pin to another pin) is rather useless, because you can do that without a controller in between. Here are some better examples, what can be done with controllers: Nowadays no one can escape those professors, we are all slaves of such devices. Be it a short spike on the operating voltage, a strong magnetic field or an electric burst, those can fail and they do what they want. If they fail, we are their victims (Spliff: Computers are silly). They control us, not we control them. It is a pity that not many people know how these work.

To the top of that page

What is inside a microprofessor?

To know what is going on inside such small black packages, here a list of what is inside and can be invoked with a few lines of program code. Always inside are: But, there is much more hardware inside, that can be activated by means of program code: This brief trip into the internals demonstrates that these professors can do nearly anything that you might need. What, in earlier days, needed lots of electronic components is nowadays all inside these black boxes and waits for getting involved with some code lines.

The most AVRs cost only a few cents or € and include that all. If you order any 100,000 of these, you'll get them for a price smaller than for an egg. The small price and the versatile internals of such controllers are responsible for being followed and controlled by those devices.

To the top of that page

How to attach a microprofessor?

Schematic key code This schematic shows such a microcontroller at work. It is a coded entry key for your front door and consists of The secret position, where the key successful opens the door, can be set or adjusted if the key is pressed during the first application of the operating voltage. Then the position is measured and written to the EEPROM. This stores the position durably, even during losses of the operating voltage.

The wole device fits onto a small PCB and is considerably smaller than such a device with CMOS ICs.

To the top of that page

How to program a professor?

With a new, blank controller, the device does nothing. It is only working if the prgogram has been programmed to its program storage space.

The program has to do the following.
  1. After switching the operating voltage on, the controller has to check whether the EEPROM location with the key bytes is empty (e.g. larger than 1,023). If that is the case, the AD converter has to measure the voltage on pin 2 and to store this result to the respective EEPROM location. You can think about blinking the LED for a while, just to signal the initial re-adjustment.
  2. Then the direction of the pins has to be iniated. OC0A, PB1 and PB2 are outputs and drive current: The PCINT4 input pin is input, its internal pull-up resistor is activated and its digital input signal reads high.
    ADC3 is neither in- nor output (it has its digital driver switched off). It measures the voltage of the potentiometer by converting it to a digital value between 0 and 1,023 (hexa decimal 0x0000 to 0x01FF).
  3. Normal program execution is interrupt-controlled while the controller sleeps. He only wakes up if the key is pressed. Then he starts an AD conversion and, and after completion, compares the result with the value stored in the EEPROM. If that matches, then If no match is recognized, the PCINT4 interrupts are disabled (no more reaction) and the LED blinks for one hour. Only after following that PCINT4 interrupts are re-enabled. So it lasts, at average, 512 * 1 hour = 21 days for a brute force attack on your key controller.
This program sequence is written in assembler language into a text file named "keycode_v1.asm" and feed this to an AVR assembler program (preferably my own one named gavrasm, available as Pascal source code or as executable for Linux and Windows). If no error message occurs two new files are generated by the assembler, named "keycode_v1.hex" and "keycode_v1.eep". These have unreadable hexa decimal content as ASCII characters, but every programmer software knows what to do with those unreadable characters: to burn it into the flash storage (keycode_v1.hex) and into the EEPROM of the device (keycode_v1.eep). Then the controller has what it takes to perform its task: instruction words. And he executes those, just like we had planned.

If we have forgotten something or formulated single steps falsely, the controller does, neverteless, what he is said to do with the wrong code: he doesn't do what we expect, but he is exactly doing what he is told. Such a bug in the source code can drive you crazy. There is no other method available than going through the code again and again, and to find what is wrong. If you come near to such a debugging person, be cautios! He can throw stuff at you, if you ask him whether he is hungry (yes he is, but he cannot eat given the state he is in) or if you ask him to taking a walk with the dog (he is able to walk, but not now). If the bug is found and fixed, this debugging state is over, and the person will return back to normal behaviour.

If the program is free of bugs, the whole is mounted into the door and the family members are happy opening the door without the key hardware. Until you've re-programmed the code and did not communicate the new combination. In that case you'll have to use an attack method named "e;brute force" to open the door. As you have to wait for an hour to test the next combination, you'll need 1.023 hours for that (because it is always the last combination that matches). A matter of 42 days or one year (if you work on that project for 8 hours per day only). A powerful design!

To the top of that page

What makes AVRs unique?

For the amateur only a few controller families are practically applicable. Only two families are practical: PICs and AVRs. All others are industrial products, have too large hurdles for the amateur and don't come into question.

A few differences between AVRs and PICs are: In the following tables different AVR- and PIC-types are compared using a larger number of criteria. Not the most recent types have been selected, but types that are longer available. Both types are of a similar age and are comparable types.

Table 1: 8 pin

PIC vs. AVR, 8 pin

PIC AVR Better: Factor:

Dimension 10F200 ATtiny13

max. clock [MHz] 4 20 AVR 5
Time per instruction [µs] 1 0.05 AVR 20
Instructions [Number] 33 120 AVR 3,64
Internal RC [%] 1 10/3 PIC 3,33
Register [Number] 1 32 AVR 32
Program storage [Instructions] 256 512 AVR 2
EEPROM storage [Bytes] 0 64 AVR exclusive
SRAM storage [Bytes] 16 64 AVR 4
max. stack depth [Addresses] 2 32 AVR 16
Interrupts [Number] 0 10 AVR exclusive
8 bit timer [Number] 1 1 - -
8 bit PWM [Channels] 0 2 AVR exclusive
AD [Channels] 0 4 AVR exclusive
Price (Reichelt) [€] 0.49 0.70 PIC 1.42

Table 2: 28 pin

PIC vs. AVR, 28 pin

PIC AVR Better: Factor:

Dimension 16F870 ATmega8

max. clock [MHz] 20 16 PIC 1.25
Time per instruction [µs] 0.2 0.06 AVR 3.2
Instructions [Number] 35 130 AVR 3.71
Internal RC [%] 1 3/1 PIC 3
Register [Number] 1 32 AVR 32
Program storage [Instructions] 2048 4096 AVR 2
EEPROM storage [Bytes] 64 512 AVR 8
SRAM storage [Bytes] 128 1024 AVR 8
max. stack depth [Addresses] 8 512 AVR 64
8 bit multiplier - no yes AVR exclusive
Interrupts [Number] 11 19 AVR 1.73
8 bit timer [Number] 2 2 - -
16 bit timer [Number] 1 1 - -
8 bit PWM [Channels] 0 3 AVR exclusive
10 bit PWM [Channels] 1 2 AVR 2
16 bit PWM [Channels] 0 2 AVR exclusive
ADC [Channels] 5 5 - -
UARTs [Number] 1 1 - -
Price (Reichelt) [€] 2.50 1.45 AVR 1.72

Table 3: 40 pin

PIC vs. AVR, 40 pin

PIC AVR Better: Factor:

Dimension 16F877 ATmega32

max. clock [MHz] 20 16 PIC 1.25
Time per instruction [µs] 0.2 0.06 AVR 3.2
Instructions [Number] 35 131 AVR 3.74
Internal RC [%] 1 3/1 PIC 3
Register [Number] 1 32 AVR 32
Program storage [Instructions] 8192 16384 AVR 2
EEPROM storage [Bytes] 256 512 AVR 2
SRAM storage [Bytes] 368 1024 AVR 2.78
max. stack depth [Addresses] 8 512 AVR 64
Interrupts [Number] 15 19 AVR 1.27
8 bit multiplier - no yes AVR exclusive
8 bit timer [Number] 2 2 - -
16 bit timer [Number] 1 1 - -
8 bit PWM [Channels] 0 1 AVR exclusive
16 bit PWM [Channels] 2 2 - -
ADC [Channels] 8 8 - -
UARTs [Number] 1 1 - -
Price (Reichelt) {€] 2.99 2.75 AVR 1.09

As can be seen from all three tables AVRs are better in most of the cases, in respect to The only advantage of PICs is their higher accuracy of the internal RC clock generator. Prices are not among a relevant argument.

To the top of that page

What can an amateur do with that?

The examples have shown that you get a whole arsenal of electronics for a few cents if you use microcontrollers. With that you can solve nearly any control and regulation task. Assistance for learning, hints and tools for programming are for free, the only investment to be made are the time that you need for learning AVR assembler. Do not think you can do that with other languages: you'll not get happy with other languages. Already the third box that you've built and programmed pays out your investment.

To the top of that page

©2012-2019 by