Module 1 - AVR v3 2022
Module 1 - AVR v3 2022
Microcontroller and
Interfacing
Compiled by
Prof. Hiren J. Patel
1
13/Jul/23
• Data Bus
• Address Bus
• Control Bus
• Memory
– RAM
– ROM
• Peripherals
2
13/Jul/23
Microprocessor Vs Microcontroller
3
13/Jul/23
Embedded Systems
Examples of
Applications/Embedded Systems
4
13/Jul/23
PC is an Embedded Systems???
5
13/Jul/23
Embedded Processors
• Although microcontrollers are the preferred choice for many embedded systems,
sometimes a microcontroller is inadequate for the task.
• For this reason, in recent years many manufacturers of general-purpose
microprocessors such as Intel, Freescale Semiconductor (formerly Motorola). and
AMD (Advanced Micro Devices, Inc.) have targeted their microprocessors for the
high end of the embedded market.
• Intel and AMD push their x86 processors for both the embedded and desktop PC
markets.
• In the early 1990s, Apple computer began using the PowerPC microprocessors (604,
603, 620, etc.) in place of the 680x0 for the Macintosh. In 2007 Apple switched to the
x86 CPU for use in the Mac computers.
• The PowerPC microprocessor is a joint venture between IBM and Freescale, and is
targeted for the high end of the embedded market.
• It must be noted that when a company targets a general-purpose microprocessor for
the embedded market it optimizes the processor used for embedded systems. For this
reason these processors are often called high-end embedded processors.
• Another chip widely used in the high end of the embedded system design is the ARM
(Advanced RISC Machine) microprocessor. Very often the terms embedded
processor and microcontroller are used interchangeably.
6
13/Jul/23
Any Questions???
• When comparing a system board based on a microcontroller and a
general- purpose microprocessor, which one is cheaper?
7
13/Jul/23
AVR Features
• The AVR is a modified Harvard architecture 8-bit RISC
single-chip microcontroller, which was developed by Atmel in
1996. Now it is Microchip.
• The AVR was one of the first microcontroller families to use
on-chip flash memory for program storage, as opposed to one-
time programmable ROM, EPROM, or EEPROM used by
other microcontrollers at the time.
• megaAVR chips became popular after they were designed into
the 8-bit Arduino platform.
8
13/Jul/23
ALU
PC
Instructio
n
dec.
AVR Families
• AVRs are generally classified into following:
• tinyAVR — the ATtiny series
– 0.5–16 kB program memory
– 6–32-pin package
– Limited peripheral set
• megaAVR — the ATmega series
– 4–512 kB program memory
– 28–100-pin package
– Extended instruction set (multiply instructions and instructions for handling
larger program memories); - Extensive peripheral set
• XMEGA — the ATxmega series
– 16–384 kB program memory
– 44–64–100-pin package (A4, A3, A1)
– Extended performance features, such as DMA, "Event System", and
cryptography support.
– Extensive peripheral set with ADCs
9
13/Jul/23
AVR Family(contd.)
• Application-specific AVR
– megaAVRs with special features not found on the other members of the
AVR family, such as LCD controller, USB controller, advanced PWM,
CAN, etc.
• FPSLIC (AVR with FPGA)
– FPGA 5K to 40K gates
– SRAM for the AVR program code, unlike all other AVRs
– AVR core can run at up to 50 MHz[7]
• 32-bit AVR UC3
• All AVRs are of 8-bit except 32-bit AVRS UC3
10
13/Jul/23
AVR Family
11
13/Jul/23
Features of ATmega32
• High-performance, Low-power Atmel ®AVR®
8-bit Microcontroller
• Advanced RISC Architecture
– 131 Powerful Instructions – Most Single-clock Cycle Execution
– 32 × 8 General Purpose Working Registers
– Up to 16 MIPS Throughput at 16MHz
– On-chip 2-cycle Multiplier
Features of ATmega32(Contd.)
High Endurance Non-volatile Memory segments
– 32Kbytes of In-System Self-programmable Flash program memory
– 1024Bytes EEPROM
– 2Kbytes Internal SRAM
– Write/Erase Cycles: 10,000 Flash/100,000 EEPROM
– Data retention: 20 years at 85°C/100 years at 25°C
– Optional Boot Code Section with Independent Lock Bits
In-System Programming by On-chip Boot Program
True Read-While-Write Operation
– Programming Lock for Software Security
JTAG (IEEE std. 1149.1 Compliant) Interface
– Boundary-scan Capabilities According to the JTAG Standard
– Extensive On-chip Debug Support
– Programming of Flash, EEPROM, Fuses, and Lock Bits through the JTAG
Interface
12
13/Jul/23
Features of ATmega32(Contd.)
Peripheral Features
– Two 8-bit Timer/Counters with Separate Prescalers and Compare
Modes
– One 16-bit Timer/Counter with Separate Prescaler, Compare Mode,
and Capture Mode
– Real Time Counter with Separate Oscillator (RTC)
– Four PWM Channels
– 8-channel, 10-bit ADC
8 Single-ended Channels
7 Differential Channels in TQFP Package Only
2 Differential Channels with Programmable Gain at 1x, 10x, or 200x
– Byte-oriented Two-wire Serial Interface
– Programmable Serial USART
– Master/Slave SPI Serial Interface
– Programmable Watchdog Timer with Separate On-chip Oscillator
– On-chip Analog Comparator
Features of ATmega32(Contd.)
Special Microcontroller Features
– Power-on Reset and Programmable Brown-out Detection
– Internal Calibrated RC Oscillator
– External and Internal Interrupt Sources
– Six Sleep Modes: Idle, ADC Noise Reduction, Power-save, Power-
down, Standby and Extended Standby
I/O and Packages
– 32 Programmable I/O Lines
– 40-pin PDIP, 44-lead TQFP, and 44-pad QFN/MLF
Operating Voltages
– 2.7V - 5.5V for ATmega32L
– 4.5V - 5.5V for ATmega32
Speed Grades
– 0 - 8MHz for ATmega32L
– 0 - 16MHz for ATmega32
13
13/Jul/23
14
13/Jul/23
BUS
Data
AVR CPU
Data BUS
BUS
Data BUS
Data
AVR CPU
Data BUS
15
13/Jul/23
RISC architecture
• In the early 1980s, a controversy broke out in the computer design
community
• Since the 1960s, in all mainframes and minicomputers, designers put as
many instructions as they could think of into the CPU.
– Some of these instructions performed complex tasks.
– An example is adding data memory locations and storing the sum into
memory. Naturally, microprocessor designers followed the lead of
minicomputer and mainframe designers.
– Because these microprocessors used such a large number of instructions,
many of which performed highly complex activities, they came to be
known as CISC (complex instruction set computer) processors.
• According to several studies in the 1970s, many of these complex
instructions etched into CPUs were never used by programmers and
compilers.
– The huge cost of implementing a large number of instructions
– plus the fact that a good portion of the transistors on the chip are used by the
instruction decoder,
• Designers think of simplifying and reducing the number of instructions. As
this concept developed, the resulting processors came to be known as
RISC (reduced instruction set computer).
16
13/Jul/23
The architecture
RISC vs. CISC
• CISC (Complex Instruction Set Computer)
– Put as many instruction as you can into the CPU
• RISC (Reduced Instruction Set Computer)
– Reduce the number of instructions, and use your facilities in
a more proper way.
RISC CISC
1. RISC stands for Reduced Instruction 1. CISC stands for Complex Instruction
Set Computer. Set Computer.
2. CSIC processor has complex
2. RISC processors have simple
instructions that take up multiple clocks
instructions taking about one clock
for execution. The average clock cycle
cycle. The average clock cycle per
per instruction (CPI) is in the range of 2
instruction (CPI) is 1.5
and 15.
3. Performance is optimized with more 3. Performance is optimized with more
focus on software focus on hardware.
4. It has a hard-wired unit of
4. It has a microprogramming unit.
programming.
5. The instruction set is reduced i.e. it
5. The instruction set has a variety of
has only a few instructions in the
different instructions that can be used
instruction set. Many of these
for complex operations.
instructions are very primitive.
6. CISC has many different addressing
6. The instruction set has a variety of
modes and can thus be used to
different instructions that can be used
represent higher-level programming
for complex operations.
language statements more efficiently.
17
13/Jul/23
RISC CISC
7. Complex addressing modes are 7. CISC already supports complex
synthesized using the software. addressing modes
8. Multiple register sets are present 8. Only has a single register set
9. They are normally not pipelined or
9. RISC processors are highly pipelined
less pipelined
10. Execution time is very less 10. Execution time is very high
11. Decoding of instructions is simple. 11. Decoding of instructions is complex
12. The most common RISC 12. Examples of CISC processors are
microprocessors are Alpha, ARC, ARM, the System/360, VAX, PDP-11,
AVR, MIPS, PA-RISC, PIC, Power Motorola 68000 family, AMD and Intel
Architecture, and SPARC. x86 CPUs.
13. RISC architecture is used in high-
13. CISC architecture is used in low-
end applications such as video
end applications such as security
processing, telecommunications and
systems, home automation, etc.
image processing.
What is RISC?
• Reduced Instruction Set Computer
– As compared to Complex Instruction Set Computers, i.e. x86
• Assumption: Simpler instructions execute faster
• Optimized most used instructions
• Other RISC machines: ARM, PowerPC, SPARC
• Became popular in mid 1990s
Characteristics of RISC
• Faster clock rates
• Single cycle instructions (20 MIPS @ 20 MHz)
• Better compiler optimization
• Typically no divide instruction in core
18
13/Jul/23
19
13/Jul/23
20
13/Jul/23
ALU
PC
Instructio
n
dec.
21
13/Jul/23
22
13/Jul/23
RISC architecture
LDS R20, 0x100 ; R20 = [0x100]
ADD R20, R21
ADD R20,R21 ; R20 = R20 + R21
• Feature 5 (Harvard architecture): separate buses for
LDS R20, 0x100
23
13/Jul/23
24
13/Jul/23
PC: Data
CPU Bus
Execute
Instruction dec.
Program
Bus
Interrupt Other
OSC Ports
Unit Peripherals
I/O
PINS
25
13/Jul/23
Pipelined Execution
Pipelining
• Pipelining
00 E205
01 E314
02 E321
Instruct 4 03 0F01
Instruct 3 04 0F02
Instruct 2 0516-bit
E01B
Instruct 1 06 0F01
07 9300
08 0300 RAM EEPROM Timers
Fetch 09 940C
PROGRAM
0A
Flash0009
ROM ALU
PC: Data
CPU Bus
Execute Program Instruction dec.
Bus
Interrupt Other
OSC Ports
Unit Peripherals
I/O
PINS
26
13/Jul/23
Inst.1 fetch1 R P W
Inst.2 fetch2 R P W
Inst.3 fetch3 R P W
Inst.4 fetch4 R P W
Tim
T1 T2 T3 T4 T5 e
27
13/Jul/23
Directives
Assembler Directives
.EQU and .SET
• .EQU name = value
– Example:
.EQU COUNT = 0x25
LDI R21, COUNT ;R21 = 0x25
LDI R22, COUNT + 3 ;R22 = 0x28
28
13/Jul/23
Assembler Directives
.INCLUDE
• .INCLUDE "filename.ext"
M32def.inc
.equ SREG = 0x3f
.equ SPL = 0x3d
.equ SPH = 0x3e
....
.equ INT_VECTORS_SIZE = 42 ; size in words
Program.asm
.INCLUDE "M32DEF.INC"
LDI R20, 10
OUT SPL, R20
Assembler Directives
.ORG
• .ORG address
00 E205
01 0000
Program.asm 02 0000
.ORG 0 03 0000
29
13/Jul/23
30
13/Jul/23
Assembler Assembly
EDITOR
PROGRAM assembler
myfile.asm
ASSEMBLER
PROGRAM
Machine
Language
DOWNLOAD TO DOWNLOAD TO
AVR’s EEPROM AVR ’s FLASH
31
13/Jul/23
Hex file
MEMORIES OF ATMEGA32
32
13/Jul/23
Status Register
33
13/Jul/23
For Atmega32
34
13/Jul/23
35
13/Jul/23
Stack
36
13/Jul/23
Stack
Address Code
ORG 0
000B POP R0
Memory
37
13/Jul/23
Calling a Function
940E 000A
000A 0006
0006 CALL FUNC_NAME
opCode operand 0008
00 08 INC R20
0009 L1: RJMP L1
000A FUNC_NAME:
000A ADD R20,R21
SP 000B SUBI R20,3
PC: 000C
000B
0006
0005
0004
0009
0008 000C RET
000D
Stack
Hardware
Connections
38
13/Jul/23
Reset circuit
39
13/Jul/23
Clock Source
40
13/Jul/23
41
13/Jul/23
42
13/Jul/23
43
13/Jul/23
44