DP - Unit-2 - BCAIAI507 - (Elective-IV) Embedded Systems
DP - Unit-2 - BCAIAI507 - (Elective-IV) Embedded Systems
DP - Unit-2 - BCAIAI507 - (Elective-IV) Embedded Systems
Difference between C and Embedded C: While C and Embedded C share many similarities, there are
key differences that make Embedded C suitable for embedded system development:
1. Hardware Interaction:
a. C: C is a general-purpose programming language that does not have built-in features
for direct hardware interaction. Hardware control often requires platform-specific
libraries.
b. Embedded C: Embedded C includes features for low-level hardware interaction, such
as direct register manipulation and bit-level operations, which are essential for
embedded systems.
2. Memory Management:
c. C: C assumes the availability of abundant memory and may use dynamic memory
allocation (e.g., `malloc` and `free`) extensively.
d. Embedded C: Embedded C focuses on efficient memory utilization and often avoids
dynamic memory allocation due to the limited memory resources in embedded
systems. Static memory allocation is common.
3. Portability:
e. C: C code can be highly portable across different platforms and operating systems.
f. Embedded C: Embedded C code may be less portable because it is tightly coupled
with the hardware and may include platform-specific code.
4. Standard Library:
g. C: C has a rich standard library that provides functions for various general-purpose
tasks.
h. Embedded C: Embedded C may have a limited standard library, and developers often
rely on custom libraries or write low-level code for specific tasks.
5. Real-Time Capabilities:
i. C: C is not inherently designed for real-time systems, but real-time features can be
implemented using external libraries or system-specific code.
j. Embedded C: Embedded C is designed with real-time systems in mind and includes
features for deterministic execution and real-time task scheduling.
Programming Style in Embedded C: When writing code in Embedded C, developers follow specific
programming style conventions to ensure code readability, maintainability, and compatibility with the
target hardware. Some common programming style guidelines for Embedded C include:
1. Modularity: Divide the code into functions and modules that perform specific tasks. This
improves code organization and reusability.
2. Descriptive Variable Names: Use meaningful and descriptive names for variables, functions,
and macros to enhance code clarity.
The basic structure of an Embedded C program is similar to that of a standard C program but with
certain conventions and considerations specific to embedded systems. Below is the basic structure of
an Embedded C program:
Constants: Constants are fixed values that do not change during program execution. In Embedded C,
you commonly use the following types of constants:
1. Integer Constants: These are whole numbers, either decimal, octal (prefix '0'), or hexadecimal
(prefix '0x'). For example:
a. `int integerConstant = 42;`
b. `int octalConstant = 052; // Octal (42 in decimal)`
c. `int hexConstant = 0x2A; // Hexadecimal (42 in decimal)`
2. Floating-Point Constants: These are real numbers with a fractional part and/or exponent. For
example:
float floatConstant = 3.14;
double doubleConstant = 2.71828;
3. Character Constants: Single characters enclosed in single quotes. For example:
char charConstant = 'A';
4. String Constants: Sequences of characters enclosed in double quotes. For example:
char* stringConstant = "Hello, World!";
5. Enumeration Constants: Enumerations define a set of named integer constants. For example:
enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
enum Days currentDay = Wednesday;
2. Preprocessor Constants: Constants defined using `#define` in preprocessor directives. For
example:
#define MAX_VALUE 100
int value = MAX_VALUE;
Variables: Variables are used to store and manipulate data during program execution. In Embedded
C, variables are typically declared using data types specific to the target hardware:
Integer Variables: Used to store whole numbers (int, short, long, etc.).
int integerVar = 42;
Floating-Point Variables: Used for real numbers (float, double, etc.).
float floatVar = 3.14;
Character Variables: Store single characters.
char charVar = 'A';
Pointer Variables: Hold memory addresses and are used for various purposes, including
accessing hardware registers.
int* ptr; // Pointer to an integer
Array Variables: Used to store multiple values of the same data type.
int numbers[5] = {1, 2, 3, 4, 5};
Structure and Union Variables: Organize multiple variables of different data types into a
single entity.
struct Point {
int x;
int y;
};
struct Point p1 = {10, 20};
Keywords: Keywords are reserved words in the C language that have predefined meanings. You
cannot use keywords as identifiers (variable names, function names, etc.). Examples of keywords
include `int`, `float`, `if`, `for`, `while`, and `return`.
Identifiers: Identifiers are names given to variables, functions, arrays, and other program elements.
Identifiers must adhere to certain rules:
- Must start with a letter (uppercase or lowercase) or an underscore `_`.
- Subsequent characters can include letters, digits, or underscores.
- Identifiers are case-sensitive.
Here's an example that uses constants, variables, data types, and identifiers in an Embedded C
program:
#include <stdio.h>
#define MAX_VALUE 100
int main(void) {
int integerVar = 42;
float floatVar = 3.14;
char charVar = 'A';
Arrays in Embedded C: Arrays are collections of elements of the same data type, grouped together
under a single name. They are particularly useful in embedded systems for storing and processing data
efficiently. Here are key considerations for using arrays in embedded C:
1. Declaration: Arrays are declared with a specified data type and a size that determines the
number of elements it can hold.
- Example: int sensorReadings[10]; // Declares an integer array with 10 elements
2. Initialization: Arrays can be initialized at the time of declaration or later in the code.
- Example: int temperatures[5] = {25, 28, 30, 27, 26}; // Initialize with values
3. Accessing Elements: Elements in an array are accessed using an index, which starts from 0.
- Example: int value = sensorReadings[2]; // Accesses the third element (index 2) in
the array
4. Iterating Through Arrays: Loops are commonly used to iterate through array elements.
- Example:
- for (int i = 0; i < 10; i++) {
- // Access and process sensorReadings[i]
- }
5. Array Size:
It's essential to manage array size carefully in embedded systems to avoid memory
overflow or waste.
Arrays should be sized according to the specific requirements of the application.
6. Memory Efficiency: In embedded systems with limited memory, consider using smaller data
types (e.g., `uint8_t`, `int16_t`) to minimize memory usage if appropriate.
In Embedded C programming, operators are used to perform various operations on variables and
values. These operators can be classified into several categories, including arithmetic, relational,
logical, assignment, bitwise, and more.
Types of Operators in C:
1. Arithmetic Operators: These operators perform basic arithmetic operations.
Examples: `+` (addition), `-` (subtraction), `*` (multiplication), `/` (division), `%` (modulus).
2. Relational Operators: Relational operators are used to compare values.
Examples: `==` (equal to), `!=` (not equal to), `<` (less than), `>` (greater than), `<=` (less than
or equal to), `>=` (greater than or equal to).
3. Logical Operators: Logical operators are used for logical operations.
Examples: `&&` (logical AND), `||` (logical OR), `!` (logical NOT).
4. Assignment Operators: These operators assign values to variables.
Examples: `=` (assignment), `+=` (addition assignment), `-=` (subtraction assignment), `*=`
(multiplication assignment), `/=` (division assignment), `%=` (modulus assignment), and
others.
5. Increment/Decrement Operators: These operators are used to increment or decrement
variables.
Examples: `++` (increment), `--` (decrement).
6. Bitwise Operators (Focus of Explanation): Bitwise operators perform operations at the bit
level.
Examples: `&` (bitwise AND), `|` (bitwise OR), `^` (bitwise XOR), `~` (bitwise NOT), `<<` (left
shift), `>>` (right shift).
Bitwise Operators in Embedded C: Bitwise operators are essential in embedded systems for tasks like
setting and clearing individual bits in hardware registers, managing flags, and performing bitwise
calculations. Let's explain each bitwise operator:
1. Bitwise AND (`&`): Performs a bitwise AND operation between corresponding bits of two
operands. It sets a bit to 1 only if both corresponding bits are 1.
Example: uint8_t result = 0b1010 & 0b1100; // result is 0b1000
2. Bitwise OR (`|`): Performs a bitwise OR operation between corresponding bits of two
operands. It sets a bit to 1 if at least one of the corresponding bits is 1.
Example: uint8_t result = 0b1010 | 0b1100; // result is 0b1110
3. Bitwise XOR (`^`): Performs a bitwise XOR (exclusive OR) operation between corresponding
bits of two operands. It sets a bit to 1 if the corresponding bits are different.
Example: uint8_t result = 0b1010 ^ 0b1100; // result is 0b0110
4. Bitwise NOT (`~`): Inverts (flips) all the bits of a single operand.
5. Left Shift (`<<`): Shifts the bits of the left operand to the left by a specified number of
positions, filling in with zeros on the right.
Example: uint8_t result = 0b1010 << 2; // result is 0b00101000
6. Right Shift (`>>`): Shifts the bits of the left operand to the right by a specified number of
positions, filling in with zeros on the left.
Example: uint8_t result = 0b1010 >> 2; // result is 0b000010
Bitwise operators are particularly useful for manipulating individual bits in register settings,
implementing protocols, and performing other low-level operations common in embedded systems
development.