0% found this document useful (0 votes)
105 views120 pages

Unit3 AVR Microcontroller Introduction

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 120

Microprocessors And

Microcontroller

By: Dr. Ujjaval Patel


Semester : 6th Information & Communication Technology,
Subject Code: 3163207 Adani Institute of Infrastructure Engg.
2/8/2022
AVR Microcontroller

By: Dr. Ujjaval Patel


Semester : 6th Information & Communication Technology,
Subject Code: 3163207 Adani Institute of Infrastructure Engg.
2/8/2022
AVR Microcontroller

➢ Difference between MP & MC


➢ Architectures
➢ Criteria for choosing microcontrollers
➢ AVR Family
➢ Block diagram

By: Dr. Ujjaval Patel


Semester : 6th Information & Communication Technology,
Subject Code: 3163207 Adani Institute of Infrastructure Engg.
2/8/2022
AVR Microcontroller

➢ Difference between MP & MC


➢ Architectures
➢ Criteria for choosing microcontrollers
➢ AVR Family
➢ Block diagram

By: Dr. Ujjaval Patel


Semester : 6th Information & Communication Technology,
Subject Code: 3163207 Adani Institute of Infrastructure Engg.
2/8/2022
Difference; Microprocessor & Microcntroller
Data BUS

General Serial
IO
Purpose RAM ROM Timer COM
Port Port
Micro
processor Address BUS

Control BUS
General Purpose Microprocessors

CPU RAM ROM

Serial
Port
Timer I/O Microcontrollers
2/8/2022
Difference; Microprocessor & Microcntroller

Difference

Hardware
Applications Instruction set
Architecture

2/8/2022
Hardware Architecture

Microcontroller Microprocessor
◉ On chip circuitry of CPU, limited ◉ Microprocessors do not have
amount of RAM & ROM,I/O such devices on chip. Designer
ports, timers, ADC, built in have to connect them externally
interrupt system etc. for microprocessor based
◉ Small in size, occupy less embedded system.
space, cheap in price. ◉ Bulkier and more expensive.
◉ Microcontrollers usually must ◉ 10 - 50 W for general purpose
have low-power requirements desktop CPUs.
0.5 - 1 W
2/8/2022
Applications

Microcontroller Microprocessor

◉ Microcontrollers are found in ◉ Microprocessors are suited to


small, minimum-component processing information in
designs performing control computer systems
oriented activities.
◉ Microprocessors are which
◉ microcontrollers are used to designed to be very general to
perform specialized single perform any number of tasks
tasks. (programs)
2/8/2022
Instruction set

Microcontroller Microprocessor
◉ Microcontrollers have ◉ Microprocessor instruction sets
instructions to set and clear are processing intensive implying
individual bits and perform they have powerful addressing
other bit oriented operations. modes with instructions for
operations on large volume of
◉ The instructions must be data.
extremely compact with the ◉ Instructions operate on nibbles,
majority implemented in a words or even double words.
single byte.
2/8/2022
An Embedded system
Uses a microprocessor (or
microcontroller) to do one and only
one task.

2/8/2022
Technology for Embedded System

Embedded systems are controlled by one or more


main processing cores that are typically either
❖ Microcontrollers
❖ Microprocessors
❖ Digital Signal Processors (DSP)
❖ FPGA’s

2/8/2022
2/8/2022

A short list of Embedded Systems


Anti-lock brakes
Modems
Auto-focus cameras MPEG decoders
Automatic teller machines Network cards
Automatic toll systems Network switches/routers
Automatic transmission On-board navigation
Avionic systems
Battery chargers
Pagers
Camcorders Photocopiers
Cell phones Point-of-sale systems
Cell-phone base stations Portable video games
Cordless phones Printers
Cruise control
Video game consoles
Satellite phones
Digital cameras Scanners
Disk drives Smart ovens/dishwashers
Electronic card readers Speech recognizers
Electronic instruments Stereo systems
Electronic toys/games
Factory control
Teleconferencing systems
Fax machines Televisions
Fingerprint identifiers Temperature controllers
Home security systems Theft tracking systems
Life-support systems TV set-top boxes
Washers and dryers
VCR’s, DVD players
AVR Microcontroller

➢ Difference between MP & MC


➢ Architectures
➢ Criteria for choosing microcontrollers
➢ AVR Family
➢ Block diagram

