Embedded C Programming
UNIT-1
Embedded Systems & C Basics
What is an Embedded System?
• An embedded system is a small computing machine that combines hardware and
software components in a single unit and is designed to perform a dedicated task.
Embedded systems are generally used as an integral part of an extensive system or
device to perform or control a specific function.
Depending on the architecture and requirements, embedded systems can be
designed as –
• Programmable embedded systems
• Fixed task-embedded systems
In modern times, embedded systems are used in various industries, from consumer
electronics like smartwatches, smartphones, TVs, etc., to larger industrial
automation systems and robotics.
Block Diagram of Embedded
System
• The block diagram of a typical embedded system is depicted in the
following figure.
The major components of the embedded system are explained
here –
Embedded Software
• This component of an embedded system is used to make the hardware
components functional and control their functionality.
• It includes operating systems and application software. The operating system used
in embedded systems is generally a real-time operating system (RTOS).
Application software depends on the system’s requirements.
Embedded system software is majorly responsible for the following functions
• System control and operation
• Input-output handing
• Communication
• Data processing, etc.
Power Supply
• The power supply unit provides the electric power necessary for the embedded
system to function. In embedded systems, the power supply unit takes AC power
from the supply mains and converts it into a stable DC supply at a voltage suitable
for embedded system components.
Microprocessor
• The microprocessor is the main component of the embedded system that executes
instructions, processes data and performs calculations.
Memory Unit
• The memory unit is responsible for storing data and instructions in the embedded
system. A typical embedded system’s memory unit generally contains RAM and
ROM.
External Device Interface Controller
• This component is responsible for creating a communication interface
between internal components of an embedded system and external
devices or peripherals. External device controller performs functions
like –
• Data collection from sensors and other input devices
• Send signals or outputs to external devices.
Characteristics of Embedded
Systems
The following are some essential characteristics and features of embedded systems
–
• Task–Specific Design – Embedded systems are specially designed to perform a
specific task. Hence, they are different from a general-purpose computer that can
perform multiple operations or functions.
• Real-Time Performance – Embedded systems are usually designed to meet the
real-time requirements of the large system or device in which they are used.
• Integrated in Large System – Most embedded systems are designed to be part of
a large system or device, like an air conditioner, to automatically regulate the
temperature.
• Cost-Effective Solution – Embedded systems are cost-effective solutions for
those systems with low or no performance demands.
• Run on Firmware – Embedded systems have a micro-design and a firmware-
driven mechanism stored in a non-volatile memory like ROM.
• Use Limited Hardware Resources – Embedded systems are designed to use
minimal hardware resources, such as very small memory, a simple user interface,
and limited input capabilities.
Real-World Examples of Embedded Systems
The following are some examples of embedded systems in real-world applications
• In modern vehicles, embedded systems perform a variety of functions, such as
cruise control, air bag system, navigation, infotainment, etc.
• Many embedded systems are used in smartphones. Some common examples
include cameras, microphones, speakers, display units, operating systems,
interfacing modules, network controllers, I/O ports, sensors, etc.
• In industries, embedded systems automate, control, and monitor specific
operations within a large industrial setup.
• In medical equipment, embedded systems are used for automatic vital sign
monitoring, controlling complex operations, etc.
• In wearable and fitness devices, embedded systems are integrated to monitor heart
rate, oxygen level, step counts, and more.
Compiler
• A compiler is a computer program (or a set of programs) that transforms the
source code written in a programming language (the source language) into another
computer language (normally binary format). The most common reason for
conversion is to create an executable program.
• The name "compiler" is primarily used for programs that translate the source code
from a high level programming language to a low-level language (e.g., assembly
language or machine code).
Cross-Compiler
• If the compiled program can run on a computer having different CPU or operating system than the
computer on which the compiler compiled the program, then that compiler is known as a cross-
compiler.
Decompiler
• A program that can translate a program from a low-level language to a high-level language is called a
decompiler.
Language Converter
• A program that translates programs written in different high-level languages is normally called a
language translator, source to source translator, or language converter.
A compiler is likely to perform the following operations −
• Preprocessing
• Parsing
• Semantic Analysis (Syntax-directed translation)
• Code generation
• Code optimization
• Assemblers
• An assembler is a program that takes basic computer instructions
(called as assembly language) and converts them into a pattern of bits
that the computer's processor can use to perform its basic operations.
An assembler creates object code by translating assembly instruction
mnemonics into opcodes, resolving symbolic names to memory
locations. Assembly language uses a mnemonic to represent each low-
level machine operation (opcode).
Embedded Development Tools
• Cross Compiler: Compiles code on a host machine (e.g., PC) into machine code for the
target microcontroller.
• Debugger: Tool to test and debug code on the embedded system, can be hardware-based
(JTAG) or software simulators.
• IDE (Integrated Development Environment): Software that combines editor,
compiler, debugger, and build tools (e.g., Eclipse, Keil, Atmel Studio, VS Code +
PlatformIO).
• Programmer/Flasher: Uploads the compiled code to the microcontroller.
Basics of C Programming
Data Types
• int: Integer numbers (typically 2 or 4 bytes).
• char: Single characters (1 byte).
• float: Floating point numbers.
• double: Double precision floating point numbers.
• void: Represents no value or no data type.
• Derived types: Arrays, pointers, structures.
Variables
• Named storage locations in memory.
• Must be declared with a data type.
• Can be initialized and assigned values.
int count = 10;
char letter = 'A';
float temperature = 36.5;
• Control Statements
• if-else: Decision making
• if (condition) {
// statements
• } else {
// statements
•}
•switch: Multiple-choice branching
switch (variable) {
case 1: // code; break;
case 2: // code; break;
default: // code;
}
Loops
for loop: Executes a block repeatedly a fixed number of times
for (int i = 0; i < 10; i++) {
// code
}
while loop: Executes while a condition is true
while (condition) {
// code
}
do-while loop: Executes the block at least once, then repeats while condition is true
do {
// code
} while (condition);
What is Embedded C Programming?
• Embedded C is a extension of the C programming language
specifically designed for Embedded Systems.
• These are small, dedicated computers that are integrated within larger
devices to control their functionality.
• From washing machines to robots, countless devices rely on
embedded systems for their operation.
Why should you learn Embedded C?
Here are a few good reasons:
• High Demand: Companies in many industries need developers who
know Embedded C, so it’s a skill that can lead to a stable and
rewarding career.
• Versatility: Embedded C is used in a lot of different things, so once
you learn it, you can work on all sorts of projects.
• Challenge and Reward: Getting really good at Embedded C means
you’ll understand how hardware and software work together, which
can be both challenging and satisfying.
The Foundation of Embedded C
• Microcontrollers (MCUs): These are tiny computers embedded in devices. They
contain a CPU, memory, and I/O peripherals to interact with the outside world.
• Integrated Development Environment (IDE): This software helps you write,
compile, debug, and download code onto the microcontroller. Beginner-friendly
IDEs include Keil MDK, Arduino IDE, and Eclipse with CDT plugin.
• Compilers: These tools convert your human-readable C code into machine code
that the microcontroller can understand.
Building Blocks of Embedded C
• Data Types: These define the type and size of data your program can handle, like
int (integer), float (floating-point number), and char (character).
• Variables: Named storage locations that hold data, declared with a data type and
name, and optionally an initial value.
• Operators: Perform operations on data, like arithmetic (+, -, *, /), logical (&&,
||, !), and comparison (==, !=, <, >).
• Control Flow Statements: Control the program’s flow, including if-else, switch,
for, and while loops.
• Functions: Reusable code blocks that perform specific tasks, taking input
(arguments) and returning output (a value or action).
Embedded C Specifics
• Embedded C adds hardware interaction features:
• Bit Manipulation: Deals with individual bits (0s and 1s) for hardware control,
using operators like & (bitwise AND), | (bitwise OR), and << (left shift).
• Input/Output (I/O) Programming: Reading data from sensors (inputs) and
controlling devices like LEDs (outputs) using specific microcontroller registers
and functions.
• Interrupt Service Routines (ISRs): Special functions that respond to external
events like button presses or timer overflows, allowing the program to react
efficiently.
Beyond the Basics
• Memory Management: Learn to allocate and manage memory efficiently, a
critical skill in embedded systems where resources are limited.
• Real-Time Programming: Embedded systems often operate in real-time,
requiring them to respond to events within strict time limits. Understanding real-
time concepts is crucial.
• Hardware Interfacing: Explore communication protocols like I2C, SPI, and
UART to connect the microcontroller with external devices such as sensors and
displays.
Structure of Embedded C Program
#include <header_file.h> // Include necessary header files for the microcontroller and peripherals
// Define global variables and constants here
void setup(); // Function to initialize hardware peripherals
void loop(); // Function containing the main program logic
int main() {
setup(); // Call setup function to initialize hardware
while(1) {
loop(); // Call loop function repeatedly for main program logic
}
return 0;
}
void setup() {
// Initialize hardware peripherals (e.g., GPIO, UART, SPI, I2C)
// Configure pins, set up communication protocols, etc.
}
void loop() {
// Main program logic
// Read sensor data, process input, control outputs, etc.
}
Introduction of Embedded C
Programming
• https://piembsystech.com/introduction-to-embedded-c-programming
-language/
• https://youtu.be/Gvo2GJS1W5E
• What is Keil µVision5?
• Keil µVision5 is an Integrated Development Environment (IDE) for
developing embedded applications in C/C++ for ARM, 8051, and other
microcontroller architectures.
• Requirements
Hardware (example):
• 8051 (e.g., AT89C51) or ARM Cortex-M (e.g., STM32F103) based board
• USB cable for programming
• LED, Push button, etc., for hands-on practice
Software:
• Download Keil µVision5 IDE:
👉 https://www.keil.com/download/
• For 8051: Install the C51 compiler
• For ARM: Install the ARM MDK (Microcontroller Development Kit)
(Free for STM32 and Nuvoton Cortex-M0/M0+ chips with the Community
version)
Basic Workflow in Keil
1.Create a New Project
2.Select your Microcontroller (e.g., AT89C51 or STM32F103C8T6)
3.Add Startup Files / System files
4.Write Your Code in main.c
5.Build the Project
6.Program the Microcontroller (using USB programmer like ST-Link, J-Link, or
USBASP)
7.Debug / Simulate using Keil debugger or real hardware
Blinking an LED (8051 Example)
• Objective: Blink an LED connected to pin P1.0
• Circuit:
• Connect LED (with resistor) to Port 1 Pin 0 (P1.0)
#include <REGX51.H> // Header file for 8051
void delay_ms(unsigned int ms) {
unsigned int i, j;
for(i=0; i<ms; i++)
for(j=0; j<1275; j++);
}
void main(void) {
while(1) {
P1 = 0x01; // Turn ON LED at P1.0
delay_ms(500);
P1 = 0x00; // Turn OFF LED
delay_ms(500);
}
}
Steps to Run in Keil 5 (8051 Example)
1.Open Keil µVision5
2.Project > New µVision Project
3.Name the project → Save in folder
4.Select device: AT89C51 (or your 8051 chip)
5.Click Project > New File → Save as main.c
6.Paste the code above
7.Add to Source Group → Right-click → Add main.c
8.Click Rebuild All (F7)
9.Use Proteus or real hardware + USBASP + ProgISP to upload HEX file