DELAY in Atmega32 using ATMEL STUDIO 7 Assembly

The ATmega32 AVR microcontroller’s software may produce a time delay by using the delay function. The function is installed together with Atmel Studio and is a part of the GNU library.

While software delay functions are useful for short applications for rapid development and experimentation, in most circumstances, it is best to utilize an AVR timer to produce delays.

More are the cycles and more will be the delay created.

Let us understand it through a program in assembly language.

Firstly, we will include a header file.

CODE:

.INCLUDE “M32DEF.INC”

If you do not know how to include it, you can check by clicking here.

To get to know about DELAY let’s start by writing a mini instruction.

LDI R20, 0XFF
DELAY:
    NOP                          // NO OPERATION 
            NOP
            NOP
            NOP
            DEC R20
            BRNE DELAY
            RET

First of all, we will load an 8-bit hex value i.e., 255 in R20. Then we used NOP (No Operation) commands which means the cycles will be wasted but no operation will be performed. Then we decrease the value of R20 until it reaches 0.

BRNE (Branch Not Equal) remains true till we have the non-zero value in the previous command. It will continue to jump to DELAY till the value decreases to 0.

When it becomes 0 the BRNE command becomes false, and it moves forward. This is called and as well subroutine. To end the subroutine, we used the RET command.   

DEBUG:

You can see below the vale 255 has been loaded in the register and the count cycle is 1.

As you step over, you can see that the cycles are increasing. As you have learned before, the greater the number of cycles the more delay will be created.

On further moving you can see that after the decrement you left with the value of FE.

Also, keep in mind that the branch not equal command took two cycles while the other commands take only a single cycle while executing. When the branch not equal command is true it takes two cycles but when it becomes false it takes only a single cycle.

Now let’s talk about creating a big delay for this purpose we need to use the nested loop.

LDI r20, 0x10
DELAY:
LDI R21,0XFF
  DELAY1:
    NOP
    NOP
    DEC R21
    BRNE DELAY1

What will happen is that it first loads the value of 10 in register 20. But before DELAY1 runs, it first loads the value of FF in R21. The rest of the instructions will run and when this loop becomes false it runs toward the main loop of DELAY and runs the instructions of NOP and decrement the value of 20. And whenever this statement becomes true it jumps to the loop of DELAY.

In this way, the cycles will be wasted, and more and more delays will be created.

You can see above both values have been loaded in the respective registers.

Conclusion

To sum up, employing the ATMEL Studio 7 Assembly language to create delays in Atmega32 provides a subtle balance between accuracy and ease of use. Although it offers direct control over time and facilitates effective resource use, one possible disadvantage may be the need for manual calibration for various clock frequencies.

In addition, the code’s readability could be impaired in contrast to high-level languages. However, the Atmega32’s assembly-based delay might be a wise option for applications where exact timing is essential and resource efficiency is a top concern. In the end, it comes down to the project’s particular requirements and the programmer’s comfort level with its specifics.

For Complete Trial Watch the Video: DELAY in Atmega32 using ATMEL STUDIO 7 Assembly

For more blogs explore the website: https://ninjatech.live/

Leave a Comment

Your email address will not be published. Required fields are marked *