Learn EMBEDDED-C-CPP with Real Code Examples
Updated Nov 27, 2025
Code Sample Descriptions
1
Blink LED (Embedded C, AVR microcontroller)
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB |= (1 << PB0); // Set PB0 as output
while(1)
{
PORTB ^= (1 << PB0); // Toggle LED
_delay_ms(500);
}
}
Classic embedded example toggling a GPIO pin to blink an LED on an AVR microcontroller.
2
Embedded C++ Class for LED Control
#include "stm32f4xx.h"
class LED {
public:
LED(GPIO_TypeDef* port, uint16_t pin) : port(port), pin(pin)
{
port->MODER |= (1U << (pin * 2)); // Set as output
}
void on() { port->ODR |= (1U << pin); }
void off() { port->ODR &= ~(1U << pin); }
void toggle() { port->ODR ^= (1U << pin); }
private:
GPIO_TypeDef* port;
uint16_t pin;
};
int main()
{
LED led(GPIOB, 0);
while(1)
{
led.toggle();
for(volatile int i=0; i<1000000; ++i);
}
}
A simple Embedded C++ class encapsulating GPIO operations for LED control on an ARM Cortex-M MCU.
3
Read Button Input (Embedded C)
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB |= (1 << PB0); // LED output
DDRD &= ~(1 << PD0); // Button input
while(1)
{
if(PIND & (1 << PD0)) PORTB |= (1 << PB0);
else PORTB &= ~(1 << PB0);
_delay_ms(50);
}
}
Reads a push button connected to a GPIO and toggles an LED accordingly on an AVR MCU.
4
PWM LED Brightness (Embedded C, AVR)
#include <avr/io.h>
int main(void)
{
DDRB |= (1 << PB1); // OC1A output
TCCR1A |= (1 << COM1A1) | (1 << WGM10);
TCCR1B |= (1 << WGM12) | (1 << CS10); // Fast PWM, no prescaler
while(1)
{
for(uint8_t i=0; i<255; i++) { OCR1A = i; for(volatile int j=0;j<1000;j++); }
}
}
Generates PWM on an AVR to control LED brightness.
5
UART Transmit (Embedded C)
#include <avr/io.h>
void uart_init() { UBRR0 = 103; UCSR0B = (1<<TXEN0); }
void uart_send(char c) { while(!(UCSR0A & (1<<UDRE0))); UDR0 = c; }
int main(void) {
uart_init();
while(1) { uart_send('A'); }
}
Sends a character over UART on an AVR microcontroller.
6
ADC Read and LED Control (Embedded C)
#include <avr/io.h>
int main(void) {
ADCSRA = (1<<ADEN);
DDRB |= (1<<PB0);
while(1) {
ADCSRA |= (1<<ADSC);
while(ADCSRA & (1<<ADSC));
if(ADC > 512) PORTB |= (1<<PB0);
else PORTB &= ~(1<<PB0);
}
}
Reads an analog sensor and controls an LED based on threshold on an AVR MCU.
7
Timer Interrupt Toggle LED (Embedded C)
#include <avr/io.h>
#include <avr/interrupt.h>
ISR(TIMER0_OVF_vect){ PORTB ^= (1<<PB0); }
int main(void){ DDRB |= (1<<PB0); TIMSK0 |= (1<<TOIE0); sei(); TCCR0B |= (1<<CS02); while(1); }
Uses a timer interrupt to toggle an LED periodically on an AVR.
8
Embedded C++ Motor Control Class
class Motor {
public:
Motor(GPIO_TypeDef* dirPort, uint16_t dirPin, TIM_TypeDef* pwmTimer) : dirPort(dirPort), dirPin(dirPin), pwmTimer(pwmTimer) {}
void forward() { dirPort->ODR |= (1<<dirPin); }
void reverse() { dirPort->ODR &= ~(1<<dirPin); }
void setSpeed(uint8_t duty) { pwmTimer->CCR1 = duty; }
private:
GPIO_TypeDef* dirPort;
uint16_t dirPin;
TIM_TypeDef* pwmTimer;
};
Encapsulates motor control logic using PWM and direction pins on an ARM MCU.
9
SPI Send Byte (Embedded C)
#include <avr/io.h>
void spi_init() { DDRB |= (1<<PB3)|(1<<PB5)|(1<<PB2); SPCR = (1<<SPE)|(1<<MSTR); }
void spi_send(uint8_t data){ SPDR = data; while(!(SPSR & (1<<SPIF))); }
int main(){ spi_init(); while(1){ spi_send(0xAA); } }
Sends a byte over SPI on an AVR microcontroller.
10
I2C Master Send (Embedded C)
#include <avr/io.h>
#define F_SCL 100000UL
#define TWBR_VAL ((F_CPU/F_SCL-16)/2)
void i2c_init(){ TWBR=TWBR_VAL; }
void i2c_start(){ TWCR=(1<<TWSTA)|(1<<TWEN)|(1<<TWINT); while(!(TWCR&(1<<TWINT))); }
void i2c_send(uint8_t data){ TWDR=data; TWCR=(1<<TWEN)|(1<<TWINT); while(!(TWCR&(1<<TWINT))); }
int main(){ i2c_init(); i2c_start(); i2c_send(0xA0); }
Sends a byte over I2C as a master on AVR.