By: Dr. Ujjaval Patel


Semester : 6th Information & Communication Technology,
Subject Code: 3163207 Adani Institute of Infrastructure Engg.
2/8/2022
Architectures of Computers

CISC RISC
◉ Complex Instruction Set Computer ◉ Reduced Instruction Set Computer
◉ Emphasis on hardware ◉ Emphasis on software
◉ Includes multi- clock complex ◉ Single-clock, reduced instructions
instructions only
◉ Memory to memory: “LOAD” and ◉ Register to register: “LOAD” and
“STORE” incorporated in instructions “STORE are independent instructions
◉ Small code sizes, high cycles per second ◉ Largecodesizes, lesscyclespersecond

2/8/2022
Architectures of Computers

2/8/2022
Most common 8 bit Microcontrollers

◉ INTEL’ s 8051
◉ Free Scale Semiconductor’s (Motorola) 68HC08/68HC11
◉ Atmel’s AVR
◉ Zilog’s Z8
◉ Microchip Technology PIC 16X
◉ Phillips/ Signatics

2/8/2022
Introduction to AVR
www. Micro Digital Ed. com

Chapter 1 BIHE university

The AVR microcontroller


and embedded
systems
using assembly and c

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
AVR Microcontroller

➢ Difference between MP & MC


➢ Architectures
➢ Criteria for choosing microcontrollers
➢ AVR Family
➢ Block diagram

By: Dr. Ujjaval Patel


Semester : 6th Information & Communication Technology,
Subject Code: 3163207 Adani Institute of Infrastructure Engg.
2/8/2022
2/8/2022

Criteria for choosing a


Microcontroller
Must meet the task efficiently and cost effectively.
The analysis may include:
➢ Number of I/O s
➢ Amount of RAM and ROM on chip
➢ Number of Timers
➢ Speed
➢ Packaging
➢ Power consumption
➢ How easy is it to upgrade to higher performance and/or lower power
consumption versions.
➢ Cost per unit
➢ Availability: Software & Hardware with appropriate expertise
AVR family Overview

➢ Classic AVR: AT90S2313, AT90S4433

➢ Mega AVR: ATmega8, ATmega32, ATmega128

➢ Tiny AVR: ATtiny13, ATtiny25

➢ Special Purpose AVR: AT90PWM216,AT90USB1287

2/8/2022
AVR Microcontroller

➢ Difference between MP & MC


➢ Architectures
➢ Criteria for choosing microcontrollers
➢ AVR Family
➢ Block diagram

By: Dr. Ujjaval Patel


Semester : 6th Information & Communication Technology,
Subject Code: 3163207 Adani Institute of Infrastructure Engg.
2/8/2022
Classic AVR (AT90Sxxxx)

2/8/2022
mega AVR (ATmegaxxxx)

2/8/2022
Tiny AVR (ATtinyxxxx)

2/8/2022
Special Purpose AVR

2/8/2022
AVR different groups
www. Micro Digital Ed. com
BIHE university

• Classic AVR
– e.g. AT90S2313, AT90S4433
• Mega
– e.g. ATmega8, ATmega32, ATmega128
• Tiny
– e.g. ATtiny13, ATtiny25
• Special Purpose AVR
– e.g. AT90PWM216,AT90USB1287

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Let’s get familiar with the AVR part numbers
www. Micro Digital Ed. com
BIHE university

ATmega128

Atmel group
Flash =128K

ATtiny44 AT90S4433

Atmel
Tiny Flash =4K Atmel Classi
Flash =4K
group c
group

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Comparison of different
microcontrollers: 8 bits

2/8/2022
AVR IC Packages

2/8/2022
AVR On Chip ROM Size & Address Space

2/8/2022
AVR Microcontroller

➢ Difference between MP & MC


➢ Architectures
➢ Criteria for choosing microcontrollers
➢ AVR Family
➢ Block diagram

By: Dr. Ujjaval Patel


Semester : 6th Information & Communication Technology,
Subject Code: 3163207 Adani Institute of Infrastructure Engg.
2/8/2022
AVR’s CPU: Block Diagram

RAM EEPROM Timers


General
Purpose
P R O G R AM Registers
ROM
address bus
Program CPU
Bus data bus
control bus

Interrupt Other
OSC Unit Ports P eripherals

