• Artificial Intelligence
  • Embedded Systems
  • Quantitative Trading

ATmega328P Register Map

Complete register reference for bitwise operations on ATmega328P microcontroller

Register Address Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
SREG 0x3F I T H S V N Z C
MCUCR 0x35 - BODS BODSE PUD - - IVSEL IVCE
MCUSR 0x34 - - - - WDRF BORF EXTRF PORF
WDTCSR 0x60 WDIF WDIE WDP3 WDCE WDE WDP2 WDP1 WDP0
PRR 0x64 PRTWI PRTIM2 PRTIM0 - PRTIM1 PRSPI PRUSART0 PRADC
CLKPR 0x61 CLKPCE - - - CLKPS3 CLKPS2 CLKPS1 CLKPS0
OSCCAL 0x66 CAL7 CAL6 CAL5 CAL4 CAL3 CAL2 CAL1 CAL0
EICRA 0x69 - - - - ISC11 ISC10 ISC01 ISC00
EIMSK 0x1D - - - - - - INT1 INT0
EIFR 0x1C - - - - - - INTF1 INTF0
PCICR 0x68 - - - - - PCIE2 PCIE1 PCIE0
PCIFR 0x1B - - - - - PCIF2 PCIF1 PCIF0
TCCR0A 0x24 COM0A1 COM0A0 COM0B1 COM0B0 - - WGM01 WGM00
TCCR0B 0x25 FOC0A FOC0B - - WGM02 CS02 CS01 CS00
TIMSK0 0x6E - - - - - OCIE0B OCIE0A TOIE0
TCCR1A 0x80 COM1A1 COM1A0 COM1B1 COM1B0 - - WGM11 WGM10
TCCR1B 0x81 ICNC1 ICES1 - WGM13 WGM12 CS12 CS11 CS10
TCCR1C 0x82 FOC1A FOC1B - - - - - -
TIMSK1 0x6F - - ICIE1 - - OCIE1B OCIE1A TOIE1
TCCR2A 0xB0 COM2A1 COM2A0 COM2B1 COM2B0 - - WGM21 WGM20
TCCR2B 0xB1 FOC2A FOC2B - - WGM22 CS22 CS21 CS20
TIMSK2 0x70 - - - - - OCIE2B OCIE2A TOIE2
ASSR 0xB6 - EXCLK AS2 TCN2UB OCR2AUB OCR2BUB TCR2AUB TCR2BUB
SPCR 0x2C SPIE SPE DORD MSTR CPOL CPHA SPR1 SPR0
SPSR 0x2D SPIF WCOL - - - - - SPI2X
UCSR0A 0xC0 RXC0 TXC0 UDRE0 FE0 DOR0 UPE0 U2X0 MPCM0
UCSR0B 0xC1 RXCIE0 TXCIE0 UDRIE0 RXEN0 TXEN0 UCSZ02 RXB80 TXB80
UCSR0C 0xC2 UMSEL01 UMSEL00 UPM01 UPM00 USBS0 UCSZ01 UCSZ00 UCPOL0
TWCR 0xBC TWINT TWEA TWSTA TWSTO TWWC TWEN - TWIE
TWSR 0xB9 TWS7 TWS6 TWS5 TWS4 TWS3 - TWPS1 TWPS0
ADCSRA 0x7A ADEN ADSC ADATE ADIF ADIE ADPS2 ADPS1 ADPS0
ADCSRB 0x7B - ACME - - - ADTS2 ADTS1 ADTS0
ADMUX 0x7C REFS1 REFS0 ADLAR - MUX3 MUX2 MUX1 MUX0
DIDR0 0x7E - - ADC5D ADC4D ADC3D ADC2D ADC1D ADC0D
DIDR1 0x7F - - - - - - AIN1D AIN0D
ACSR 0x30 ACD ACBG ACO ACI ACIE ACIC ACIS1 ACIS0
EECR 0x1F - - EEPM1 EEPM0 EERIE EEMPE EEPE EERE
SPMCSR 0x37 SPMIE RWWSB - RWWSRE BLBSET PGWRT PGERS SELFPRGEN

Common Bitwise Operations

Bitwise operations are fundamental for embedded programming, allowing direct manipulation of hardware registers at the bit level.

Set a Bit

REGISTER |= (1 << BIT_NUMBER);

Sets the specified bit to 1 without affecting other bits

Example: PORTB |= (1 << PB5);
Sets bit 5 of PORTB to HIGH

Clear a Bit

REGISTER &= ~(1 << BIT_NUMBER);

Clears the specified bit to 0 without affecting other bits

Example: PORTB &= ~(1 << PB5);
Sets bit 5 of PORTB to LOW

Toggle a Bit

REGISTER ^= (1 << BIT_NUMBER);

Flips the specified bit (0→1 or 1→0)

Example: PORTB ^= (1 << PB5);
Toggles bit 5 of PORTB

Test a Bit

if (REGISTER & (1 << BIT_NUMBER))

Checks if the specified bit is set (returns non-zero if true)

Example: if (PINB & (1 << PB5))
Tests if bit 5 of PINB is HIGH

Set Multiple Bits

REGISTER |= (1 << BIT1) | (1 << BIT2);

Sets multiple bits simultaneously

Example: DDRB |= (1 << PB4) | (1 << PB5);
Sets bits 4 and 5 of DDRB as outputs

Clear Multiple Bits

REGISTER &= ~((1 << BIT1) | (1 << BIT2));

Clears multiple bits simultaneously

Example: PORTB &= ~((1 << PB4) | (1 << PB5));
Clears bits 4 and 5 of PORTB

Register Categories

System Control

SREG, MCUCR, MCUSR, PRR, CLKPR

Interrupts

EICRA, EIMSK, EIFR, PCICR, PCIFR

Timers

TCCR0A/B, TCCR1A/B/C, TCCR2A/B, TIMSK0/1/2

Communication

SPCR, SPSR (SPI), UCSR0A/B/C (USART), TWCR, TWSR (TWI)

ADC

ADCSRA, ADCSRB, ADMUX, DIDR0, DIDR1

Memory

EECR (EEPROM), SPMCSR (Flash)

Pro Tips

  • Use bit-shift operators: Always use (1 << n) instead of hardcoded values for better readability
  • Read-Modify-Write: Most register operations require reading the current value, modifying it, then writing back
  • Atomic operations: For interrupt-sensitive code, disable interrupts during read-modify-write sequences
  • Datasheet reference: Always consult the ATmega328P datasheet for specific bit functions and restrictions
  • Volatile keyword: Use volatile for variables shared between ISRs and main code