Skip to main content

Microcontrollers

Section 4.3 Status Register SREG

The status register (SREG) on the ATmega328P contains information about the result of the most recently executed instruction on the microcontroller. The information in SREG can be used to control the flow of the program being executed by way of conditional branch instructions (which is discussed in SectionΒ 15.3). All of the machine instructions that are capable of affecting the flags in SREG are listed in [16.15]. The status register contains eight bits (known as flags) as defined below.
Each individual flag can be set or cleared individually using a bit instruction as defined in [16.15]. In addition, the flags can be used with conditional (branch) instructions that can execute different code depending on whether a particular flag is set or clear.

Subsection 4.3.1 I: Global Interrupt Enable Flag (Bit 7)

The global interrupt enable flag (I) is used to enable and disable interrupts (as will be discussed in ChapterΒ 8). It must be set to globally enable interrupts and it must be cleared to globally disable interrupts. This bit is automatically cleared by hardware on the ATmega328P when an interrupt service routine is invoked, and is subsequently set again when the execution of the ISR has been completed.

Subsection 4.3.2 T: Bit Copy Storage Flag (Bit 6)

The bit copy storage flag (T) is used a single bit of storage on the microcontroller without having to make use of an entire 8-bit register.

Subsection 4.3.3 H: Half Carry Flag (Bit 5)

The half carry flag (H) is set when there is a carry from the least significant nibble (4 bits) in the most recently executed instruction. It is useful when dealing with binary coded decimal (BCD) numbers. In BCD arithmetic, the half carry has a value of 16. The most significant bit (MSB) of 16 (the number 1) belongs in the 10’s place of the BCD number. The remaining value of 6 needs to be added back in to the least significant nibble. An example of BCD addition where the half carry flag is set is shown in ExampleΒ 4.3.1.

Example 4.3.1. BCD addition with half carry.

Add 39 and 48 in BCD.
\begin{align*} \amp 1111 \amp 0000 \amp (\textrm{carries})\\ \amp 0011 \amp 1001 \amp (39_{10})\\ + \amp 0100 \amp 1000 \amp (48_{10})\\ = \amp 1000 \amp 0001 \amp (81_{10}, \textrm{wrong})\\ + \amp 0000 \amp 0110 \amp (6_{10})\\ = \amp 1000 \amp 0111 \amp (87_{10}, \textrm{correct}) \end{align*}
Because there is a half carry, adding 39 and 48 yields an incorrect sum unless 6 is added again into the least significant nibble of the result.
An algorithm for packed BCD addition (when two BCD digits are represented in an 8-bit binary number, as depicted in ExampleΒ 4.3.1) is provided in [16.1].

Subsection 4.3.4 S: Sign Flag (Bit 4)

The sign flag (S) is equal to an exclusive OR operation between the N and V flags. It gives the true sign of the result of the most recent operation, as described below.
  • N = 0 and V = 0: sign is positive (no overflow occurred)
  • N = 0 and V = 1: sign is negative (an overflow occurred, and the result is falsely positive)
  • N = 1 and V = 0: sign is negative (no overflow occurred)
  • N = 1 and V = 1: sign is positive (an overflow occurred, and the result is falsely negative)

Subsection 4.3.5 V: Two’s Complement Overflow Flag (Bit 3)

The two’s complement overflow flag (V) denotes whether or not overflow occurred with a signed arithmetic instruction. This bit is only generated with signed numbers. When set, the most recently executed operation resulted in an overflow condition. When cleared, the most recently executed operation did not result in an overflow. There are different equations for its generation based on the particular instruction that is in use.
In signed addition, an overflow occurs if the addition of two negative numbers results in a positive number, or when the addition of two positive numbers results in a negative number. Overflow is not possible when adding a positive to a negative number.
In signed subtraction, an overflow occurs if a positive number minus a negative number results in a negative number, or when a negative number minus a positive number results in a positive number. Overflow is not possible when subtracting a positive number from a positive number, or when subtracting a negative number from a negative number.
In signed multiplication, an overflow occurs if not all of the overflow bits are equal.

Subsection 4.3.6 N: Negative Flag (Bit 2)

The negative flag (B) is used to determine if a signed value is positive or negative. This bit is only generated with signed numbers. The negative flag is the MSB of the result of the most recently executed operation. A value of 0 indicates a positive number, and a value of 1 indicates a negative number.

Subsection 4.3.7 Z: Zero Flag (Bit 1)

The zero flag (Z) is set if the result of the most recently executed operation is zero.

Subsection 4.3.8 C: Carry Flag (Bit 0)

This carry flag (C) is set if the most recently executed operation resulted in a carry or a borrow. This flag is particularly useful when adding numbers that require more than 8-bits to store. For example, when adding 16-bit numbers, the microcontroller must work 8-bits at a time due to the size limitation of the CPU registers. A carry on the least significant byte (carry flag of 1) indicates that one must be added to the most significant byte.
In unsigned addition and subtraction, the presence of a carry or borrow indicates overflow.