I/O
P IN S

2/8/2022
Data RAM space
8 bit
Data Address
R0
Space R1
R2
$0000
$0001 General

...
Purpose

...
Registers R31
$001F I/O Address
TWBR $00
$0020
TWSR $01
Standard I/O

...
...
Registers
SPH $3E
$005F SREG $3F
$0060
General
purpose
...

RAM
(SRAM)

$FFFF
2/8/2022
Data Memory with Extended I /O

2/8/2022
Data Memory with Extended I /O

2/8/2022
Data Memory with Extended I /O

2/8/2022
Data Memory with Extended I /O

2/8/2022
Data Memory with Extended I /O

2/8/2022
AVR CPU and Memory space
8 bit
Data Address
R0
Space R1
R2
$0000
$0001 General

...
Purpose

...
Registers R31
$001F I/O Address
TWBR $00
$0020
TWSR $01
Standard I/O

...
...
Registers
SPH $3E
$005F SREG $3F
$0060
General
purpose
...

RAM
(SRAM)

$FFFF
2/8/2022
Data Memory size

2/8/2022
Some simple instructions
1. Loading values into the general purpose registers
LDI (Load Immediate)
◼ LDI Rd, k R0
◼ Its equivalent in high level languages: R1
R2
Rd = k


where, Rd can be R16 to R31 Only
R15
k = data R16

◼ Example: R17


◼ LDI R16,53
◼ R16 = 53 R30
R31
◼ LDI R19,132 registers

◼ LDI R23,0x27
◼ R23 = 0x27
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
R0 thru R15
◼ Only registers in the range R16 to R31 can be loaded immediate.
◼ We cannot load a constant into the registers R0 to R15 directly.
◼ It would have to be loaded into a valid register first then copied.
◼ To load the value of 10 into register zero (R0):

◼ Code:

LDI R16,10 ;Set R16 to value of 10


MOV R0,R16 ;Copy contents of R16 to R0

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Some simple instructions
2. Arithmetic calculation
◼ There are some instructions for doing Arithmetic
and logic operations; such as:
ADD, SUB, MUL, AND, etc. R0
R1
R2


R15
R16
R17


R30
R31

registers

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Some simple instructions
2. Arithmetic calculation

◼ There are some instructions for doing Arithmetic


and logic operations; such as:
ADD, SUB, MUL, AND, etc. R0
R1

◼ ADD Rd,Rs R2


◼ Rd = Rd + Rs
Example:
R15

R16

◼ ADD R25, R9 R17


◼ R25 = R25 + R9
◼ ADD R17,R30 R30
R31
◼ R17 = R17 + R30 registers

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
A simple program
◼ Write a program that calculates 19 + 95
LDI R16, 19 ;R16 = 19
LDI R20, 95 ;R20 = 95
ADD R16, R20 ;R16 = R16 + R20

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
A simple program
◼ Write a program that calculates 19 + 95 + 5
LDI R16, 19 ;R16 = 19
LDI R20, 95 ;R20 = 95
LDI R21, 5 ;R21 = 5
ADD R16, R20 ;R16 = R16 + R20
ADD R16, R21 ;R16 = R16 + R21

LDI R16, 19 ;R16 = 19


LDI R20, 95 ;R20 = 95
ADD R16, R20 ;R16 = R16 + R20
LDI R20, 5 ;R20 = 5
ADD R16, R20 ;R16 = R16 + R20

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Some simple instructions
2. Arithmetic calculation

◼ ADC Rd,Rs
R0
◼ Rd = Rd + Rs R1

Used for 16-bit addition


R2


R15
R16
R17


R30
R31

registers

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Some simple instructions
2. Arithmetic calculation

◼ SUB Rd,Rs
R0
◼ Rd = Rd - Rs R1
R2


R15
R16
R17


R30
R31

registers

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Some simple instructions
3. Logical operations

◼ AND Rd,Rs
R0
◼ Rd = Rd & Rs R1
R2


R15
R16
R17


R30
R31

registers

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Some simple instructions
3. Logical operations

◼ OR Rd,Rs
R0
◼ Rd = Rd + Rs R1
R2


R15
R16
R17


R30
R31

registers

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Some simple instructions
3. Logical operations

◼ EOR Rd,Rs
R0
◼ Rd = Rd ɸ Rs R1
R2


