• Microprocessor Video Tutorials

Conditional Branch Instructions in AVR Microcontroller



In AVR microcontrollers, Conditional Branch Instructions play an important role in various decision-making processes. The main function of conditional branch instructions in AVR microcontrollers is to allow the program to branch out of a loop depending on a specific condition. This is an important process for improving the control flow and efficiency in embedded systems.

Conditional Branch Instructions in AVR Microcontroller

In this chapter, we will learn about different conditional branch instructions in AVR microcontrollers. Before that let's get an overview of various registers in AVR microcontrollers.

Registers in AVR Microcontrollers

Before going to discuss about conditional branch instructions, it is important to understand different types of registers in AVR microcontrollers, which are explained as follows −

Status Register (SReg)

In AVR microcontrollers, the status register is a flag register that holds flags indicating the results of arithmetic and logical operations, and determine when a conditional branch should occur.

It is an 8-bit register with only 6-bits acting as the conditional flags which are responsible for influencing the branch instructions. These 6 flags get updated after execution of every arithmetic or logical instruction.

The following table provides a brief overview of these six conditional flag bits

Flag Description Condition
C (Carry Flag) This flag indicates a carry out from the D7 bit in arithmetic operations. This flag gets set when there is a carry out after an 8-bit addition or subtraction.
Z (Zero Flag) The zero flag reflects the result of an arithmetic or logical operation. The zero flag is set or Z = 1 if the result is zero, and the flag is cleared or Z = 0 if the result is not zero.
N (Negative Flag) The negative flag indicates the sign of a signed number operation. The D7 bit is used as the sign bit for the binary representation of signed numbers. If the D7 bit is zero, then N = 0 and the result is positive. If the D7 bit is one, then N = 1 and the result is negative.
V (Overflow Flag) The overflow flag identifies the overflow in signed number operation. This flag gets set when the result of the singed number operation is too large and causes high-order bit to overflow into the sign bit.
S (Sign Flag) The sign flag is obtained from N XOR V i.e., it is the result of XOR of N flag and V flag. It is used to determine the sign of a signed operation result.
H (Half Carry Flag) The half-carry flag is used in instructions that perform operations on BCDs. This flag gets set if there is a carry from D3 to D4 during an addition or subtraction operation, otherwise this bit is cleared.

This is all about different types of registers in AVR microcontrollers. Let's now discuss about conditional branch instructions in AVR microcontrollers.

Conditional Branch Instructions in AVR Microcontrollers

In AVR microcontrollers, the conditional branch instructions are those instructions that used to branch out of a loop. In other words, these instructions decide whether to jump to another portion of the program depending on a specific condition or a flag status. These instructions provide an optimized execution of a program by avoiding unnecessary computations when a condition is met.

The following highlights some of the important conditional branch instructions used in AVR microcontrollers −

Instruction Description Branch Condition / Flag Status
BREQ Branch if equal Branch if Z = 1 (Result is zero)
BRNE Branch if not equal Branch if Z = 0 (Result is non-zero)
BRSH Branch if same or higher (unsigned) Branch if C = 0
BRLO Branch if lower (unsigned) Branch if C = 1
BRLT Branch if less than (signed) Branch if S = 1
BRGE Branch if greater than or equal (signed) Branch if S = 0
BRVS Branch if overflow flag is set Branch if V = 1
BRVC Branch if overflow flag is clear Branch if V = 0

Working of Conditional Branch Instructions

The following points explain how these conditional branch instructions executes −

  • First of all, each conditional branch instruction checks the status of a specific flag in the status register (SReg).
  • Then, if the condition is satisfied, the program branches or jumps to a specific part.
  • If the condition is not satisfied, then program executes as it is going.

Example

The following example demonstrates the use of BREQ instruction in AVR microcontroller −

LDI R16, 0x010;	Load R16 with value 10
LDI R17, 0x07;	Load R17 with value 7
CP R16, R17;	Compare R16 and R17
BREQ MATCH_FOUND;	Branch to MATCH_FOUND if they are equal
; Code continues if R16  R17
MATCH_FOUND:
; Code executes if R16 = R17

Applications of Conditional Branch Instructions in AVR

The following are some real-world applications of conditional branch instructions in AVR microcontrollers −

  • Conditional branch instructions are used to exit a loop in a program when a specific condition is met.
  • They are also used to for implementing decision making logics.
  • They are also used for handling interrupts or respond to hardware events.
  • These instructions also manage different operational states in embedded systems.

Conclusion

In this chapter, we covered the conditional branch instructions in AVR microcontrollers and their applications in real-world. In conclusion, conditional branch instructions are used to decide whether to branch out a loop in a program whenever a specific condition is met.

Advertisements