0% found this document useful (0 votes)
796 views1 page

AVR SREG: Status Register Overview

The SREG register is the status register for the AVR microcontroller. It contains 8 status flags - I, T, H, S, V, N, Z, and C - that provide information about the results of arithmetic logic operations. The I flag controls global interrupts. The T flag is used for bit copy instructions. The H, S, V, N, Z, and C flags indicate results like half carry, sign, overflow, negative, zero, and full carry from operations. All flags are readable and writable.

Uploaded by

salar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
796 views1 page

AVR SREG: Status Register Overview

The SREG register is the status register for the AVR microcontroller. It contains 8 status flags - I, T, H, S, V, N, Z, and C - that provide information about the results of arithmetic logic operations. The I flag controls global interrupts. The T flag is used for bit copy instructions. The H, S, V, N, Z, and C flags indicate results like half carry, sign, overflow, negative, zero, and full carry from operations. All flags are readable and writable.

Uploaded by

salar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

6.3.

1 SREG – AVR Status Register


The AVR status register – SREG – is defined as:
Bit 7 6 5 4 3 2 1 0
0x3F (0x5F) I T H S V N Z C SREG
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0

• Bit 7 – I: Global Interrupt Enable


The global interrupt enable bit must be set for the interrupts to be enabled. The individual interrupt enable control is then
performed in separate control registers. If the global interrupt enable register is cleared, none of the interrupts are enabled
independent of the individual interrupt enable settings. The I-bit is cleared by hardware after an interrupt has occurred, and is
set by the RETI instruction to enable subsequent interrupts. The I-bit can also be set and cleared by the application with the
SEI and CLI instructions, as described in the instruction set reference.

• Bit 6 – T: Bit Copy Storage


The bit copy instructions BLD (bit LoaD) and BST (Bit STore) use the T-bit as source or destination for the operated bit. A bit
from a register in the register file can be copied into T by the BST instruction, and a bit in T can be copied into a bit in a
register in the register file by the BLD instruction.

• Bit 5 – H: Half Carry Flag


The half carry flag H indicates a half carry in some arithmetic operations. Half carry Is useful in BCD arithmetic. See Section
“” on page 281 for detailed information.

• Bit 4 – S: Sign Bit, S = N V


The S-bit is always an exclusive or between the negative flag N and the two’s complement overflow flag V. See Section “”
on page 281 for detailed information.

• Bit 3 – V: Two’s Complement Overflow Flag


The two’s complement overflow flag V supports two’s complement arithmetics. See Section “” on page 281 for detailed
information.

• Bit 2 – N: Negative Flag


The negative flag N indicates a negative result in an arithmetic or logic operation. See Section “” on page 281 for detailed
information.

• Bit 1 – Z: Zero Flag


The zero flag Z indicates a zero result in an arithmetic or logic operation. See Section “” on page 281 for detailed
information.

• Bit 0 – C: Carry Flag


The carry flag C indicates a carry in an arithmetic or logic operation. See Section “” on page 281 for detailed information.

ATmega328P [DATASHEET] 11
7810D–AVR–01/15

Common questions

Powered by AI

The Global Interrupt Enable bit (I-bit) in the AVR Status Register (SREG) is crucial for managing interrupts in ATmega328P microcontrollers. It controls whether interrupts are enabled globally. When this bit is set, the microcontroller can respond to individually enabled interrupts. Conversely, if this bit is cleared, no interrupts can occur, regardless of the individual interrupt enable settings. This bit ensures that the microcontroller only handles interrupts when it is ready and prevents unintended interrupt execution. The I-bit is automatically cleared by hardware after an interrupt occurs, ensuring control returns to the normal program flow, and is set again by the RETI instruction, preparing the system for future interrupts .

The Half Carry Flag (H) in the AVR Status Register plays an essential role in supporting binary-coded decimal (BCD) arithmetic operations in the ATmega328P microcontroller. It signals a carry from the lower four bits to the higher four bits in 8-bit binary addition operations, which is particularly crucial for BCD arithmetic where numbers are represented in decimal form. This flag allows for accurate summation by detecting mid-byte carries, thus ensuring correct BCD calculations and supporting arithmetic operations that mimic decimal calculations in binary systems .

The Carry Flag (C) in the SREG is instrumental for managing multi-byte arithmetic operations in the ATmega328P microcontroller. It indicates whether an arithmetic operation results in a carry out from the most significant bit, which is essential during multi-byte operations where the carry affects the higher byte. When a carry is generated at the lower byte in an addition operation, this flag can be used to add an extra 1 to the next byte, maintaining arithmetic accuracy across bytes. Its role is critical in implementing large number arithmetic and multi-byte data processing by ensuring proper carry propagation .

In the ATmega328P microcontroller, the Zero Flag (Z) in the SREG is used to determine if the result of an arithmetic or logic operation yields a zero. Its value impacts conditional branching, allowing programs to branch based on operational results. For instance, if a result is zero, certain instructions, such as branch if equal (BEQ), will execute, altering the program flow accordingly. This flag is critical in loops and conditional statements for decisions based on equality, thereby facilitating precise control structures within embedded software .

The Sign Bit (S) in the SREG of the ATmega328P microcontroller indicates the sign of the result of an arithmetic operation. It is mathematically formulated as an exclusive OR (XOR) operation between the Negative Flag (N) and the Two’s Complement Overflow Flag (V). The S-bit assists in detecting whether a result is positive or negative; if the XOR operation yields a 1, it indicates a negative result, and if it yields a 0, a positive result. This comprehension of sign assists in controlling flow in conditional operations dependent on the sign of calculation results .

A programmer might directly manipulate the Global Interrupt Enable bit using SEI (Set Global Interrupt Flag) and CLI (Clear Global Interrupt Flag) instructions in scenarios that require precise timing or context control in the ATmega328P microcontroller. For instance, during critical sections of code where interrupts might disrupt the process, such as data processing routines or peripheral management, the CLI instruction can be used to disable interrupts temporarily. Conversely, SEI is used to re-enable interrupts once the critical section is completed, ensuring normal interrupt service is resumed. This direct control helps maintain system stability and synchrony in embedded applications .

The Bit Copy Storage (T) in the SREG is used in conjunction with BLD (bit LoaD) and BST (Bit STore) instructions to manipulate individual bits. The T-bit serves as either a temporary storage or transfer point during these operations. For example, a bit from a register can be copied into T using the BST instruction, and later, this bit can be transferred into another register using the BLD instruction. This facilitation of bit manipulations enables programmers to efficiently perform low-level data handling operations, which optimizes code, especially in situations needing precise control of individual bits .

The Two’s Complement Overflow Flag (V) in the SREG is significant for managing arithmetic operations in two’s complement arithmetic used by the ATmega328P. This flag indicates whether an overflow condition has occurred in signed arithmetic operations. It helps in correctly interpreting the result of arithmetic operations by signaling if the calculated value went beyond the limits of representable positive or negative numbers, thus aiding programmers in debugging and handling such events appropriately .

You might also like