R15
R16
R17


R30
R31

registers

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Some simple instructions
2. Arithmetic calculation

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Some simple instructions
2. Arithmetic calculation
◼ INC Rd
◼ Rd = Rd + 1
◼ Example:
◼ INC R25
◼ R25 = R25 + 1

◼ DEC Rd
◼ Rd = Rd - 1
◼ Example:
◼ DEC R23
◼ R23 = R23 - 1
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Some simple instructions
2. Arithmetic calculation
◼ COM Rd
◼ Complements content of Rd
◼ Example:
◼ Before execution: R20 = 0101 0101 = 0x55
◼ COM R20
◼ After execution: R20 = 1010 1010 = 0xAA
◼ Example:
◼ LDI R16,0x55
◼ OUT PORTB,R16
◼ COM R16
◼ OUT PORTB,R16
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Arithmetic instruction: Operates on Single byte of data
Example: Write a program to toggle all bits of PORTB continuously.

Solution:
LDI R20, 0x55 ;R20 = 55
REPEAT: OUT PORTB,R20
COM R20
JMP REPEAT

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Arithmetic instruction: Operates on Single byte of data
◼ CLR Rd
◼ Clear content of Rd

◼ NEG Rd
◼ Takes 2’s complement of Rd

◼ SWAP Rd
◼ Swap nibbles in Rd

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Arithmetic instruction: Operates on Single byte of data
◼ ROR Rd
◼ Rotate right Rd through carry
◼ ROL Rd
◼ Rotate Left Rd through carry
◼ LSL Rd
◼ Logical shift Left Rd
◼ LSR Rd
◼ Logical shift right Rd

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Data Transfer Instructions & Data Address Space

General RAM EEPROM Timers


Purpose
PROGRAM
Registers
ROM

Program CPU Data


Bus Bus address bus
data bus
control bus
Data
8 bit Bus
Data Address
Space R0
R1
R2
$0000 Interrupt Other
$0001 General OSC Ports
Unit Peripherals

...
Purpose
...

Registers R31
$001F I/O Address I/O
TWBR $00 PINS
$0020
TWSR $01
Standard I/O
LDS (Load direct from data space)
...
...

Registers
SPH $3E
$005F SREG $3F
$0060 LDS Rd, addr ;Rd = [addr]
General
purpose
...

RAM
(SRAM) Example:
LDS R1, 0x60
$FFFF

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Data Address Space
LDS (Load direct from data space)

LDS Rd, addr ;Rd = [addr]

Rd → 0 to 31
Addr → 0x0000 to 0xFFFF
Example: WAP to add content of memory locations 0x300 & 0x302 and store result in
register R1
LDS R1, 0x60

LDS R0,0x300
LDS R1,0x302
ADD R1,R0

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Data Address Space
Example: What does the following instruction do?
LDS R20,2
8 bit
Data Address
Answer: Space
R0
R1
R2
It copies the contents of R2 into R20; as 2 is the address of R2. $0000
$0001 General

...
Purpose
Registers R31
$001F I/O Address
TWBR $00
$0020
TWSR $01
Standard I/O

...
Registers
SPH $3E
$005F SREG $3F
$0060
General
purpose

...
RAM
(SRAM)

$FFFF

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Data Address Space

STS (Store direct to data space)

STS addr,Rd ;[addr]=Rd

Example:
STS 0x60,R15 ; [0x60] = R15

Note:
➢ You cannot store an immediate data directly into SRAM location in the AVR.
➢ This must be done via the GPRs.

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Data Address Space
Example: Write a program that stores 55 into location 0x80 of RAM.

Solution:
LDI R20, 55 ;R20 = 55
STS 0x80, R20 ;[0x80] = R20 = 55

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Data Address Space
Example: Write a program that copies the contents of location 0x80
of RAM into location 0x81.

Solution:
LDS R20, 0x80 ;R20 = [0x80]
STS 0x81, R20 ;[0x81] = R20 = [0x80]

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Data Address Space

Example: Write a program that stores 55 into location 0x80 of RAM.

Solution:
LDI R20, 55 ;R20 = 55
STS 0x80, R20 ;[0x80] = R20 = 55

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Data Address Space

Example: Write a program that copies the contents of location 0x80


of RAM into location 0x81.

