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.
Explain why C remains widely used in embedded development and identify alternatives.
Distinguish physical package pins, microcontroller port pins and Arduino pin labels.
Describe how DDR, PORT and PIN registers configure and control digital I/O.
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.
| Language | Typical characteristics |
|---|---|
| Assembly (ASM) | Direct instruction-level control; highly specific to a processor architecture. |
| C | Low-level access and relatively small runtime overhead; widely used for microcontrollers. |
| C++ | Abstractions and object-oriented techniques alongside low-level hardware access. |
| MicroPython / CircuitPython | Python implementations designed for supported microcontroller platforms. |
| Rust | Compile-time ownership and borrowing checks, with opportunities for low-level programming. |
| Ada / SPARK | Used 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

Figure 1. ATmega328P integrated circuit packages.

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.
| PORT | PORT pin | Arduino pin | ATmega pin (28-pin package) |
|---|---|---|---|
| PORTB | PB0 | 8 | 14 |
| PORTB | PB1 | 9 | 15 |
| PORTB | PB2 | 10 | 16 |
| PORTB | PB3 | 11 | 17 |
| PORTB | PB4 | 12 | 18 |
| PORTB | PB5 | 13 | 19 |
| PORTB | PB6 | 20 | 9 |
| PORTB | PB7 | 21 | 10 |
| PORTC | PC0 | 14/A0 | 23 |
| PORTC | PC1 | 15/A1 | 24 |
| PORTC | PC2 | 16/A2 | 25 |
| PORTC | PC3 | 17/A3 | 26 |
| PORTC | PC4 | 18/A4 | 27 |
| PORTC | PC5 | 19/A5 | 28 |
| PORTC | PC6 | 22/RESET | 1 |
| PORTD | PD0 | 0/RXD | 2 |
| PORTD | PD1 | 1/TXD | 3 |
| PORTD | PD2 | 2 | 4 |
| PORTD | PD3 | 3 | 5 |
| PORTD | PD4 | 4 | 6 |
| PORTD | PD5 | 5 | 11 |
| PORTD | PD6 | 6 | 12 |
| PORTD | PD7 | 7 | 13 |
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:
- Hardware control: Configure inputs, outputs and peripherals.
- Low-level access: Read or update hardware state directly.
- 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.
| Register | Function | Example |
|---|---|---|
| DDR | Sets pin direction (input/output). | DDRB |
| PORT | Sets output level, or enables an internal pull-up for a pin configured as input. | PORTB |
| PIN | Reads 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.

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

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.

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;
}