Blink LED with TIME DELAY in Atmega32 using ATMEL STUDIO 7 Assembly

Blink LED with TIME DELAY in Atmega32

Blinking an LED with a delay is the initial step in programming a microcontroller, much to writing “Hello World” in C or C++. An extremely well-liked, high-performance 8-bit AVR microcontroller is the Atmega32. DDR and PORT are the two registers that must be used for this sample project. The microcontroller’s DDR, or data direction register, sets the input/output direction of each pin. Corresponding pin outputs are made when the DDR register is HIGH, and corresponding pin inputs are made when the DDR register is LOW. The output register that ascertains the condition of every pin on a certain port is called the PORT register. The corresponding pin logic high (5V) is made when the PORT register is HIGH, and the corresponding pin logic low (0V) is made when the PORT register is LOW.

Blink LED with Time Delay in Atmega32

Let us understand it through a program in assembly language.

We’ll write a program to toggle all the bits of PORTB by sending values 0x55 and 0xAA continuously. Put a time delay between each issuing of data to PORTB.

Firstly, we will include a header file.

.INCLUDE “M32DEF.INC”

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

We always need to initialize the stack before calling the subroutine.

LDI R16,HIGH(RAMEND) 
OUT SPH, R16
LDI R16,LOW(RAMEND)
OUT SPL,R16                              // INITIALIZING  THE STACK

For initializing the stack, we OUT the High(RAMEND) on the SPH and we OUT the Low(RAMEND) on the SPL. Here SPH means Stack Pointer High and SPL means Stack Pointer Low. It means that we have specified the space for the stack in RAM.

LDI R16,0XFF
OUT DDRB,R16                          // portb=11111111

Here we loaded the hex value of FF in the general-purpose register of R16.

Then we configured the data direction register of the PORTB as output. That means that all the pins on the PORTB are 1 which also identifies that it has been configured as output.

Data direction registers are used for the configuration of the input or output.

LDI R19,0X55 

Then we loaded the hex value of 55 in general-purpose register R19. We have to complement this value of 55 and also OUTPUT this on port B.

LOOP1:

    COM R19
    CALL Delay
    OUT PORTB, R19
            CALL Delay
            JMP LOOP1
LDI R21,0XFF
  DELAY:
    NOP
            NOP
            DEC R21
            BRNE DELAY
            RET

What we have done above is first compliment the value of R19 and then we called the Delay, and it will jump to the delay.

When we jump to delay, we say that there is NOP i.e. no operation which means one cycle is wasted. Next is also no operation instruction which means one more cycle is wasted. The next command is of the decrement of the R21 value which is FF. The delay command remains true until the previous command, which is R21 remains non-zero.  When the value becomes true the BRNE command is terminated, and it jumps to RET.

The RET instruction gives the address of the next instruction to the CALL command to the program counter.

Now we will build the solution by pressing the F7 key and selecting the build option from the bar.

The next step is to debug the program which you can select from the menu bar.

As you can see above FF value has been loaded in R16 also on port B. That means port B pins have the 1 value.

After complementing this value, we will have AA in the R19. 

Now after calling the delay and wasting the cycles, we will have the following output.

Now you will see the output proteus. In Proteus, we have connected the 8 LEDs to port B because we have already configured port B as output.

Then add the hex file in the program files of the proteus.

Conclusion

Within this article, I have covered the following subjects:

  • Introduction and developing the code.
  • Codes that come with a description.
  • Schematic diagram with Proteus simulation.

Now you know how to Blink LED with Time Delay in Atmega32

CODE:

.INCLUDE "M32DEF.INC"
LDI R16,HIGH(RAMEND) 
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16      // INITIALIZING  THE STACK
LDI R16,0XFF
OUT DDRB,R16  // portb=11111111
LDI R19,0X55 

LOOP1:

    COM R19
    CALL Delay
    OUT PORTB,R19
CALL Delay
JMP LOOP1
LDI R21,0XFF
  DELAY:
    NOP
NOP
DEC R21
BRNE DELAY
RET

Additionally, If you want to see the project How to Make Simple Water Level Indicator in Atmega32 then visit our website

Leave a Comment

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