// FREE MINI-COURSE · { }

Embedded C

Embedded C from Zero — Learn the C that real embedded engineers write — straight on the chip, one 20-second lesson at a time.

5 of 26 lessons out · next: What is a register?

▶ Start lesson 1
  1. 01 What is Embedded C?C that runs straight on a microcontroller, no operating system — Arduino code is C++ wrapped around the same chip. EP.19 · 20 s
  2. 02 Your first program: main() + while(1)Every Embedded C program = #include, int main(void), setup once, then a while(1) loop that never ends. EP.34 · 20 s
  3. 03 Data types on an 8-bit chipuint8_t holds 0–255, uint16_t 0–65535; pick the smallest that fits; 255 + 1 wraps to 0. EP.54 · 24 s
  4. 04 Binary & hex: one byte, three waysOne byte = 8 bits; 0b00100000 = 0x20 = 32 are the same number. EP.55 · 24 s
  5. 05 #define: name your pins#define swaps names for numbers before compiling (LED_PIN 5), so code reads like English. EP.56 · 24 s
  6. 07 PLANNEDLED ON with 2 lines of registersDDRB |= (1<<5); PORTB |= (1<<5); lights D13 (= PB5) without digitalWrite.
  7. 08 PLANNEDBit shift: 1 << n1 << n makes a mask with only bit n set (1<<5 = 0b00100000).
  8. 09 PLANNEDSET a bit with |=OR-ing a mask turns one bit on and leaves the other 7 untouched.
  9. 10 PLANNEDCLEAR a bit with &= ~AND with the inverted mask turns one bit off, others untouched.
  10. 11 PLANNEDTOGGLE with ^= → blinkXOR flips one bit; with _delay_ms(500) it blinks the LED.
  11. 12 PLANNEDREAD a pin: button on D2Pull-up via PORTD, read PIND & (1<<PIND2); pressed = 0 (LOW).
  12. 13 PLANNEDBit tricks cheat sheetSET / CLEAR / TOGGLE / READ as 4 reusable macros.
  13. 14 PLANNEDPointers in 20 secondsA pointer stores an address; *p reads/writes the value there.
  14. 15 PLANNEDRegisters are just memoryPORTB is address 0x25 — *(volatile uint8_t*)0x25 does the same as PORTB.
  15. 16 PLANNEDvolatile: why it mattersvolatile stops the compiler from caching a value an interrupt or the hardware can change.
  16. 17 PLANNEDFlash, RAM, EEPROMATmega328P: 32 KB flash (program), 2 KB RAM (lost at power-off), 1 KB EEPROM (kept).
  17. 18 PLANNEDWhy _delay_ms freezes your chipDuring a delay the CPU does nothing else — a button press is missed.
  18. 19 PLANNEDTimers: a counter driven by the clockTimer1 with ÷1024 counts 15 625 ticks every second.
  19. 20 PLANNEDExact 1 s blink, no delay (CTC)OCR1A = 15624 + CTC mode → flag every 1 s while the CPU stays free.
  20. 21 PLANNEDInterrupts: the button calls youINT0 on D2 + ISR() runs the instant the button falls; sei() turns interrupts on.
  21. 22 PLANNEDBlink in the backgroundISR(TIMER1_COMPA_vect) toggles the LED every second while main() does other work.
  22. 23 PLANNEDPWM with registersTimer0 fast PWM on D6: OCR0A = 128 → 50 % brightness.
  23. 24 PLANNEDADC: read a knobADMUX + ADCSRA turn the voltage on A0 into 0–1023.
  24. 25 PLANNEDUART: talk to your PCUBRR0 = 103 → 9600 baud; send "Hello" byte by byte through UDR0.
  25. 26 PLANNEDMini project: knob → LED + PCADC + PWM + UART together: the knob sets brightness and the value prints on the PC.