IT343 Lecture 02
IT343 Lecture 02
com
BIHE university
Microcontrollers
Lecture 2
www.NicerLand.com
www.MicroDigitalEd.com
Most common microcontrollers
www. Micro Digital Ed. com
BIHE university
• 8-bit microcontrollers
(the CPU can work on only 8 bits of data at a time)
AVR, PIC, 8051
• 32-bit microcontrollers
ARM, AVR32, PIC32, CodeFire
2
AVR Family
The AVR is an 8-bit RISC single-chip
microcontroller with Harvard architecture that
comes with some standard features such as on-
chip program (code) ROM, data RAM, data EEPROM,
timers and I/O ports
Why Harvard-RISC Architecture
There are three ways available to microprocessor
designers to increase the processing power of
the CPU:
Increase the clock frequency of the chip, but it
increase heat dissipation.
Use Harvard architecture by increasing the
number of buses to bring more information (code
and data) into the CPU, but (for general purpose
microprocessors this architecture is very expensive
and unrealistic).
Change the internal architecture of the CPU and
use what is called RISC architecture, not CISC
(complex instruction set computer)
Von Neumann vs. Harvard architecture
www. Micro Digital Ed. com
BIHE university
• Harvard architecture
Code Data
Memory Memory
Data bus
CPU Address bus
Control bus
6
CISC Vs. RISC
7
AVR internal architecture
www. Micro Digital Ed. com
BIHE university
PROGRAM
ROM
Program
Bus Bus
CPU
Interrupt Other
OSC Ports
Unit Peripherals
I/O
PINS
8
Memory Types In AVR
VCC
Flash ROM
Not deleted when power is off GND VCC
Big in size 8
D0-D7
Suitable for codes, tables and fixed data n
A0-An-1
Suitable for small data that may be modified but should not
be lost when power is off
RAM
deleted when power is off
Suitable for storing the data we want to manipulate
because we have fast access to read or modify them.
AVR microcontroller peripherals
AVR’s CPU
AVR’s CPU
Arithmetic & Logic
Unit (ALU)
R0
32 General Purpose
R1
ALU
R2
registers (GPRs),
…
R0-R31
SREG: I T H S V N Z C
R15
Program Counter
CPU R16
R17
(PC) register
…
PC
R30
Instruction decoder
Instruction Decoder R31
contains Instruction
Instruction Register
registers
Register (IR)
11
THE GENERAL PURPOSE REGISTERS (GPRs)
...
Purpose Purpose
...
...
R31
$001F Registers $001F Registers
$0020 $0020
Standard I/O I/O Address Standard I/O
Registers $00 Registers
...
...
$01
(SFRs) (SFRs)
$005F $005F
...
$0060 $0060
$3E
Extended SREG $3F Extended
...
...
SRAM SRAM
$21FF
$2200
External External
SRAM SRAM
$FFFF $FFFF
ATmega640/V
ATmega328 ATmega1280/V
ATmega64 ATmega1281/V
ATmega128 ATmega2560/V
ATmega2561/V
Data Address Space
Interrupt Other
OSC Ports
Unit Peripherals
I/O
PINS
15
I/O memory (SFRs) (special function registers)
17
Internal data SRAM
Internal data SRAM is widely used for storing
data and parameters by AVR programmers and C
compilers.
PC: Data
CPU Bus
Execute
Instruction dec.
Program
Bus
Interrupt Other
OSC Ports
Unit Peripherals
I/O
PINS
22
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
23
Status Register (SREG)
SREG: I T H S V N Z C
Carry
Interrupt oVerflow Zero
Temporary Negative
Sign Data Address
Half carry N+V Space
Example:Show
Example:
Example:
Example: Showthe
Show
Show thestatus
the
the status of
status
status ofthe
of
of theC,
the
the C,H,
C,
C, H,$0000
H,
H, andZZ
and
and
and ZZ flagsafter
flags
flags
flags afterthe
after
after theaddition
the
the addition
$0001 General
subtraction
subtraction
of
of 0x9C of
of 0x9C
0x23
0x73 from
from
from 0x9C
0xA5
0x52 in
in the
the following
following
0x64 in the following instructions:
0x38 and 0x2F instructions:
instructions:
Purpose
...
LDI LDILDI 0x38
LDI
R16, R20, 0x9C
R20,
R20, 0x9C
0xA5
0x52;R16 = 0x38
Registers
R0 $001F IO Address
ALU LDI LDI 0x2F
LDI
LDI
R17, R21,
R21,
R21, 0x9C
0x23
0x73
R1 0x64
;R17 = $0020
0x2F
$00
$01
R2 Standard IO
SUB R17R20,
SUB
ADD R20, R21
R20, R21;add R17;add
R21 ;subtract
;subtract
R21 toR21R21 from R20
R20from R20
...
ADD R16, to Registers
R16
...
…
SPH $3E
SREG: I T H S V N Z C $005F
Solution:
Solution:
Solution: R15 11
SREG $3F
Solution:
CPU
$0060
$52
$9C
$A5 0101
R16 0010
1001 1100
1010 1100
0101
$38
$9C 0011
1001 1000
- $73 0111
R17 0011
...
+-- +$64
$9C
$23
$2F 10010100
0010
0110 1100
0011
1111
$DF 1101 1111 R20
R20 == $DF
…
PC $00
$82
$67 00000000
1000
0110 0000
0010
0111 R20
R16 == 00
$00
$82
0x67
$100 1 0000 R20
C = 1 because R21 is bigger than R20 and there is a borrow from D8 bit.
CC===100because
C becausethere
because R21 is
R21 is not
isnot bigger
bigger
a carry
R30 than R20
than
beyond R20 andbit.
and
the D7 there is
there is no
no borrow
borrow from
from D8
D8 bit.
bit.
Z
C == 00 decoder
because
because the
thereR20
is has
no a value
carry otherthe
beyond than
D7zero after the subtraction.
Instruction
ZZ =
H == 1
01 because
because there
because the R20
the R20 iscarry
is ahaszero after
a value
from the D3
other
the than 0 bit.
subtraction.
to theafter the subtraction.
D4 bit.
H = 1 because there isR31 a borrow
carry from
from D4D3
the toto
D3.
the D4 bit.
ZH
H =
= 00 because
because there
there is
is no
no borrow
borrow from
from D4
D4 to
to D3.
D3.
Z = 0 because the R16 (the result) has a value 0other
= 1 because
Instruction Register the R20 (the
registers
result) has a value in it after
than 0 the addition.
after the addition.
$FFFF
24
The End
QUESTIONS?
25