Unit 4
Unit 4
Unit 4
Term:2015-2016
III Year B.-tech I Sem
UNIT 4 SYLLABUS
UNIVERSITY QUESTIONS
1. What is the difference between microprocessor and
microcontroller? Give 8051 architecture.
2)Explain standard AVR architecture.
3. Explain memory organization in AVR.
4. Explain internal and external memory of 8051.
5. Explain interrupt structure in AVR.
6. Give any five instructions of 8051 and explain each.
7. Explain register file of AVR.
4.1 INTRODUCTION
RAM
ROM
I/O
Time Seri
Port
al
r
CO
COM
M
Port
Port
A single chip
Microcontroller
Microcontroller (uC)
u-Computer on a single chip of silicon
Interrupt
Control
4k
ROM
Timer 1
Timer 2
128 bytes
RAM
CPU
OSC
Bus
Control
4 I/O Ports
P0 P2 P1
Addr/Data
Serial
P3
TXD RXD
Vcc
Internal
Pull-Up
Internal CPU
bus
Write to
latch
P1.X
Clk
Q
M1
B1
Read pin
P1.X
pin
2 Tri-state buffer
B1: controlled by Read pin
Read pin 1 really read the data present at the pin
A transistor M1 gate
Gate=0: open
Gate=1: close
Internal RAM
First 128 bytes:
00h to 1Fh Register Banks
20h to 2Fh
External Memory
/EA pin 31 external access
/EA=0 indicates that code is stored externally.
/PSEN ALE are used for external ROM.
For 8051 internal code, /EA pin is connected to
Vcc.
/ means active low.
1F
18
17
Bank 2
10
0F
Bank 1
08
07
06
05
04
03
02
01
00
R7
R6
R5
R4
R3
R2
R1
R0
Bank 0
4 Register Banks
Each bank has R0-R7
Selectable by PSW.2,3
78
20h 2Fh
(16 locations 8-bits = 128 bits)
2D
2C
Bit addressing:
mov C, 1Ah
or
mov C, 23h.2
2B
2A
29
28
27
26
25
1A
24
23
10
22 0F
08
21 07
20
06
05
04
03
02
01
00
TMOD Register
Gate :
is high.
TCON Register
Interrupt
EA
---
: Global enable/disable.
: Undefined.
mov B, #11
mov DPTR,#7521h
;DPTR = 0111010100100001
COUNT EGU 30
~
~
0RG 200H
MYDATA:DB IRAN
MOV
ADD
ADD
MOV
MOV
MOV
A,R7
A,R4
A,R7
DPTR,#25F5H
R5,DPL
R,DPH
a, 70h
R0,40h
56h,a
0D0h,a
MOV A,R4
MOV A,7
MOV A,R7
MOV 7,2
MOV R7,R6
MOV R2,#5
MOV R2,5
;Put 5 in R2
;Put content of RAM at 5 in R2
; dptr 9000h
; a M[9000]
;a M[4005]
continue
PC
1000
1002
1003
mov a, #5
movc a, @a + PC
Nop
;a M[1008]
Table Lookup
MOVC only can read internal code
memory
Arithmetic Instructions
Add
Subtract
Increment
Decrement
Multiply
Divide
Decimal adjust
Arithmetic Instructions
Mnemonic
Description
ADD A, byte
ADDC A, byte
SUBB A, byte
INC A
increment A
INC byte
INC DPTR
DEC A
decrement accumulator
DEC byte
decrement byte
MUL AB
DIV AB
DA A
ADD Instruction
add a, byte
; a a + byte
addc a, byte
; a a + byte + C
These instructions affect 3 bits in PSW:
C = 1 if result of add is greater than FF
AC = 1 if there is a carry out of bit 3
OV = 1 if there is a carry out of bit 7, but not from bit 6, or
visa versa.
ADD Examples
What is the value of
the C, AC, OV flags
after the second
instruction is
executed?
mov a, #3Fh
add a, #D3h
0011 1111
1101 0011
10001 0010
C = 1
AC = 1
OV = 0
2s
0000
0111
1000
1111
complement:
0000 00 0
1111
0000
1111
7F 127
80 -128
FF -1
(negative 113)
(negative 45)
(overflow)
Addition Example
; Computes Z = X + Y
; Adds values at locations 78h and 79h and puts them in 7Ah
;-----------------------------------------------------------------X
equ
78h
Y
equ
79h
Z
equ
7Ah
;----------------------------------------------------------------org 00h
ljmp Main
;----------------------------------------------------------------org 100h
Main:
mov a, X
add a, Y
mov Z, a
end
Subtract
SUBB A, byte
Example:
SUBB A, #0x4F
;A A 4F C
Notice that
There is no subtraction WITHOUT borrow.
Therefore, if a subtraction without borrow is desired,
it is necessary to clear the C flag.
Example:
Clr c
SUBB A, #0x4F
;A A 4F
INC A
increment A
INC byte
INC DPTR
DEC A
decrement accumulator
DEC byte
decrement byte
Multiply
When multiplying two 8-bit numbers, the size of the
maximum product is 16-bits
FF x FF = FE01
(255 x 255 = 65025)
MUL AB
; BA
A * B
Division
Integer Division
DIV AB
; divide A by B
A Quotient(A/B)
B Remainder(A/B)
OV - used to indicate a divide by zero condition.
C set to zero
Decimal Adjust
DA a
; decimal adjust a
Logic Instructions
Bitwise logic operations
Clear
Rotate
Swap
Logic instructions do NOT affect the flags in PSW
Bitwise Logic
ANL AND
ORL OR
XRL XOR
CPL Complement
Examples:
ANL
00001111
10101100
00001100
00001111
ORL 10101100
10101111
XRL
00001111
10101100
10100011
CPL
10101100
01010011
a, byte
direct, reg. indirect, reg,
immediate
byte, a
direct
byte, #constant
CPL Complement
ex:
cpl a
;PSW OR 00011000
clear
rotate left
rotate left through Carry
rotate right
rotate right through Carry
swap accumulator nibbles
A
byte
Ri
@Ri
(direct mode)
(register mode)
(register indirect mode)
Rotate
Rotate instructions operate only on a
RL a
Mov a,#0xF0
; a 11110000
RR a ; a 11100001
RR a
Mov a,#0xF0
; a 11110000
RR a ; a 01111000
mov a, #0A9h
add a, #14h
; a A9
; a BD (10111101), C0
rrc a
; a 01011110, C1
RLC a
mov a, #3ch
setb c
; a 3ch(00111100)
; c 1
rlc a
; a 01111001, C1
Rotate and
Multiplication/Division
Note that a shift left is the same as
multiplying by 2, shift right is divide by
2
mov
clr
rlc
rlc
rrc
a, #3 ; A 00000011 (3)
C ; C 0
a ; A 00000110 (6)
a ; A 00001100 (12)
a ; A 00000110 (6)
Swap
SWAP a
mov a, #72h
swap a
; a 27h
; a 27h
Unconditional Jumps
SJMP <rel addr>
Short jump,
relative address is 8-bit 2s complement number,
so jump can be up to 127 locations forward, or 128
locations back.
Long jump
Absolute jump to
anywhere within 2K block of program memory
JMP @A + DPTR
indexed jump
Long
Infinite Loops
Re-locatable Code
Memory specific NOT Re-locatable (machine
code)
org 8000h
Start: mov C, p1.6
mov p3.7, C
ljmp Start
end
Jump table
Mov
Mov
Rl
Jmp
dptr,#jump_table
a,#index_number
a
@a+dptr
...
Jump_table: ajmp case0
ajmp case1
ajmp case2
ajmp case3
Conditional Jump
These instructions cause a jump to occur only if
a condition is true. Otherwise, program
execution continues with the next instruction.
loop: mov a, P1
jz loop
mov b, a
Conditional jumps
Mnemonic
Description
JZ <rel addr>
Jump if a = 0
Jump if a != 0
JC <rel addr>
Jump if C = 1
Jump if C != 1
Jump if bit = 1
Jump if bit != 1
&clear
send a 0 to LED
else
send a 1 to LED
jz led_off
Setb P1.6
sjmp skipover
led_off: clr P1.6
mov A, P0
skipover:
Description
Iterative Loops
For A = 0 to 4 do
{}
clr a
loop: ...
...
inc a
cjne a, #4, loop
For A = 4 to 0 do
{}
mov R0, #4
loop: ...
...
djnz R0, loop
Iterative Loops(examples)
mov a,#50h
mov b,#00h
cjne a,#50h,next
mov b,#01h
next: nop
end
mov a,#0aah
mov b,#10h
Back1:mov r6,#50
Back2:cpl a
djnz r6,back2
djnz b,back1
end
mov a,#25h
mov r0,#10h
mov r2,#5
Again: mov @ro,a
inc r0
djnz r2,again
end
mov a,#0h
mov r4,#12h
Back: add a,#05
djnz r4,back
mov r5,a
end
; stack PC
; PC address 16 bit
Return
Return is also similar to a jump, but
Return instruction pops PC from stack to
get address to jump to
ret
; PC stack
Subroutines
call to the subroutine
Main:
sublabel:
...
acall sublabel
...
...
...
...
the subroutine
ret
Subroutine - Example
square: push b
mov b,a
mul ab
pop b
ret
8 byte and 11 machine cycle
square: inc a
movc a,@a+pc
ret
table: db 0,1,4,9,16,25,36,49,64,81
13 byte and 5 machine cycle
sqrt:
Sqrs:
reset service
; Port 3 is an input
; Clear bits 7..4 of A
inc a
movc a, @a + PC
ret
db 0,1,1,1,2,2,2,2,2,3,3,3,3,3,3,3
end
main program
subroutine
data
Why Subroutines?
Subroutines allow us to have
"structured" assembly language
programs.
This is useful for breaking a large
design into manageable parts.
It saves code space when
subroutines can be called many
times in the same program.
example of delay
mov a,#0aah
Back1:mov p0,a
lcall delay1
cpl a
sjmp back1
Delay1:mov r0,#0ffh;1cycle
Here: djnz r0,here ;2cycle
ret
;2cycle
end
Delay=1+255*2+2=513 cycle
Delay2:
mov r6,#0ffh
back1: mov r7,#0ffh ;1cycle
Here: djnz r7,here ;2cycle
djnz r6,back1;2cycle
ret
;2cycle
end
Delay=1+(1+255*2+2)*255+2
=130818 machine cycle
P1.6
reset service
100h
cpl
sjmp
Delay:
Loop1:
Loop0:
clr
GREEN_LED
acall Delay
GREEN_LED
Again
mov
mov
mov
main program
R7, #02
R6, #00h
R5, #00h
djnz
djnz
R5, $
R6, Loop0
djnz
R7, Loop1
ret
END
equ
subroutine
Example
; Move string from code memory to RAM
org 0
mov dptr,#string
mov r0,#10h
Loop1:
clr a
movc a,@a+dptr
jz stop
mov @r0,a
inc dptr
inc r0
sjmp loop1
Stop:
sjmp stop
; on-chip code memory used for string
org 18h
String:
db this is a string,0
end
Example
; p0:input p1:output
mov a,#0ffh
mov p0,a
back:
mov a,p0
mov p1,a
sjmp back
Again:
request
setb p1.2
mov a,#45h
;data
jnb p1.2,again ;wait for data
mov p0,a
setb p2.3
clr p2.3
;enable strobe
Example
; duty cycle 50%
back:
cpl p1.2
acall delay
sjmp back
back:
setb p1.2
acall delay
Clr p1.2
acall delay
sjmp back
Example
; duty cycle 66%
back:
setb p1.2
acall delay
acall delay
Clr p1.2
acall delay
sjmp back
GERNEL purpose
32 general purpose registers having storage capacity of 8-Bits
Named as R0,R1,R2 to R31.
Register 0 to 15 & 16 to 31 are different.
Can store both Data & Addresses.
SPECIAL purpose: Three registers
Program counter
Stack Pointer
Status Register
Sequence
4.11 AVR
Memory Access
Memory:
Program Memory (Flask
Memory)
2K Bytes of flash memory
128 Bytes of In-System
Programmable EEPROM
program memory holds
interrupt function addresses,
16 bit and double word (32
bit) opcode, and static data
tables
Data Memory
AVR
AT90S2313 instruction Architecture
AVR Instruction SET
118 Powerful Instructions Most Single Clock Cycle Execution
All arithmetic operations are done on registers R0 - R31
Mostly instructions take one cycle for execution
ADD Rd,Rr
Rd: Destination (and source) register in the Register File
Rr: Source register in the Register File
Instruction Execution
Instruction add R23, R11
Be encoded as the 16-bit opcode 0x0EEB.
Bit pattern :
Three components.
5 red bits 00011 distinguish this as an add
instruction.
5 blue bits 10111 indicates register 23 is the first
operand register.
The 5 green bits 01011 indicates register 11 is
the second operand register.
All add Rd, Rr instructions follow this pattern.
AVR
Peripheral Features
FullDuplexUART
to protect ARM IP
Processor Modes
The ARM has seven basic operating modes:
User : unprivileged mode under which most tasks run
FIQ : entered when a high priority (fast) interrupt is raised
IRQ : entered when a low priority (normal) interrupt is raised
Supervisor : entered on reset and when a Software Interrupt
instruction is executed
Abort : used to handle memory access violations
Undef : used to handle undefined instructions
System : privileged mode using the same registers as user mode
FIQ
User
mode
r0-r7,
r15,
and
cpsr
IRQ
SVC
User
mode
r0-r12,
r15,
and
cpsr
User
mode
r0-r12,
r15,
and
cpsr
Undef
User
mode
r0-r12,
r15,
and
cpsr
Abort
User
mode
r0-r12,
r15,
and
cpsr
r8
r9
r10
r11
r12
r13 (sp)
r14 (lr)
r13 (sp)
r14 (lr)
r13 (sp)
r14 (lr)
r13 (sp)
r14 (lr)
r13 (sp)
r14 (lr)
spsr
spsr
spsr
spsr
spsr
Thumb state
Low registe
Thumb state
High registe
cpsr
The Registers
28 27
NZCVQ
f
23
16 15
U n d e f i n e d
s
x
24
IFT
mode
c
T Bit
Architecture xT only
T = 0: Processor in ARM state
T = 1: Processor in Thumb state
J bit
Mode bits
Specify the processor mode
Exception Handling
When an exception occurs, the ARM:
Copies CPSR into SPSR_<mode>
Sets appropriate CPSR bits
0x1C
Change to ARM state
Change to exception mode
Disable interrupts (if appropriate)
FIQ
IRQ
(Reserved)
Data Abort
Prefetch Abort
0x18
0x14
0x10
0x0C
Stores the return address in LR_<mode>
Software Interrupt
0x08
Sets PC to vector address
Undefined Instruction
0x04
To return, exception handler needs
Reset
0x00 to:
Vector
table can
be at
Vector
Table
0xFFFF0000 on
state.and onARM720T
ARM9/10 family
devices
Development of the
ARM Architecture
1
2
Halfword
and
signed
halfword /
byte
support
System
3
mode
Thumb
instructi
Early
on set
ARM
ARM7TD
architectu MI
res
ARM720T
4
SA-110
SA1110
4T
ARM9TD
MI
ARM940
T
Improved
ARM/Thu 5TE
mb
Interworki
ng
Saturated
maths
CLZ
DSP multiplyaccumulate
ARM102
instructions
0E
XScale
ARM9E-S
ARM966E
-S
Jazelle
5TEJ
Java bytecode
execution
ARM9EJS
ARM7EJS
ARM926EJS
ARM1026E
J-S
SIMD
Instructions
Multi-processing
V6 Memory
architecture
(VMSA)
ARM1136EJ
-S
Unaligned data
support
32 bit RAM
Interrupt
Controller
nIRQ
8 bit ROM
nFIQ
ARM
Core
Peripherals
I/O
AMBA
Arbiter
External
RAM
ARM
TIC
External
Bus
Interface
Decoder
Remap/
Pause
AHB or ASB
APB
System Bus
Peripheral Bus
AMBA
ADK
Complete AMBA Design Kit
Interrupt
Controller
On-chip
RAM
Timer
Bus Interface
Bridge
External
ROM
Reset
ACT
AMBA Compliance Testbench
PrimeCell
ARMs AMBA compliant peripherals
AMBA
The objective of the AMBA specification is to:
Facilitate right-first-time development of embedded microcontroller products with one or more CPUs,
GPUs or signal processors,
Be technology independent, to allow reuse of IP cores, peripheral and system macrocells across
diverse IC processes,
Encourage modular system design to improve processor independence, and the development of
reusable peripheral and system IP libraries
Minimize silicon infrastructure while supporting high performance and low power on-chip
communication.