AVR Microcontroller
and Assembly
LanguageLecture# 02
Microprocessor System and Interfacing
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
1
Lecture Objectives
About AVR Microcontroller
Microcontroller Architecture
Assembly Language
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
2
Harvard and Von Neumann Architectures
A Von Neumann architecture has only one bus which is used
for both data transfers and instruction fetches
Harvard architecture has separate data and instruction
busses, allowing transfers to be performed simultaneously on
both busses.
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
3
Microprocessors
A microprocessor incorporates the functions of
a computer's central processing unit (CPU) on a
single integrated circuit (IC), or at most a few integrated
circuits.
It is a multipurpose, programmable device that
accepts digital data as input, processes it according to
instructions stored in its memory, and provides results as
output.
It is an example of sequential digital logic, as it has internal
memory. Microprocessors operate on numbers and symbols
represented in the binary numerical system.
Examples Intel x86(286,386,486), Pentium series, Motorola
Power PC, Advance RISC Machine (ARM) Processor .
Microprocessor might only include an arithmetic logic
unit (ALU) and a control logic section.
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
4
Microprocessors
The ALU performs Arithmetic operations addition, subtraction,
and logical operations AND or OR.
Each operation of the ALU sets one or more flags in a status
register, which indicate the results of the last operation (zero
value, negative number, overflow. or others).
Control Unit retrieves instruction operation codes from
memory, and initiates whatever sequence of operations of the
ALU required to carry out the instruction.
A single operation code might affect many individual data
paths, registers, and other elements of the processor.
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
5
Microprocessor
A multi-core processor is simply a single chip containing
more than one processing cores.
This effectively multiplies the processor's potential
performance by the number of cores.
Some components, such as bus interface and cache,
may be shared between cores.
The cores are physically very close to each other, they
can communicate with each other much faster than
separate processors in a multiprocessor system, which
improves overall system performance.
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
6
Choosing a Microcontroller
Microcontrollers are generally 8-bit.
Each controller has a unique instruction set and
register set and are not compatible with one
another.
Criteria to choose microcontroller include:
Meeting needs efficiently and cost effectively
Speed, Packaging, Power Consumption, RAM and ROM,
IO pins and timer, Cost per unit.
Hardware and software development tools
Availability and reliability of microcontroller sources
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
9
AVR Microcontroller
8-bit single-chip microcontroller with Harvard
Architecture
On-chip program ROM
Data RAM
EEPROM
Timers
I/O ports
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
10
AVR Microcontroller
Program ROM
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
11
Used to store programs
Size can vary from 1K to 256K
AVR is the first microcontroller to use on-chip Flash
memory
AVR Microcontroller
Data RAM and EEPROM
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
12
Used for data storage
It has general purpose registers, I/O memory and
internal SRAM
32 general purpose registers
Size of SRAM and I/O memory size varies
EEPROM stores critical data which does not need to
be changed
AVR Microcontroller
I/O Pins
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
13
Normally 3 to 86 pins for I/O purpose
Depends on package
The package size varies from 8 to 100 pins
AVR Microcontroller
Peripherals
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
14
ADC
Timers
USART
I2C
SPI
AVR Architecture and Programming
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
15
General Purpose Registers
AVR Data Memory
Instruction with Data Memory
AVR Status Register
AVR Data Format and Directives
AVR Assembly Programming
Assembling an AVR Program
Program Counter and Program ROM space
AVR Architecture and Programming
General Purpose Registers (GPRs)
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
16
Many registers to store data
Store information temporarily which is byte of data or address
pointing to data
In AVR there are 32 GPRs
Located at lowest of memory locations (address from 0x0000
to 0x001F
R0D7 D6 D5 D4 D3 D2 D1 D0
R1D7 D6 D5 D4 D3 D2 D1 D0
R2D7 D6 D5 D4 D3 D2 D1 D0
R31D7 D6 D5 D4 D3 D2 D1 D0
AVR Architecture and Programming
GPRs – LDI Instruction
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
17
Copies 8-bit data into general purpose registers
LDI Rd, K
K is an 8-bit value, Rd is any of the R16 to R31 registers
LDI loads right operand in left operand
I stands for “immediate”, provide data immediately
Remember that
To present a number in hex either use $ or 0x before anynumber. Otherwise it will be treated as a decimal.
If values from 0 to F are moved then rest of the bits will betreated as zeros.
If a larger value greater than 255 is moved then it will causean error
AVR Architecture and Programming
GPRs – LDI Instruction – Example
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
18
LDI R20, 0x20 ; load R20 with 0x20
LDI R31, 0x87 ; load R31 with 0x87
LDI R25, 0x79 ; load R25 with 0x79
LDI R25, $23 ; What is the value of R25?
LDI R31, 86 ; What is the value of R31?
AVR Architecture and Programming
GPRs – ADD Instruction
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
19
Adds the value of one register to the other
ADD Rd, Rr
Adds Rr with Rd and stores value in Rd
AVR Architecture and Programming
GPRs – ADD Instruction - Example
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
20
Add two numbers 0x25 and 0x34
LDI R16, 0x25
LDI R17, 0x34
ADD R16, R17
What is the value of R16?
What is the value of R17?
AVR Architecture and Programming
The AVR Data Memory
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
21
Used to store data
Divided in three parts
GPRs
I/O Memory
SRAM
AVR Architecture and Programming
The AVR Data Memory - GPRs
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
22
32 bytes of memory
Address are from $00 - $1F
AVR Architecture and Programming
The AVR Data Memory – I/O Memory
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
23
Used to control microcontroller peripherals such as
Status registers
Timers
Serial Communication
I/O Ports
ADC
Memory depends on number of pins available
At-least 64 bytes of memory is available for all AVRs
AVR Architecture and Programming
The AVR Data Memory – SRAM
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
24
Store any 8-bit wide data
Accessed by address directly
Total memory size is sum of all memories
EEPROM memory is used to store data permanently. It is not erased when power is off
Data Memory
(Bytes)
SFRs
(Bytes)
SRAM
(Bytes)
GPRs
(Bytes)
ATtiny25 224 64 128 32
ATtiny85 608 64 512 32
ATmega8 1120 64 1024 32
ATmega16 1120 64 1024 32
ATmega32 2144 64 2048 32
ATmega128 4352 64+160 4096 32
ATmega2560 8704 64+416 8192 32
AVR Architecture and Programming
Instruction with Data Memory
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
25
Instructions can be used to access different locations of data
memory
AVR Architecture and Programming
Instruction with Data Memory – STS
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
26
STore direct to data Space
STS K, Rr
K is address location between 0x0000 and 0xFFFF
Rr to take data from
To copy contents of GPRs into an address location in data
space
This location can be any part of data space available
Example
STS 0x230, R25
What will happen when this instruction is executed?
AVR Architecture and Programming
Instruction with Data Memory – STS – Class Exercise
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
27
LDI R20, 0x99
STS 0x200, R20
STS 0x201, R20
STS 0x202, R20
STS 0x203, R20
What will be the result of these instructions ? Anyone who can fill
this table ?
Address Data
0x200
0x201
0x202
0x203
AVR Architecture and Programming
Instruction with Data Memory - LDS
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
28
Load Direct from data Space
LDS Rd, K
Rd data location to be stored
K is address between 0x0000 and 0xFFFF
Instruction tells CPU to copy one byte of data from address
location
This location can be any part of data space available
Example
LDS R5, 0x200
What will be the result of this instruction ?
AVR Architecture and Programming
Instruction with Data Memory – LDS – Class Exercise
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
29
Load two numbers in two GPRs
Store these numbers at locations 0x300 and 0x302
Add two numbers located at 0x300 and 0x302
R0 R1 Addr $300 Addr $302
Before LDS R0, 0x300 ? ? 12 13
After LDS R0, 0x300 12 ? 12 13
After LDS R1, 0x302 12 13 12 13
ADD R1, R0 25 13 12 13
AVR Architecture and Programming
Instruction with Data Memory – STS – Class Exercise
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
30
Put 0x45 to Ports B, C. The addresses of port B, C and D is 0x38,
0x35, 0x32
Home Task # 1
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
31
Load R16 with value from address 55H and R20
with value 0x23. Add these values and send to
Port D with address 0x32.
Home Assignment Task
Complete all “Review Questions” in the book related
to the topics covered for AVR Architecture and
Assembly Language
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
32
Assignments
Write Lectures in your own words
Include any example and exercise performed during
lecture
Include couple of more exercises/questions related to
the topics in class. (Be creative – You will get bonus
Marks for it)
Send notes of both lectures by Saturday Night
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
33