AVR_ Microcontroller_Muhammad Ali_Mazidi_AVR_Lecture4_Fall2023

60 views 34 slides Aug 17, 2024
Slide 1
Slide 1 of 34
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34

About This Presentation

AVR Microcontroller Muhammad Ali Mazidi


Slide Content

www. Mi cro D igit al Ed. com
BIH E universit y
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.
Branch, Call, and Time Delay LoopBranch, Call, and Time Delay Loop
Chapter 4Chapter 4
The AVR microcontroller
and embedded
systems
using assembly and c

www. Mi cro D igit al Ed. com
BIH E universit y
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.
Branch Instruction and LoopingBranch Instruction and Looping
•Looping in AVR:
•Repeating a sequence of instructions or an operation a certain number of times is
called a loop.
•In the above program, we add 3 to R16 six times. That makes 6 x 3 = 18 = 0x12.
•One problem with the above program I that too much code space would be needed to
increase the number of repetitions to 50 or 100.

www. Mi cro D igit al Ed. com
BIH E universit y
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.
•Using BRNE instruction for looping:
•The BRNE (branch if not equal) instruction uses the zero flag in the status register.

www. Mi cro D igit al Ed. com
BIH E universit y
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.

www. Mi cro D igit al Ed. com
BIH E universit y
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.

www. Mi cro D igit al Ed. com
BIH E universit y
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.
•Loop inside a loop
•We use a loop inside a loop, which is called a nested loop.

www. Mi cro D igit al Ed. com
BIH E universit y
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.

www. Mi cro D igit al Ed. com
BIH E universit y
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.

www. Mi cro D igit al Ed. com
BIH E universit y
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.
•Other conditional jumps
•BREQ (branch if equal, branch if Z = 1)

www. Mi cro D igit al Ed. com
BIH E universit y
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.
BRSH (branch if same or higher, branch if C = 0)
will execute the next instruction below BRSH.

www. Mi cro D igit al Ed. com
BIH E universit y
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.

www. Mi cro D igit al Ed. com
BIH E universit y
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.
•Unconditional branch instruction
•JMP (JMP is a long jump):
•JMP is an unconditional jump that can go to any memory location in the 4M (word)
address space of the AVR.
•10 bit for opcode.
•The 22 bit target address allows a jump to 4M (words) of memory locations from
$000000 to $3FFFFF so it can cover the entire address space.
•RJMP (relative jump):
•In this 2 byte (16bit) instruction, the first 4 bit are the opcode and the rest (lower 12
bits) is the relative address of the target location.
•The address range of $000 - $FFF is divided into forward and backward jumps.
•IJMP(indirect jump):
•IJMP is a 2 byte instruction. When the instruction executes, the PC is loaded with the
content of the Z register, so it jump to the address pointed by the Z register.

www. Mi cro D igit al Ed. com
BIH E universit y
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.
Call instructions and StackCall instructions and Stack
•CALL

www. Mi cro D igit al Ed. com
BIH E universit y
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.
•Stack and stack pointer in AVR
•The stack is a section of RAM used by the CPU to store information temporarily. This
information could be data or an address.
•How stack are accessed in the AVR:
•If the stack is a section of RAM, there must be a register inside the CPU to point to it.
The register used to access the stack is called the SP (stack pointer) register.
•SPL (low byte of the SP) and SPH (high byte of the SP).

www. Mi cro D igit al Ed. com
BIH E universit y
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.
•To store the value in stack we use PUSH command and retrieve the value back POP.
•Pushing onto the stack
•The stack pointer (SP) point to the top of the stack (TOS). As we push data onto the
stack, the data are saved where the SP points to and the SP is decremented by one.
•Popping from the stack
•Popping the contents of the stack back into a given register I the opposite process of
pushing. When POP instruction is executed, the SP is incremented and the top
location of the stack is copied back to the register. That means the stack is LIFO (last
in first out) memory.

www. Mi cro D igit al Ed. com
BIH E universit y
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.
•Initializing the stack pointer

www. Mi cro D igit al Ed. com
BIH E universit y
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.

www. Mi cro D igit al Ed. com
BIH E universit y
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.
•CALL instruction and the role of the stack
•RET instruction and the role of the stack

www. Mi cro D igit al Ed. com
BIH E universit y
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.

www. Mi cro D igit al Ed. com
BIH E universit y
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.

www. Mi cro D igit al Ed. com
BIH E universit y
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.
•Calling many subroutines from the main program:
•In Assembly language programming, it is common to have one main program and
many subroutines that are called from the main program.

www. Mi cro D igit al Ed. com
BIH E universit y
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.
•RCALL (relative call)

www. Mi cro D igit al Ed. com
BIH E universit y
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.

www. Mi cro D igit al Ed. com
BIH E universit y
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.
AVR Time Delay and Instruction PipelineAVR Time Delay and Instruction Pipeline
•Delay calculation for the AVR:
•In creating a time delay using Assembly language instruction, one must be mindful of
two factors that can affect the accuracy of the delay
•1. The crystal frequency
•2. The AVR design
•Done using three ways to do that
•1. Use Harvard architecture
•2. Use RISC architecture
•3. Use pipelining
•Pipelining

www. Mi cro D igit al Ed. com
BIH E universit y
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.
•AVR multistage execution pipeline
•In AVR each instruction is executed in 3 stages: operand fetch, ALU operation
execution, and result write back.

www. Mi cro D igit al Ed. com
BIH E universit y
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.
•Instruction cycle time for the AVR
•It takes a certain amount of time for the CPU to execute an instruction.
•This is referred to as machine cycles.
•Because all the instructions in the AVR are either 1 word (2byte) or 2 word (4byte)
most instruction take no more than one or two machine cycles to execute.

www. Mi cro D igit al Ed. com
BIH E universit y
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.

www. Mi cro D igit al Ed. com
BIH E universit y
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.

www. Mi cro D igit al Ed. com
BIH E universit y
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.

www. Mi cro D igit al Ed. com
BIH E universit y
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.

www. Mi cro D igit al Ed. com
BIH E universit y
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.

www. Mi cro D igit al Ed. com
BIH E universit y
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.

www. Mi cro D igit al Ed. com
BIH E universit y
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.

www. Mi cro D igit al Ed. com
BIH E universit y
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.
Questions and AnswersQuestions and Answers
END
Tags