Solution:
LDS R20, 0x80 ;R20 = [0x80]
STS 0x81, R20 ;[0x81] = R20 = [0x80]

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Data Address Space

Example: Add contents of location 0x90 to contents of location 0x95


and store the result in location 0x313.
Solution:
LDS R20, 0x90 ;R20 = [0x90]
LDS R21, 0x95 ;R21 = [0x95]
ADD R20, R21 ;R20 = R20 + R21
STS 0x313, R20 ;[0x313] = R20

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Data Address Space

Example: Store 0x53 into the SPH register. The address of SPH is 0x5E

Solution:
LDI R20, 0x53 ;R20 = 0x53
STS 0x5E, R20 ;SPH = R20

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Data Address Space (Standard I/O Space)
8 bit
Data Address
Space R0
R1
$0000 R2 IN (IN from IO location)
$0001 General

...
Purpose
IN Rd,IOaddress ;Rd = [addr]
...

Registers R31
$001F I/O Address
TWBR $00
$0020
TWSR $01
Standard I/O
Example:
...
...

Registers IN R1, 0x3F ;R1 = SREG


SPH $3E
$005F SREG $3F
$0060 IN R17,0x3E ;R17 = SPH
General
purpose
...

RAM
(SRAM)

$FFFF

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Data Address Space (Standard I/O Space)
8 bit
Data Address OUT (OUT to IO location)
Space R0
R1
$0000 R2
$0001 General OUT IOAddr,Rd ;[addr]=Rd

...
Purpose
...

Registers R31
$001F I/O Address Example:
TWBR $00
$0020
TWSR $01
Standard I/O
OUT 0x3F,R12 ;SREG = R12
...
...

Registers OUT 0x3E,R15 ;SPH = R15


SPH $3E
$005F SREG $3F
$0060
General
purpose
...

Using Names of IO registers


RAM
(SRAM)
Example:
OUT SPH,R12 ;OUT 0x3E,R12
IN R15,SREG ;IN R15,0x3F
$FFFF

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Introduction to AVR
Chapter 1

The AVR microcontroller


and embedded
systems
using assembly and c

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Data Address Space (Standard I/O Space)
8 bit
Data Address
Space R0
R1
$0000 R2
$0001 General

...
Purpose
...

Registers R31
$001F I/O Address
TWBR $00
$0020
TWSR $01
Standard I/O
...
...

Registers
SPH $3E
$005F SREG $3F
$0060
General
purpose
...

RAM
(SRAM)

$FFFF

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Data Address Space (Standard I/O Space)

Example: Write a program that adds the contents of the PINC IO


register to the contents of PIND and stores the result in location
0x90 of the SRAM

Solution:
IN R20,PINC ;R20 = PINC
IN R21,PIND ;R21 = PIND
ADD R20,R21 ;R20 = R20 + R21
STS 0x90,R20 ;[0x90] = R20

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Data Address Space (Standard I/O Space)
Example: Write a program that adds the contents of the PINC IO register
to the contents of PIND and stores the result in location 0x90 of the
SRAM

Solution:
IN R20,PINC ;R20 = PINC
IN R21,PIND ;R21 = PIND
ADD R20,R21 ;R20 = R20 + R21
STS 0x90,R20 ;[0x90] = R20

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Data memory space
◼ MOV Instruction
◼ MOV Rd,Rs
◼ Copy content of Rs to Rd.
◼ Rd & Rs → R0 to R31

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Status Register (SREG)
I T H
SREG: S V N Z C
Carry
Interrupt Zero
oVerflow
Data Address
Sign Space
Temporary
N+V
$0000
Negative $0001 General
Purpose

...
Registers
$001F IO Ad
TWBR $00
Half Carry $0020
TWSR $01
Standard IO

...
...
Registers
SPH $3E
$005F SREG $3F
$0060
General
purpose

...
RAM
(SRAM)

$FFFF

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Status Register (SREG)

Example: Show the status of the C, H, and Z flags after the


subtraction of 0x23 from 0xA5 in the following instructions:
LDI R20, 0xA5
LDI R21, 0x23
SUB R20, R21 ;subtract R21 from R20

Solution:
$A5 1010 0101
- $23 0010 0011
$82 1000 0010 R20 = $82
C = 0 because R21 is not bigger than R20 and there is no borrow from D8 bit.
Z = 0 because the R20 has a value other than 0 after the subtraction.
H = 0 because there is no borrow from D4 to D3.

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Status Register (SREG)

