COUNTER-in-Atmega32-using-ATMEL-STUDIO-7-Assembly

COUNTER in Atmega32 using ATMEL STUDIO 7 Assembly

Ever wonder how electrical gadgets count occurrences or keep track of time? One key idea in the realm of ATMEL STUDIO 7 Assembly language programming and microcontrollers such as Atmega32 is “COUNTER.” The microcontroller may record events or monitor time intervals with the aid of this little yet effective function. This introduction will cover the fundamentals of utilizing ATMEL STUDIO 7’s Assembly language to operate a COUNTER in an Atmega32. If these phrases seem a little confusing right now, don’t worry—we’ll break it down step by step so you can comprehend it easily!

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.

.DEF COUNT=R17                  // VARIABLE=R17

Here we declare general purpose register to COUNT. whenever we use COUNT that means we are using the general purpose register.

.ORG 0X00                            // INITIAL LOCTATION

Here we gave the initial position to our program which means we are starting our program from this location.

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.

LDI COUNT,0             // R17 HAS 0 INITIALLY

Then we loaded the 0 value in the count variable because we have to load the values from 0 to 255 as mentioned above in the statement.

LOOP:
     CALL COUNTER               // CALLING SUBROUTINE
             RJMP LOOP        

Here we are calling the subroutine and we named it counter.           

COUNTER:                 // SUBROUTINE
    INC COUNT                     //R17=R17+1
            OUT PORTB,COUNT             //PORTB=R17
            CALL Delay
            RET

And what happens in the counter is we are incrementing the COUNT and then OUTPUT that value of the general-purpose register on the PORTB.

According to our program, we will create a delay between the first subroutine and the other subroutine that we are now creating.

So, it will jump on the delay below and waste the cycles.

LDI R21,0XFF
  DELAY:
    NOP
            NOP
            DEC R21
            BRNE DELAY
            RET

 The delay subroutine is running based on the value that we loaded in the R21.  When the values become zero in the R21 then the branch not equal becomes false and it will run out of that loop and come to RET instruction.

The branch not equal remains true until the value on the previous instruction is non-zero. 

Delay time will be calculated based on number of cycles that are running.

Now we will build our program by pressing the F7 key. The next step is to debug the program by selecting the debug option.

Selection of Debug Option:

Below is the way how to open the processor status.

Before jumping on the COUNTER, notice the cycle counter and stopwatch time.

Now after jumping on the COUNTER, it will increment the value in R17.

Conclusion

There you have it, then! We moved through ATMEL STUDIO 7 Assembly’s COUNTER universe in Atmega32. Recall that this is only the start. As you continue to investigate, you’ll discover even more amazing things you can do using COUNTERs to add intelligence and excitement to your projects. If you keep trying, you’ll quickly become an expert at configuring your Atmega32 microcontroller to perform incredible tasks!

For Complete Trial Watch the Video: COUNTER 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 *