www. MicroDigital Ed. com
BIHEuniversity
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 Loop
Branch, Call, and Time Delay Loop
Chapter 4
Chapter 4
The AVR microcontroller
and embedded
systems
using assembly and c
www. MicroDigital Ed. com
BIHEuniversity
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 Looping
Branch 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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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 Stack
Call instructions and Stack
• CALL
www. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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 Pipeline
AVR 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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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. MicroDigital Ed. com
BIHEuniversity
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 Answers
Questions and Answers
END

AVR_ Microcontroller_Muhammad Ali_Mazidi_AVR_Lecture4_Fall2023

  • 1.
    www. MicroDigital Ed.com BIHEuniversity 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 Loop Branch, Call, and Time Delay Loop Chapter 4 Chapter 4 The AVR microcontroller and embedded systems using assembly and c
  • 2.
    www. MicroDigital Ed.com BIHEuniversity 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 Looping Branch 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.
  • 3.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 4.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 5.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 6.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 7.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 8.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 9.
    www. MicroDigital Ed.com BIHEuniversity 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)
  • 10.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 11.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 12.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 13.
    www. MicroDigital Ed.com BIHEuniversity 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 Stack Call instructions and Stack • CALL
  • 14.
    www. MicroDigital Ed.com BIHEuniversity 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).
  • 15.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 16.
    www. MicroDigital Ed.com BIHEuniversity 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
  • 17.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 18.
    www. MicroDigital Ed.com BIHEuniversity 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
  • 19.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 20.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 21.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 22.
    www. MicroDigital Ed.com BIHEuniversity 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)
  • 23.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 24.
    www. MicroDigital Ed.com BIHEuniversity 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 Pipeline AVR 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
  • 25.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 26.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 27.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 28.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 29.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 30.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 31.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 32.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 33.
    www. MicroDigital Ed.com BIHEuniversity 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.
  • 34.
    www. MicroDigital Ed.com BIHEuniversity 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 Answers Questions and Answers END