Welcome 👋
This interactive study guide is designed to help you master AVR Embedded Systems and ATmega328P architecture. Navigate through different learning styles using the modes below:
- 📖 Tutorial Mode: Walk through guided Socratic explanations, interactive code snippets, and review core lecture concepts step-by-step.
- 🔧 Interactive Coding: Practice writing and arranging AVR Assembly and C code with real-time check feedback.
- ⚡ Practice Quiz: Challenge yourself with exam-style multiple-choice questions, detailed explanations, and track your mistakes.
- ⚡🔧 Blitz Coding: Test your skills with customized, randomized coding tasks across multiple topics to build speed and accuracy.
💡 Pro-Tip: Press Ctrl+K (or click the search 🔍 icon next to Home) to search for anything instantly across tutorials, quizzes, and coding tasks. You can also view reference answers using the new 🔓 Solution button during coding tasks!
🍅 Focus Timer
⏳ Final Exam Countdown
📋 Exam Reference Sheet
AVR Architecture & Memories (ATmega328P)
Harvard Architecture: Separate program bus (Flash) and data bus (SRAM/Registers). Allows simultaneous instruction fetch and data access.
Memory Sizes:
- Flash (Program Memory): 32 KB (16K x 16-bit instructions, 2-byte/4-byte size)
- SRAM (Data Memory): 2 KB (General Purpose R0-R31, I/O registers, internal SRAM)
- EEPROM (Non-volatile): 1 KB (Data storage surviving reset/power loss)
Registers: 32 general-purpose registers (R0-R31). Only R16-R31 support immediate loading (LDI). X=R27:R26, Y=R29:R28, Z=R31:R30 are 16-bit pointer registers.
AVR Assembly Basics
General syntax: INSTRUCTION Destination, Source
Core Instructions:
LDI Rd, K: Load constant K (0-255) into Rd (R16-R31).LDS Rd, k: Load byte from SRAM address k (16-bit address).STS k, Rr: Store register Rr byte to SRAM address k.IN Rd, A: Read byte from I/O register A (address 0-63).OUT A, Rr: Write register Rr to I/O register A (address 0-63).MOV Rd, Rr: Copy register Rr to Rd.ADD Rd, Rr: Add Rr to Rd (updates SREG flags: C, Z, N, V, H).SUB Rd, Rr: Subtract Rr from Rd (updates flags).INC Rd/DEC Rd: Increment / Decrement register Rd.CP Rd, Rr: Compare Rd with Rr (performs subtraction, updates flags, doesn't write back Rd).
SREG (Status Register) Flags: I T H S V N Z C (C=Carry, Z=Zero, N=Negative, V=Overflow, S=Sign, H=Half-Carry, I=Global Interrupt Enable).
Jump & Call Instructions (Program Flow)
Unconditional Jumps:
RJMP k: Relative jump (offset ±2K instructions, 2-byte instruction, 2 cycles).JMP k: Absolute jump to 22-bit address (4-byte instruction, 3 cycles).IJMP: Indirect jump (jumps to the address stored in the Z register R31:R30).
Subroutine Calls:
RCALL k/CALL k: Pushes Program Counter (PC) (2 bytes) onto Stack, decrements SP by 2, then jumps.RET: Return from subroutine. Pops 2 bytes from Stack into PC, increments SP by 2.RETI: Return from Interrupt Service Routine. Same as RET but also sets SREG global interrupt bit (I = 1) automatically.
Stack Pointer Initialization (RAMEND = 0x08FF):
Conditional Branches: BREQ k (branch if Z=1), BRNE k (branch if Z=0), BRCS k (branch if C=1), BRCC k (branch if C=0). Relative offset range: -64 to +63 instructions.
GPIO & Bit Fiddling (ATmega328P Port Registers)
Each port (B, C, D) has three registers: DDRx (Direction), PORTx (Output / Pull-up), and PINx (Input Pin Value - Read Only).
| DDRx Bit | PORTx Bit | Pin Config | Description |
|---|---|---|---|
| 0 (Input) | 0 | Input, High-Z | Pin is floating (high impedance). Input reading is volatile. |
| 0 (Input) | 1 | Input, Pull-up | Internal 20K-50K pull-up resistor enabled. Pin idles HIGH. |
| 1 (Output) | 0 | Output, LOW | Pin is driven to GND (0V). Can sink current. |
| 1 (Output) | 1 | Output, HIGH | Pin is driven to VCC (5V). Can source current. |
Bitwise Trinity in C:
- Set bit:
PORTB |= (1 << 5); - Clear bit:
PORTB &= ~(1 << 5); - Toggle bit:
PORTB ^= (1 << 5); - Read pin:
if ((PINB & (1 << 3)) == 0) { ... }
Directives in Assembly: SBI ioreg, bit (Set bit in I/O reg), CBI ioreg, bit (Clear bit), SBIS ioreg, bit (Skip next instruction if I/O bit is set), SBIC ioreg, bit (Skip if clear).
Timer / Counter (Truth Tables & Calculations)
Calculations:
- Delay tick:
- Preload Value (Normal Mode):
- Compare Value (CTC Mode):
CTC & Normal Mode Selection Cheat Sheet:
- Timer 0 / Timer 2 (8-bit) Normal Mode:
TCCR0A = 0x00;(WGM02:00 = 000) - Timer 0 / Timer 2 (8-bit) CTC Mode:
TCCR0A = (1<<WGM01);(WGM02:00 = 010) - Timer 1 (16-bit) Normal Mode:
TCCR1A = 0x00; TCCR1B = 0x00;(WGM13:10 = 0000) - Timer 1 (16-bit) CTC Mode (TOP=OCR1A):
TCCR1A = 0x00; TCCR1B = (1<<WGM12);(WGM13:10 = 0100)
WGM (Waveform Generation Mode) Truth Tables:
Timer 0 and Timer 2 (8-bit):
| Mode | WGM02 | WGM01 | WGM00 | Mode Description | TOP | TOV Flag Set on |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | Normal (Upcounting) | 0xFF | MAX (0xFF) |
| 2 | 0 | 1 | 0 | CTC (Clear Timer on Compare Match) | OCR0A | MAX (0xFF) |
| 3 | 0 | 1 | 1 | Fast PWM | 0xFF | MAX (0xFF) |
| 7 | 1 | 1 | 1 | Fast PWM | OCR0A | TOP |
Timer 1 (16-bit):
| Mode | WGM13 | WGM12 | WGM11 | WGM10 | Mode Description | TOP | TOV Flag Set on |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | Normal (Upcounting) | 0xFFFF | MAX (0xFFFF) |
| 4 | 0 | 1 | 0 | 0 | CTC (Clear Timer on Compare Match) | OCR1A | MAX (0xFFFF) |
| 14 | 1 | 1 | 1 | 0 | Fast PWM | ICR1 | TOP |
| 15 | 1 | 1 | 1 | 1 | Fast PWM | OCR1A | TOP |
Clock Source / Prescaler Selector (CS02:CS00 for Timer 0 & 1):
| CSn2 | CSn1 | CSn0 | Prescaler / Clock Source Selection |
|---|---|---|---|
| 0 | 0 | 0 | No clock source (Timer/Counter stopped) |
| 0 | 0 | 1 | clk_I/O (No prescaling) |
| 0 | 1 | 0 | clk_I/O / 8 |
| 0 | 1 | 1 | clk_I/O / 64 |
| 1 | 0 | 0 | clk_I/O / 256 |
| 1 | 0 | 1 | clk_I/O / 1024 |
| 1 | 1 | 0 | External clock on Tn pin (Falling Edge) |
| 1 | 1 | 1 | External clock on Tn pin (Rising Edge) |
Interrupts (Vectors & Registers)
Registers:
SREG(bit 7 - I): Global Interrupt Enable. Set bysei(), cleared bycli().EIMSK: External Interrupt Mask Register (bit 0 = INT0 enable, bit 1 = INT1 enable).EICRA: External Interrupt Control Register A. Configures trigger edges:
| ISCn1 | ISCn0 | Trigger Mode Description |
|---|---|---|
| 0 | 0 | Low level of INTn pin generates interrupt request |
| 0 | 1 | Any logical change on INTn pin generates interrupt request |
| 1 | 0 | Falling edge of INTn pin generates interrupt request |
| 1 | 1 | Rising edge of INTn pin generates interrupt request |
Selected Vector Table Addresses (in Flash):
- Reset:
0x0000 - INT0:
0x0002 - INT1:
0x0004 - TIMER1 COMPA:
0x0016 - TIMER1 OVF:
0x001A - TIMER0 OVF:
0x0020
UART / Serial Communication (USART0)
Baud Rate Divisor Formulas (Async Normal Mode, U2X0 = 0):
Baud Rate Divisor Formulas (Async Double Speed Mode, U2X0 = 1):
Core Registers:
UDR0: USART Data Register (Read = RX buffer, Write = TX buffer).UCSR0A: Status register. Bits:RXC0(Receive Complete flag),TXC0(Transmit Complete flag),UDRE0(USART Data Register Empty flag - 1 means ready to accept new byte).UCSR0B: Control register B. Bits:RXCIE0(RX Complete Interrupt Enable),RXEN0(Receiver Enable),TXEN0(Transmitter Enable),UCSZ02(Character Size bit 2).UCSR0C: Control register C. Bits:UMSEL01:UMSEL00(00 = Async, 01 = Sync),UPM01:UPM00(Parity: 00=None, 10=Even, 11=Odd),USBS0(Stop bits: 0=1-bit, 1=2-bit),UCSZ01:UCSZ00(Character size: 11 = 8-bit).
ADC (Analog-to-Digital Converter)
Voltage to Digital Conversion Formula:
Core Registers:
ADMUX: Channel and Reference Selection.REFS1:REFS0: 00 = AREF pin, 01 = AVCC with capacitor at AREF, 11 = Internal 1.1V ref.ADLAR: Left Adjust Result. (0 = Right adjusted to ADCL/ADCH, 1 = Left adjusted).MUX3:MUX0: Analog Channel Select (0000 = ADC0, 0001 = ADC1, ..., 0101 = ADC5).
ADCSRA: Control and Status Register A.ADEN: ADC Enable. Must be 1 to use ADC.ADSC: ADC Start Conversion. Write 1 to start conversion.ADIF: ADC Interrupt Flag. Becomes 1 when conversion is complete. Clear by writing 1.ADIE: ADC Interrupt Enable.ADPS2:ADPS0: Prescaler Select bits. Divides system clock to ADC clock (must be between 50kHz and 200kHz for max accuracy). 111 = clk/128, 110 = clk/64.
PWM & DC Motor Control (Not in Exam)
PWM Frequency Formulas:
Duty Cycle Calculations:
Compare Output Mode in Fast PWM (COM0A1:COM0A0):
| COM0A1 | COM0A0 | Compare Output Mode Description |
|---|---|---|
| 0 | 0 | Normal port operation, OC0A disconnected |
| 1 | 0 | Clear OC0A on Compare Match, set OC0A at BOTTOM (non-inverting mode) |
| 1 | 1 | Set OC0A on Compare Match, clear OC0A at BOTTOM (inverting mode) |