Flags in AVR Atmega32 using Atmel Studio

What are The Flags in AVR Atmega32

In this article, I will explain the Flags in AVR Atmega32 and how to write and assemble a program to add the following data and then use the simulator to examine the C, H, and Z flags after the execution of each addition. $92, $23, $66, $87, $F5.

Video:

Moreover, If you want to get a better understanding of Flags in AVR Atmega32 then please watch this Video given below

What does the AVR flag mean?

Status Register (SReg):

It is the flag register.However, also called the status register on the AVR microcontroller. It possesses eight bits. There are six conditional flags made up of these 8 bits. They are C, Z, N, V, S, and H.

The flag/status register is where the ALU performs all its actions. But If our response is zero, the Z flag will be raised, and the C flag will be raised if there is any carry.

Explanation Of  Flags in AVR Atmega32

Here’s an illustration to help me explain:

Assume that these are the eight bits D7, D6, D5, D4, D3, D2, D1, and D0.

8 bits in flags in avr atmega32

The H flag will rise if any operation is applied between two bits. for example, a carry from D3 to D4.

Additionally, the C flag will be raised if any operation is applied again between any two bits and the carry extends past the last bit, which is D7.

The Z flag will be set if you add some numbers, and the result is zero.

Code Explanation

. INCLUDE “M32DEF.INC”

Firstly, include a header file of Atmega32 in your code.

Then, we are going to add five values. For that purpose, we will load values in data direction registers.

LDI R16, 0X92 

Here we are storing value 92 in R16.

LDI R17, 0X23

So, we are storing value 23 in R17.

LDI R18, 0X66

Here we are storing value 66 in R18.

LDI R19, 0X87

We are storing value 87 in R19.

LDI R20, 0XF5

Here we are storing value F5 in R16.

ADD R16, R17

Both values are added, and the result is stored in R16.

Adding Registers values in Flags in AVR Atmega32

ADD R16, R18
ADD R16, R19
ADD R16, R20

In the next step, build the solution by clicking an F7 key.

Click on the F10 key, or open processor status by clicking on Debug option shown below.flags in avr atmega32 processor status You can see here a status register in which there are 8 bits.flags in avr status register By pressing the F10 key, the code will be step-by-step checked.

Moreover, When we moved over to look at R16’s value, we discovered that 92 values had been saved.processing Furthermore, we will see all values stored in Registers from R17 to R20.flags in avr atmega32 loading in registers

loading in register flags in avr atmega adding register valuesNow we are checking the added values, first checking the R16 value in which the value of R17 is added.adiing register values Now, we will check the status register again. It has raised two flags.

Moreover, One is the sign bit and the other is the Negative bit, which means its complement value.carry flag in status register In the next step, the carry flag is set in the status register.Because the sum of the last two bits is greater than an 8-bit register can hold, the MSB of the sum is shifted into the Carry Flag of the Status Register.half carry flag In the next step, half carry flag is raised, which means from D3 bit carry goes to D4 bit.flags raised in status register Finally, in R16 the value of R20 is stored, and three flags are raised. The blue color shows these flags were already grown and they are again raised. The carry flag was down, and it’s again up now.   carry flag again up

Code:

.INCLUDE "M32DEF.INC"
//   WE HAVE TO ADD THEM $92, $23, $66, $87, $F5
LDI R16, 0X92
LDI R17, 0X23
LDI R18, 0X66
LDI R19, 0X87
LDI R20, 0XF5
 
ADD R16,R17  
ADD R16,R18
ADD R16,R19
ADD R16,R20

Additionally, If you want to see the project Density Based Traffic Light Control in Atmega32 then visit our website.

 

 

Leave a Comment

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