Professional Documents
Culture Documents
AVR Basics
If you are reading this tutorial, you should be already familiar with electricity and
microcontroller basics. If not, read through Introduction to Electricity and Microcontroller
tutorial
and
then
return.
AVR microcontrollers can be termed as a mini computer with all peripherals on the chip. A
typical AVR microcontroller can contain peripherals like RAM, EEPROM, Flash memory, InputOutput (I/O) pins, Analog to Digital converters, PWM channels, Timers etc. It also has a CPU
for
processing,
but
not
as
fast
and
complex
as
the
one
within
computer.
These AVR microcontroller (from now on termed as cs) is an 8-bit microcontroller and
based on Reduced Instruction Set Computer (RISC) architecture. 8-bit means that the c
can transmit and receive data in a set of 8 bits. Atmel manufactures 3 variations of 8-bit
microcontrollers.
TinyAVR
MegaAVR
XmegaAVR
These variations are there to distinguish based on their physical size, memory size, number
of inbuilt peripherals and their applications. MegaAVR is the most popular one with enough
memory
for
our
basic
projects
with
suitable
peripherals.
Atmega8 is one such MegaAVR which will be discussed here, in this tutorial. Atmel provides
datasheets for each AVR manufactured. They look so complex and huge that it is easier to
get lost within that. However it is recommended to understand one datasheet and the
format remains same for others too. Atmega8 data sheet can be foundhere, with 300 and
odd pages. The first page provides information on all the features included in Atmega8 and
the second details pin configuration. Information in these two pages can be kept handy as a
basic reference. Some of the features can be highlighted here:
1 KB internal SRAM Static Random Access Memory, a fast power efficient storage
for data values. However SRAM is volatile, meaning data is lost on power off.
10,000 Flash/100,000 EEPROM: This means you can rewrite your program 10,000
times before your AVR dies (technically).
23 programmable Input-Output (I/O) lines and 28 pins: These I/O lines are what you
use to interact with your Atmega8, and it contains 28 pins (5 pins for power lines and
control)
Operating voltage: 4.5 - 5.5V: This means your can use your Atmega8 within this
power range. Less voltage? It does not respond. More Voltage? You fry your c and need
to purchase a new one
There are also other features like interrupts, timers, PWM controls, Analog to Digital
Converters (ADC) and more, which we will discuss as and when we use those features in our
projects.
CPU uses these ports to read input and write output into them.
Let us take an example of a commonly used c, the Atmega8. It has 3 ports: PortB, PortC,
and PortD. Each of these port is associated with 3 registers - DDRx, PORTx and PINx which
sets direction, input and output functionality of ports. Each bit in these registers configures
a pin of a port. Bit0 of a register is mapped to Pin0 of a particular port, Bit 1 to Pin1, and
Bit2 to Pin2 etc. They have a one to one relationship between them. Let us discuss each of
these registers in detail and their functionality.
DDRx
DDR stands for Data Direction Register and x indicates port alphabet. As the name
suggests, this register is used to set the direction of Port pins to be either input or output.
For
input,
set
and
for
output,
set
1.
Let us consider PortB, as shown in the figure. To set this port as input or output, we need
to initialize DDRB. Each bit in DDRB corresponds to respective pin in PortB.
Suppose we write DDRB = 0xFF, then all bits in PortB are set to Output (1). Similarly,
writing DDRB=0x00 configures the port to be Input (0).
What if we need to make few pins of a port as input and others as output? The rules remain
the same. Write 1 to the respective pin to make it output and 0 otherwise.
Example, writing DDRB = F0, then the binary equivalent is 11110000. This means the first
four pins are set to be Output and the last four pins are
set to be Input.
DDRB:
PORTB:
PB7
PB6
PB5
PB4
PB3
PB2
PB1
PB0
Status:
Output
Output
Output
Output
Input
Input
Input
Input
PORTx
If DDRx sets the direction, PORTx sets value to the corresponding pin. DDRx can set a bit to
be either input or output, PORTx changes its functionality based on it. Let us consider both
cases.
When pins are set as Output
Suppose all pins are set to be output. i.e. DDRx = 0xFF. Now, writing 1 to a bit in PORTx will
make output high (normally +5 Volts) for that particular pin; writing 0 makes output low
and gives low (0 Volts) in that particular pin.
When pins are set as Input
Suppose a particular bit in DDRx is set as Input (0), then there are two possibilities. If
corresponding PORTx bit is set to 1, then an internal pull-up resistor is enabled. This means
even if we do not connect this pin to anything it still reads as 1. However, this can read as 0
by externally connecting it to ground and pulling it down.
An interesting state happens if PORTx is set to 0 and pins are set as Input. In this case, pin
enters into a tri-state mode and internal pull-up resistor is disabled. This makes the pin
enter into a state of high impedance. Even if the pin is not connected, it catches noise from
surrounding objects and read result from this pin is unpredictable. It is always a good
practice enable the internal pull-up resistor while reading. This tri-state is used for receiving
analog inputs, especially when using internal Analog to Digital converter.
PINx
The functionality of a PINx register is straightforward; it reads data from the Port Pin. If
DDRx is set to Input (0) for a particular bit, then we can read data from PINx. However if
DDRx is set to Output (1), then reading PINx register gives the same data which has been
output on that particular Pin.
Some newer AVRs have an additional feature where it can write to PINx register to toggle
the value of PORTx, irrespective of Data Direction Register.
We can summarize register functionalities in a simple table format.
DDRx.n
PORTx.n
Status
These concepts start being more clear once we get into practical applications.