Skip to content
Related Articles

Related Articles

Time Delay in AVR Microcontroller
  • Last Updated : 21 Dec, 2020

In this article, we will see how to create delays for the AVR. Also, we will discuss the instruction pipeline.

Delay Calculation for AVR :
In Assembly Language instructions, to create a time delay one must consider two important factors.

  1. The crystal frequency –
    The frequency of the crystal oscillator connected to XTAL1 and XTAL2 is one factor for calculating the time delay. The duration of the clock period for the instruction cycle is a function of this crystal frequency.

  2. The AVR design –
    AVR microprocessors are able to execute an instruction in one cycle. There are three ways of doing this.

    1. Use Harvard architecture to get the maximum amount of code and data into the CPU.
    2. Use RISC architecture features such as fixed-size instructions.
    3. Use pipelining to overlap fetching and execution of instructions.

Pipelining :
In the early microprocessors, the CPU could either fetch or execute at a given time. In other words, the CPU had to fetch an instruction from the memory, then execute it then again fetch the next instruction, execute it, and so on. Pipelining allows the CPU to fetch and execute the given instruction at the same time.

We can use the pipeline to speed up execution of instructions. In pipelines, the process of execution is split up into smaller steps that are all executed in parallel. In the execution of instructions, we must make sure that the sequence of instructions is kept intact and that there is no different execution.

Instruction cycle time for the AVR :
It takes a certain amount of time for the CPU to execute an instruction. This time is referred to as machine cycles. All the instructions in the AVR are either 2-byte or 4-byte and hence most of the instructions take no more than 2 machine cycles to execute (some instructions may take up 3 to 4 machine cycles to execute). In the AVR family, the duration of the machine cycle depends upon the frequency of the oscillator connected to the AVR system. In the AVR, one machine cycle consists of one oscillator period, which means that with each oscillator clock, one machine cycle passes. Therefore, to calculate the machine cycle for the AVR, we take the inverse of the crystal frequency.

Example-1 :
For the given crystal frequencies, calculate the period of the instruction cycles.

a) 8 MHz    b) 16 MHz

Solution : a) instruction cycle = 1/ 8 MHz = 0.125 us (microsecond)   
           b) instruction cycle = 1/ 16 MHz = 0.0625 us

Instruction cycles required by different instructions (considering 1 MHz as the crystal frequency) :

Instruction Instruction cycles Time to execute
LDI 1 1 us
DEC 1 1 us
OUT 1 1 us
ADD 1 1 us
NOP 1 1 us
JMP 3 3 us
CALL 4 4 us
BRNE 2/1 2 us if taken, 1 us if it fails

Example-2 :
Find the delay in us of the code snippet below if the crystal frequency is 10 MHz.

                                          Instruction Cycles
DELAY :   LDI        COUNT, 0XFF                  0
Again :   NOP                                     1          
          NOP                                     1
          NOP                                     1
          DEC        COUNT                        1
          BRNE       AGAIN                       2/1
          RET                                     4
Solution :  Time Delay = [1 +(( 1+ 1+ 1+ 1 + 2 ) x 255) + 4 ] x 0.1 us = 153.5 us

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :