Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Last updated: Friday 13 November 2026 @ 17:29:40
Embedded systems · AVR

Introduction to Embedded C Programming

Learn how embedded software interacts with microcontroller hardware, from processor architecture and physical pins to registers and direct port manipulation. The examples introduce the ATmega328P and the avr-gcc toolchain used in subsequent practicals.

Embedded CAVRATmega328PRegistersGPIO
Select appropriate languages

Explain why C remains widely used in embedded development and identify alternatives.

Understand AVR hardware

Distinguish physical package pins, microcontroller port pins and Arduino pin labels.

Work with registers

Describe how DDR, PORT and PIN registers configure and control digital I/O.

Read embedded C

Interpret bitwise operations in simple ATmega328P GPIO programmes.

Languages used in embedded systems

Different embedded platforms favour different programming languages, depending on hardware resources, development requirements and safety constraints.

LanguageTypical characteristics
Assembly (ASM)Direct instruction-level control; highly specific to a processor architecture.
CLow-level access and relatively small runtime overhead; widely used for microcontrollers.
C++Abstractions and object-oriented techniques alongside low-level hardware access.
MicroPython / CircuitPythonPython implementations designed for supported microcontroller platforms.
RustCompile-time ownership and borrowing checks, with opportunities for low-level programming.
Ada / SPARKUsed in systems prioritising reliability and formal verification.
Java (embedded JVMs)Possible where the device can support a suitable virtual machine and runtime.

Why use Embedded C?

C is particularly useful when software must control hardware directly:

  • Direct hardware access: Device-specific headers expose registers and peripherals.
  • Portability: Standard C can be reused, although register-level code often needs adaptation between devices.
  • Efficiency: Compiled programmes can be small and fast, with relatively predictable execution costs.
  • Standardisation: ISO C provides a common language foundation, supplemented by target-specific facilities.

The ATmega328P microcontroller

The ATmega328P is an 8-bit microcontroller based on the AVR architecture, originally developed by Atmel and now part of Microchip Technology. It is used on several Arduino boards, notably the classic Arduino Uno and Nano.

Its AVR instruction set contains 131 instructions, many of which complete in one clock cycle. At a 16 MHz clock rate, the theoretical peak throughput approaches 16 MIPS for suitable instructions; actual performance varies with the instruction mix.

Packages and pinouts

The ATmega328P is available in multiple packages, including:

  • 32-lead TQFP (Thin Quad Flat Package)
  • 28-pin SPDIP (Shrink Plastic Dual In-line Package)
  • 32-pad VQFN (Very Thin Quad Flat No-leads)
  • 28-pad VQFN/MLF variants
ATmega328P integrated circuit package variants

Figure 1. ATmega328P integrated circuit packages.

ATmega328P pinout diagram

Figure 2. ATmega328P pinout.

Port and pin mapping

The following table reproduces the supplied mapping. The physical ATmega pin column refers to the 28-pin package; other packages have different physical pin numbers. Arduino labels are board-level conventions.

PORTPORT pinArduino pinATmega pin (28-pin package)
PORTBPB0814
PORTBPB1915
PORTBPB21016
PORTBPB31117
PORTBPB41218
PORTBPB51319
PORTBPB6209
PORTBPB72110
PORTCPC014/A023
PORTCPC115/A124
PORTCPC216/A225
PORTCPC317/A326
PORTCPC418/A427
PORTCPC519/A528
PORTCPC622/RESET1
PORTDPD00/RXD2
PORTDPD11/TXD3
PORTDPD224
PORTDPD335
PORTDPD446
PORTDPD5511
PORTDPD6612
PORTDPD7713

What are registers?

A register is a small storage location used by a processor or peripheral. On the ATmega328P, software accesses hardware control and status registers through defined I/O addresses. Some registers control how GPIO pins operate; others configure timers, communication interfaces and analogue-to-digital conversion.

Registers provide:

  1. Hardware control: Configure inputs, outputs and peripherals.
  2. Low-level access: Read or update hardware state directly.
  3. Precise configuration: Change individual bits without rewriting unrelated settings.

Example: Data Direction Register (DDR)

Each GPIO port has an associated Data Direction Register, such as DDRB. Setting a bit makes the corresponding pin an output; clearing it makes the pin an input.

// Example for ATmega328P microcontroller
DDRB |= (1 << PB5);  // Set PB5 as an output (LED pin

What are ports?

A port groups several digital I/O pins. The ATmega328P has ports PORTB, PORTC and PORTD, each with registers for direction, output state and input state.

RegisterFunctionExample
DDRSets pin direction (input/output).DDRB
PORTSets output level, or enables an internal pull-up for a pin configured as input.PORTB
PINReads pin state.PINB

Example: Configure and control a port pin

DDRB |= (1 << PB5);  // Set PB5 as an output (LED connected to Pin 13 on Arduino)
PORTB |= (1 << PB5); // Set PB5 high (turn on LED)
PORTB &= ~(1 << PB5); // Set PB5 low (turn off LED)

Key registers and their roles

Data Direction Register (DDR)

DDRB, DDRC and DDRD configure whether each supported pin is an input or output. For example, DDRB |= 0B100000; // PORTB5 sets bit 5 of port B as an output.

Data Direction Register diagram

PORT register

The PORT register controls output values. For example, PORTB |= (1 << PB5); sets PB5 high.

PORT register diagram

PIN register

A PIN register reads the logic level observed at a port input. For example, if (PINB & (1 << PB0)) {...} tests whether PB0 reads high.

PIN register diagram

Peripheral control registers

Peripherals including UART, SPI, timers and ADC have control/status registers. For instance, UCSR0B controls aspects of USART0 transmission and reception.

Example programme: Blinking an LED

The following is the supplied example using direct register access. It configures the Arduino digital pin 13 / PB5 line as an output and switches it on and off repeatedly.

#include <avr/io.h>
#include <util/delay.h>

#define F_CPU 16000000
#define BLINK_DELAY_MS 2000

int main (void)
{
  // Arduino digital pin 13 (pin 5 of PORTB) for output
  DDRB |= 0B100000; // PORTB5
  
  while(1) {
    // turn LED on
    PORTB |= 0B100000; // PORTB5
    _delay_ms(BLINK_DELAY_MS);
    
    // turn LED off
    PORTB &= ~ 0B100000; // PORTB5
    _delay_ms(BLINK_DELAY_MS);
  }
}

Why use direct register manipulation?

  • Resource control: Minimise runtime overhead on constrained hardware.
  • Timing awareness: Understand which operations affect latency, important for timers, interrupts and PWM.
  • Hardware visibility: See how individual bits connect software instructions to physical outputs.

Direct register access is not automatically faster than every higher-level API; compilers can optimise many abstractions. It is valuable because it exposes the operation being performed and the device-specific hardware interface.

Alternative: Toggle the pin using XOR

#include <avr/io.h>
#include <util/delay.h>

#define LED_PIN PB5  // Pin connected to the LED

int main() {
    // Set the LED pin as output
    DDRB |= (1 << LED_PIN);  // DDRB controls data direction for PORTB

    while (1) {
        // Toggle the LED
        PORTB ^= (1 << LED_PIN);  // XOR operation to toggle LED
        _delay_ms(500);           // Delay of 500 milliseconds
    }
    return 0;
}