Path:
Home =>
AVR-EN => Absolute beginner
(Diese Seite in Deutsch:
)
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.
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.
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.
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:
- read the state of the input pin,
- write the state on the output pin, and
- 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:
- it has to define the input pin as input (which is the case by
default) and that the pin is only for reading, and
- the output pin has to be set as output, and, depending from
the attached external hardware on this pin, to deliver current
from its operating voltage pin or to drive current from this
output pin to the ground pin of the device.
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
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:
- Everyone has at least one bank card. In such cards a controller
knows my account number and allows me to access my account. With
this controller I can even load money onto my card and pay with
that card. The controller remembers that stored money and does
not forget or delete it. Some small metallic lips connect the
card with the hardware of the terminal, two of these lips provide
operating voltage to the card's controller and the other lips are
communicating with the terminal. Therefore, everyone today uses
controllers, even without knowing more about what is inside the
card. Try that with CMOS logic, it won't fit into the half
millimeter thick card.
- Something nice for (semi-)professional walkers: a controller,
connected to a sensor, can count your steps. The controller can
transmit the number of your steps to a smartphone or to a laptop,
and you can see on nice daily charts how many steps you made. Try
that with some CMOS ICs, the device is heavy hanging on your legs.
Not with a controller: small, nearly no weight, and the battery
works for the whole year.
- And another nice application: attach a small antenna wire and
the controller transmits. If he comes near to a terminal, that
provides enough energy for the chip to work, the apple (into
which this RFID chip has been mounted) transmits, which type of
apple this is, so the terminal can find its price in his database.
The terminal then uses your bank card's info to book automatically
the price for that apple from your account. As the RFID chip is
rather small, you don't have to remove it before eating the apple.
So the sewage cleaner collects thousands of those small transmitters.
- In each car a large number of microcontrollers work. They control
the ignition, warn you if it is dark outside and your head light is
not on, they measure the temperature and control your heater, or
calculate and display the miles. The professor also decides if your
transmitted code fits its hardcoded internal and unlocks the door to
let you in. But: if your temperature sensor is defective, the
professor might think that it is currently minus 80 degrees Celsius
outside, and he won't start the motor (to protect it from damages).
And you sit inside your car, you see the error message on your
display, and you are unable to drive, or repair something. You are
now the professor's slave.
- Every washing machine has such a controller to control the 25
different washing procedures, to let water in, to open the door
exactly 15 seconds after the last water had been pumped out, etc.
etc. In the earlier years this was performed by a mechanical device,
driven by a motor. Nowadays a small piece of silica does that. And
won't let you open the door, if the pump has not pumped out the
water because of a defect. Again: the controller controls you, not
you the controller.
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
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:
- a program storage memory: a Read-Only memory that durably holds
the zeroes and ones of the program code, can be cleared and
re-programmed with an externally initiated procedure,depending
from the type of AVR between 256 and 65,536 16-bit command can
be stored,
- a code-processing part that reads program code (roughly 100
different codes), translates it into single steps, such as
adding or subtracting plus subsequent writing the result to a
storage place, and executes those steps,
- a clock generator, that clocks the execution of those steps,
with a frequency of between 32,768 Hz and up to 20 MHz,
can be externally clocked with a crystal or ceramic resonator
e. g. at 4,194,304 rectangle swings per second (that is
222 and can be divided down to one event per second,
by a binary divider,
- a small storage area that the central processing unit can use
to add two binaries and to write the result to, this storage area
is called "registers" and AVRs have 32 of those storages
for 8 bits, the content gets lost when the operating voltage goes
below a certain limit,
- a larger storage area called static random access memory (SRAM),
where binaries of 8 bits can be stored and read, depending from
the AVR type between 64 and 32,768 storage bytes are available,
its content does not survive a loss of the operating voltage.
But, there is much more hardware inside, that can be activated by means
of program code:
-
external pins that can either serve as input or as output pins
and can be "High" (around or slightly below the operating
voltage) or "Low" (at or around the ground or minus
voltage), can be internally connected to a resistor to the
operating voltage (pull-up resistor), which can be externally
switched to ground (e. g. with a key),
- one or up to four timers/counters with 8 or 16 bit that can be
clocked either
- by the controller clock,
- by the controller clock divided by 8, by 64, by 256 or by
1,024 in a prescaler,
- by an external signal on a certain pin of the AVR.
The count can be continously compared with a programmable constant
and, if this matches, can
- either initiate an event, or
- can switch an external pin "High" or "Low"
or can toggle this pin.
- one or two analog comparer, that compare the voltages on two
input pins and accordingly set a bit that can be read by the program,
- none or one/two Analog-to-digital converters, that can measure
the voltage on between 4 and 16 programmable pins and convert their
analogue voltage to a 10 bit wide binary number, some can also be
programmed to measure voltage differences on two programmable
pins and can multiply the difference by 20 or 100,
- none, one or several interface pins to receive and transmit
serial signals on two pins (Synchroneous or Asynchroneous
Receiver and Transmitter), with the necessary shift registers
for receiving and transmitting serial bits,
- durable EEPROM storage cells allow to write and read content
that is preserved even in case of loss of the operating voltage,
- none or one 8-bit-multiplier, that can multiply two 8-bit
binary numbers,
- an interrupt logic, that can detect events such as
- voltage changes low-to-high or high-to-low at certain
input pins,
- result completions of the analog comparer(s),
- an overflow of a counter/timer or a match of comparer
constants,
- reception of a complete byte of a serial receiver or the
completion of the transmit of a byte via the transmitter.
This logic interrupts, if enabled, the controller at the current
execution address, diverts execution towards a fixed address in
the program store, and after completing execution there returns
back to the address where execution had been interrupted. If
two or several interrupts occur at the same time, a priority
scheme controls the execution of the interrupts. The interrupt
control allows to send the controller to sleep, where only very
small operating current is consumed.
- a logic, that allows to program the program memory from an
external source and replaces the internal program (self-download),
can be used to change the program within the electronic device
(with the risk at the user if the electricity source crashes
in between).
- by setting certain bits in a durable memory called
"fuses&qut; any read access to the program storage can be
blocked, protecting its content against copying.
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
This schematic shows such a microcontroller at work. It is
a coded entry key for your front door and consists of
- of course, a micro controller ATtiny13,
- a 5V operating voltage supply,
- a 10-turn potentiometer, on which a pre-programmed
voltage can be adjusted,
- a key that has to be pressed if the potentiometer is
adjusted to the programmed voltage,
- a speaker, that provides beeps for the user,
- a light-emitting diode that blinks in case of mis-adjustment
of the input voltage,
- a relais, together with a protecting diode against high-voltage
bursts when switching off, that activates the door opener, if
the controller will allow that.
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
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.
- 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.
- Then the direction of the pins has to be iniated. OC0A,
PB1 and PB2 are outputs and drive current:
- output OC0A is low, no current flows and the speaker
is quite,
- output PB1 is high, no current flows through the
relais and the door opener is inactive,
- output PB2 is high, no current flows through the
LED and its resistor, the LED is off.
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).
- 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
- he switches the relais on by clearing the PB1 output
(switching it low), and
- writes a compare value to Timer0, that produces a
1,000 Hz tone on the OC0A output, switches Timer0 on
and toggles the OC0A output, and
- continues that for five seconds, and
- switches the relais off by setting the PB1 output
high, and
- switches the Timer0 off and clears the OC0A
output, by that switching the speaker tone off.
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
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:
- AVRs were devellopped by ATMEL alone, while PICs had
several devellopping and producing companies. Nowadays
Microchip is one of the major suppliers (exclusive
for AVRs), while PICs have more suppliers. Even though
expected, prices for AVRs have not gone up, taking the
monopoly supply situation into account, even though
it has been speculated on this over the past twenty
years.
- The instructions in PICs are only 12 bits wide, in
AVRs 16 bits. Therefore AVRs have a much more advanced
instruction set, making AVR programming more effective
and execution much faster.
- There are further basic differences, but to understand
those we would need to go deeper into the details. All
those differences speak for AVRs, none for PICs.
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
- execution speed,
- instruction set (for optimzed program design and execution),
- several exclusive items, that are not available in PICs.
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
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 http://www.avr-asm-tutorial.net