Think of a Nested Loops in Atmega32 like a set of instructions that tells the ATmega32 to repeat a task over and over again. In this magical world of microcontrollers, Atmel Studio 7 is like a wizard’s workshop where we craft these instructions in a language called assembly.
In this article, we’re going to take you on a journey into the fascinating world of nested loops in ATmega32 using Atmel Studio 7 assembly. We’ll explain what nested loops are, how they work, and how you can use them to make your gadgets do amazing things. So, if you’re curious to learn how to make your gadgets smarter, stick with us as we explore the enchanting realm of nested loops.
Let us understand it through a program in assembly language.
Firstly, we will include a header file.
CODE:
.INCLUDE “M32DEF.INC”
Then we will give the starting location to our program. Which means that from where we want to start our program.
.ORG 0X00 // starting location.
First, we need to keep in mind that Atmega microcontroller is 8 bits. And all the general purpose registers are also of 8 bits. Which means that the maximum value of these registers is 28 i.e. 256 which means from 0 to 255. Which also tells us that we cannot load values more than this.
As we have to load the values 500 times, we need nested loops.
LDI R16,0XFF // R16=0XFF
OUT DDRB, R16 // PORTB CONFIGURED AS OUTPUT
In the above first 2 lines of the code we loaded the hex value FF into the register R16. Then we OUT that value on DDRB i.e., data direction register. Data direction register is used for the configuration of input and output. So, we configured it here as output.
LDI R17,2 // R17=2 CONTROLING LOOP1
LDI R19,0XAA // R19=0XAA
Then we took a general purpose register R17 and load the decimal value 2 into it. This value will control the loop 1.
And then in the next step we loaded our main value 0XAA, which we need to be complemented, loaded into the general purpose register R19.
LOOP1:
LDI R18,250 //R18=250 CONTROLLING LOOP2
LOOP2:
OUT PORTB,R19 // PORTB=R19
COM R19 // R19=~R19
DEC R18 // R18=R18-1
BRNE LOOP2 // IF Z FLAG HAS 0 THEN JUMP BACK TO LOOP2
In loop1, we loaded a decimal value 250 into the general purpose register R18.
Inside loop1 we will use another loop which is named loop2 as given above.
In loop2 we first load our R19 value on PortB as an output. And then in the next step we complement that value as we have given in the statement. Complement actually means, convert all the binary values 1 into zero and vice versa.
In the next step we decrement the R18 which was actually the 250 values in the loop1. It will continue to decrement until we will have the value 0 and it continue to jump to loop2, and branch not equal condition remain true.
When it completed its cycle for 255 times, branch not equal condition will become false because the R18 will have the 0 value. And then it will jump to the next instruction.
DEC R17 // R17=R17-1
BRNE LOOP1 // JUMP TILL Z-FLAG =1, Z FLAg IS 1 WHEN ANSWER OF previous statement IS ZERO
break
In the above step, we will decrement the R17 value. As we know R17 had the value of 2 after decrementing it will leave with 1.
Then in the next step we used the branch not equal command for the loop1 that is our main loop. Our main loop is now based on R17, and we have the value of 1 in R17 and the condition becomes true. And it again jumped to loop1.
Keep in mind that we had the complement on PortB 255 times.
When it again jumped to the loop1 it will again load the value of 250 and it again jumped to the loop2 and check the conditions of branch not equal command. The branch not equal condition will become false for this time, and it jumped to the next instruction of DEC R17. Remember we now have the 1 value in R17 register and now BRNE will again check the value of R17 which is now 0. Then it will terminate the loop and jump to the break command.
We now have the value 1 for the Z flag. And it got raised as we have 0 value in R17.
Now we will run our code.
DEBUG:
And the next step is to debug the code also check the input output and the process status.
When we step over our code, we can see from the above image that we’ve loaded the data direction register with the value 1 on all its pins, indicating that the port has been configured as output.
OUTPUT:
In this step R19 has been loaded with 0XAA value which we need to be complemented.
Above shows the output of loop1, indicating that the value 250 has been loaded into R18.
And the value got decremented.
To see the output on the proteus you first need to add the hex file into the program file.
But now after complementing you can see the output below.
OUTPUT:
Conclusion
In the realm of embedded systems and microcontroller programming, efficient and precise control over hardware is paramount. Developers and engineers widely harness the versatility and capabilities of the Atmel ATmega32 microcontroller for various applications in robotics, automation, and IoT devices.One fundamental technique in programming microcontrollers is the use of nested loops, which allow for precise control of repetitive tasks and the manipulation of peripherals. In this context, Atmel Studio 7, a popular integrated development environment (IDE), is a powerful tool for writing and compiling assembly code for the ATmega32 microcontroller.
For Complete Trial Watch the Video: Nested Loops in Atmega32 using ATMEL STUDIO 7 Assembly
For more blogs explore the website: https://ninjatech.live/