Example: Show the status of the C, H, and Z flags after the addition
of 0x9C and 0x64 in the following instructions:
LDI R20, 0x9C
LDI R21, 0x64
ADD R20, R21 ;add R21 to R20

Solution: 1
$9C 1001 1100
+ $64 0110 0100
$100 1 0000 0000 R20 = 00
C = 1 because there is a carry beyond the D7 bit.
H = 1 because there is a carry from the D3 to the D4 bit.
Z = 1 because the R20 (the result) has a value 0 in it after the addition.

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Status Register (SREG)
Example: Show the status of the C, H, and Z flags after the addition
of 0x38 and 0x2F in the following instructions:
LDI R16, 0x38 ;R16 = 0x38
LDI R17, 0x2F ;R17 = 0x2F
ADD R16, R17 ;add R17 to R16

Solution: 1
$38 0011 1000
+ $2F 0010 1111
$67 0110 0111 R16 = 0x67
C = 0 because there is no carry beyond the D7 bit.
H = 1 because there is a carry from the D3 to the D4 bit.
Z = 0 because the R16 (the result) has a value other than 0 after the addition.

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Status Register (SREG)
Example: Show the status of the C, H, and Z flags after the
subtraction of 0x73 from 0x52 in the following instructions:
LDI R20, 0x52
LDI R21, 0x73
SUB R20, R21 ;subtract R21 from R20

Solution:
$52 0101 0010
- $73 0111 0011
$DF 1101 1111 R20 = $DF
C = 1 because R21 is bigger than R20 and there is a borrow from D8 bit.
Z = 0 because the R20 has a value other than zero after the subtraction.
H = 1 because there is a borrow from D4 to D3.

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Status Register (SREG)

Example: Show the status of the C, H, and Z flags after the


subtraction of 0x9C from 0x9C in the following instructions:
LDI R20, 0x9C
LDI R21, 0x9C
SUB R20, R21 ;subtract R21 from R20
Solution:
$9C 1001 1100
- $9C 1001 1100
$00 0000 0000 R20 = $00
C = 0 because R21 is not bigger than R20 and there is no borrow from D8 bit.
Z = 1 because the R20 is zero after the subtraction.
H = 0 because there is no borrow from D4 to D3.

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Example 2-1

Note: do not forget to add it at the beginning of the program:


.include "M32DEF.inc"
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Example 2- 2

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Example 2- 3

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Example 2- 4

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Problems (1)

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Problems (2)

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Problems (3)

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Problems (4)

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Introduction to AVR
Chapter 1

The AVR microcontroller


and embedded
systems
using assembly and c

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
AVR Data Type
➢ AVR has only 1 data type, i.e. 8-bit.
➢ All registers that manipulates the data are of 8-bit.
➢ Data can be specified in Hex, Binary, Decimal or ASCII.
➢Hex: ➢Binary:

➢Decimal: ➢ASCII:

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
Assembler directives
➢ Assembler directives gives directions to the CPU.
➢ It makes our program easy to understand and legible.
.EQU (equate):

➢It is used for fixed data assignments

➢All peripheral registers are initialized in M32DEF.INC file using this directive.

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
Assembler directives
.SET:

➢It is same as .EQU directive.


➢The value assigned using .SET directive can be reassigned later in program

.ORG (originate):
➢It is used to indicate beginning of the address.

.INCLUDE (originate):
➢Tells compiler to include content of file in the beginning of program.

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
ATMega32 Programmer Model: Data Memory
EEPROM
➢ ATmega32 contains 1024 bytes of data EEPROM memory.
➢ It is organized as a separate data space, in which single bytes
can be read and written.
➢ The EEPROM has an endurance of at least 100,000 write/erase
cycles.
➢ Different chip have different size of EEPROM memory
Chip Bytes Chip Bytes Chip Bytes

ATmega8 512 ATmega16 512 ATmega32 1024


ATmega64 2048 ATmega128 4096 ATmega256RZ 4096

ATmega640 4096 ATmega1280 4096 ATmega2560 4096


AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
ATMega32 Programmer Model: Program Memory
Flash Memory Layout

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
ATMega32 Programmer Model: Program Memory
Flash Memory Layout
➢ What will be address range in ATMega32 ?

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
ATMega32 Programmer Model: Program Memory
Flash Memory Layout
➢ Find address range in
(i) ATtiny25
(ii) ATmega16
(iii) ATmega32

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
ATMega32 Programmer Model: Program Memory
Flash Memory Layout
➢ Find address range in
(i) ATtiny25
(ii) ATmega16
(iii) ATmega32

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
ATMega32 Programmer Model: Program Memory
Flash Memory Layout

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Introduction to AVR
Chapter 1

The AVR microcontroller


and embedded
systems
using assembly and c

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
How instructions are stored in Program Memory ?

➢LDI R16,$25
➢AVR Microcontroller supports little endian convention.

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
ATMega32 Pin out & Descriptions

Clears all the


registers and
restart the
execution of
program

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
ATMega32 Pin out & Descriptions

Provides supply
voltage to the
chip. It should be
connected to +5

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
ATMega32 Pin out & Descriptions

These pins are


used to connect
external crystal
or RC oscillator

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
ATMega32 Pin out & Descriptions

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
ATMega32 Pin out & Descriptions

Reference voltage
for ADC

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
ATMega32 Pin out & Descriptions

Reference voltage
for ADC

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
ATMega32 Pin out & Descriptions

Port B Port A

Supply voltage for


ADC and portA.
Connect it to VCC

Port C
Port D

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
ATMega32 Pin out & Descriptions

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
ATMega32 Pin out & Descriptions

Port B Port A

Port C
Port D

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
ATMega32 Pin out & Descriptions

Port
Clears B
all the Port A
registers supply
Provides and
restart the
voltage to the Reference voltage
These
chip. pins
execution are
of be
It should for ADC
Supply voltage
used to connect
programto +5
connected for ADC and
external crystal
portA. Connect it
or RC oscillator
to VCC

Port C
Port D

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
ATMega32 Pin out & Descriptions
➢ Digital IO is the most fundamental mode of connecting a MCU to external world. The
interface is done using what is called a PORT.
➢ A port is the point where internal data from MCU chip comes out or external
data goes in. They are present is form of PINs of the IC.
➢ Most of the PINs are dedicated to this function and other pins are used for power
supply, clock source etc .
➢ ATMega32 ports are named PORTA, PORTB, PORTC, and PORTD.

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
ATMega32 Pin out & Descriptions

Mega32/Mega16
(XCK/T0) PB0 PA0 (ADC0)
(T1) PB1 PA1 (ADC1)
(INT2/AIN0) PB2 PA2 (ADC2)
(OC0/AIN1) PB3 PA3 (ADC3)
(SS) PB4 PA4 (ADC4)
(MOSI) PB5 PA5 (ADC5)
(MISO) PB6 PA6 (ADC6)
(SCK) PB7 PA7 (ADC7)
PINA

PORTB
DDRB
PINB
RESET DDRA AREF
PORTA
VCC AGND
PORTC
GND DDRC AVCC
PINC
XTAL2 PC7 (TOSC2)
XTAL1 PC6 (TOSC1)
(RXD) PD0 PC5 (TDI)
(TXD) PD1 PC4 (TDO)
(INT0) PD2 PC3 (TMS)
(INT1) PD3 PC2 (TCK)
(OC1B) PD4 PC1 (SDA)
(OC1A) PD5 PC0 (SCL)
(ICP) PD6 PD7 (OC2)

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
ATMega32 Pin out & Descriptions

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Introduction to AVR
Chapter 1

The AVR microcontroller


and embedded
systems
using assembly and c

AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
AVR Microcontroller and Embedded System Using Assembly and C
Mazidi, Naimi, and Naimi
© 2011 Pearson Higher Education,
Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
AVR Microcontroller and Embedded System Using Assembly and C
Mazidi, Naimi, and Naimi
© 2011 Pearson Higher Education,
Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
AVR Microcontroller and Embedded System Using Assembly and C
Mazidi, Naimi, and Naimi
© 2011 Pearson Higher Education,
Upper Saddle River, NJ 07458. • All Rights Reserved.
2/8/2022
AVR Microcontroller and Embedded System Using Assembly and C
Mazidi, Naimi, and Naimi
© 2011 Pearson Higher Education,
Upper Saddle River, NJ 07458. • All Rights Reserved.

You might also like