AVR register calculator
An AVR register is 8 bits wide, and each bit has a name in the datasheet.
The macro _BV(n) is 1 shifted left by n, so it builds the mask for one bit.
The value (1 << RXEN0) | (1 << TXEN0) enables the receiver and the transmitter of the UART.
Try an AVR register in the bit editor
UCSR0B with the receiver and the transmitter enabled, which is 0x18. Edit the expression or select a bit to flip it.
7
6
5
4
3
2
1
0
0x18
hex · 8 bits
- binary
- 0b00011000
- value
- 24
An AVR register step by step
Enable the UART receiver and transmitter:
- RXEN0 is bit 4 of UCSR0B, so its mask is 1 << 4, which is 0x10.
- TXEN0 is bit 3 of UCSR0B, so its mask is 1 << 3, which is 0x08.
- Apply OR to combine them: 0x10 | 0x08 gives 0x18.
- Write UCSR0B = 0x18, or UCSR0B |= _BV(RXEN0) | _BV(TXEN0) to keep the other bits.
Bits an AVR project sets most often
| Register | Bit | Position | Purpose |
|---|---|---|---|
| UCSR0B | RXEN0 | 4 | Enable the UART receiver |
| UCSR0B | TXEN0 | 3 | Enable the UART transmitter |
| UCSR0B | RXCIE0 | 7 | Interrupt on a byte received |
| DDRB | DDB5 | 5 | Pin 13 of an Uno as an output |
| PORTB | PORTB5 | 5 | Drive that pin high |
| TCCR1B | CS11 | 1 | Timer 1 clock select |
Where an AVR register is used
An Arduino sketch that drops to registers writes DDRB and PORTB directly.
A datasheet names each bit, and the compiler turns the name into a position.
A register write inside an interrupt needs the other bits kept.
An AVR register: points to note
- Use |= to keep the other bits. A plain = clears every bit you did not name.
- The bit names are positions, not masks. RXEN0 is 4, so the mask needs 1 << RXEN0.
- The macro _BV(x) is 1 << x. Both forms appear in AVR code, and they mean the same thing.
- A register is 8 bits, so a mask above 0xFF has no effect on it.
The full bit editor
The full editor holds many values, evaluates expressions across them, and reads live values from a serial port.
Open this value in the full editorQuestions about an AVR register
- What is _BV in AVR code?
- The macro _BV(n) gives 1 shifted left by n, which is the mask for bit n.
- What is the value of UCSR0B for a UART?
- For the receiver and the transmitter it is 0x18, which is bit 4 and bit 3.
- Why does a plain assignment break other settings?
- An assignment writes every bit. Use |= to set bits and &= ~mask to clear them.