Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
36 views

Embedded C Programming

Embedded C is an extension of C programming language used for microcontroller-based applications. It uses hardware-dependent compilers that generate code for specific microcontrollers. Embedded C supports data types like unsigned char, signed char, unsigned int for limited memory. Common data structures include arrays, structures, unions, and pointers which allow direct memory access. Programming techniques like conditional statements, loops are used for repetition.

Uploaded by

Harish. N
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Embedded C Programming

Embedded C is an extension of C programming language used for microcontroller-based applications. It uses hardware-dependent compilers that generate code for specific microcontrollers. Embedded C supports data types like unsigned char, signed char, unsigned int for limited memory. Common data structures include arrays, structures, unions, and pointers which allow direct memory access. Programming techniques like conditional statements, loops are used for repetition.

Uploaded by

Harish. N
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Embedded C Programming

Parameters C Embedded C

•C is a general-purpose programming language, which can be •Embedded C is simply an extension of C language and it is
General used to design any type of desktop-based application. used to develop micro-controller-based applications.
•It is a type of high-level language. •It is nothing but an extension of C.

•C language is a hardware-independent language. •Embedded C is a fully hardware-dependent language.


Dependency
•C compilers are OS-dependent. •Embedded C is OS-independent.

•For Embedded C, specific compilers that are able to


•For C language, the standard compilers can be used to generate particular hardware/micro-controller-based output
compile and execute the program. are used.
•Popular Compiler to execute a C language program are: •Popular Compiler to execute an Embedded C language
Compiler
• GCC (GNU Compiler collection) program are:
• Borland turbo C, • Keil compiler
• Intel C++ • BiPOM ELECTRONIC
• Green Hill Software

•Formatting depends upon the type of microprocessor that is


used.
•C language has a free format of program coding.
•It is used for limited resources like RAM and ROM.
•It is specifically used for desktop applications.
•High level of optimization.
•Optimization is normal.
•It is not easy to read and modify the Embedded C language.
•It is very easy to read and modify the C language.
•Bug fixing is complicated in an Embedded C language
•Bug fixing is very easy in a C language program.
program.
Usability and Applications •It supports other various programming languages during
•It supports only the required processor of the application
application.
and not the programming languages.
•Input can be given to the program while it is running.
•Only the pre-defined input can be given to the running
•Applications of C Program:
program.
• Logical programs
•Applications of Embedded C Program:
Data types
Types Description

Primitive data types are the most basic data types that
Primitive Data Types are used for representing simple values such as
integers, float, characters, etc.

The user-defined data types are defined by the user


User Defined Data Types
himself.

The data types that are derived from the primitive or


Derived Types built-in datatypes are referred to as Derived Data
Types.
Data types in Emb C
Data Type Bits Range

Unsigned char 8 0 – 255

Signed char 8 -128 – +127

Unsigned int 16 0 – 65535

Signed int 16 -32768 – +32767

bit 1 0–1

sbit 1 0–1

sfr 8 0 – 255

sfr16 16 0 – 65535
boolean
• Boolean can store values as true-false, 0-1, or can be yes-no. It
can be implemented in C using different methods as mentioned
below:
1.Using header file “stdbool.h”
2.Using Enumeration type
3.Using define to declare boolean values
Using the Enumeration Type
implement bool in C using an enumeration type. Here rather
than importing the library, we declare an enumeration type
so as to use bool as the data type.
#include <stdio.h>

typedef enum { false, true } bool;

int main()
{
bool a = true;
bool b = false;

printf("True : %d\n", a);


printf("False : %d", b);

return 0;
}
O/P
False=0
• Using Define to Declare Boolean Values
• In this case, the false value is assigned the integer value of 0, and the true value is assigned the integer
value of 1. You can also use an int or a char with a value of either 0 (false) or 1 (true) to represent the bool
data type in C.
#define bool int
#define false 0
#define true 1

int main()
{
bool a = true;
bool b = false;

printf("True : %d\n", a);


printf("False : %d", b);
return 0;
}
sfr
• The Special Function Register (SFR) is the upper area of addressable
memory, from address 0x80 to 0xFF. This area of memory can't be used for
data or program storage, but is instead a series of memory-mapped ports
and registers.
• A Special Function Register (or Special Purpose Register, or simply Special
Register) is a register within a microprocessor that controls or monitors the
various functions of a microprocessor.
• As the special registers are closely tied to some special function or status
of the processor, they might not be directly writable by normal instructions
• the 8051, register A, B, DPTR, and PSW are a part of the group of registers
commonly referred to as SFR (special function registers).
Array, struct and union
• Array
• a method of clubbing multiple entities of similar type into a larger
group
.one dimensional array
Two- dimensional array
• Struct
• The structure join together several variables or constant or array.
• A structure has amemory equal to or greater than the sum of the m/y
required by each number of the structure.
• struct example_struct
{
unsigned int member1;
float member2;
char* member3;
};
• Union
• It is similar to the struct
• It has member of diff data types.it can hold data of only one member at a
time.
Union union_tag
{
unsigned int member1;
Float member2;
char* member3;
}union_name;
pointer
• A pointer in C is a variable that stores the address of another
variable. Pointers provide a way to directly interact with memory
locations, allowing for efficient manipulation of data and
dynamic memory management.
int main()
{
int val=5;
int *ptr=&val;
printf(“the value of pointer: %d”, *ptr);
}
Null pointer
• A null pointer is a pointer that is specifically set to point to nothing. It
is a safe way to initialize a pointer when it's not yet assigned to a
specific memory location. Dereferencing a null pointer results in
undefined behavior. Example:
int *nullptr=NULL;
{
if(nullptr==NULL)
Printf(“the pointer is null”);
}
Pointers are a double-edged sword in Embedded C programming.
They offer powerful capabilities for direct memory access and efficient
data manipulation.
Loop
• while loop means, repeat execution of code inside the loop body
until expression evaluates to false(0). It only has one part: the
condition.
• do..while loop says, repeat the execution of code inside the “do”
body until the expression evaluates to false(0).
• The difference between a while loop and a do..while loop
is, do..while loop statements are always executed first and then the
expression will be checked.
• for loop is one of the famous and most widely used looping
statements in ‘C’. The for loop is used to execute a block of code for
a specified number of times. It has three parts: the initialization,
condition, and increment/decrement.
• Nested Loops: Loops can be nested, meaning a loop can contain another loop
inside it. This allows for complex operations and algorithms.
• Breaking Out of a Loop: The break statement can be used to break out of a
loop and exit the loop immediately.
• Continuing a Loop: The continue statement can be used to skip the current
iteration of a loop and continue with the next iteration.
• Infinite Loops: Infinite loops are loops that run indefinitely and can cause an
application to freeze or crash. They should be avoided by ensuring a proper exit
condition is provided.
• Loop Control Variables: Loop control variables are used to control the number
of iterations of a loop. They are usually incremented or decremented within the
loop.
• Use of Loops: Loops are widely used in programming for tasks such as iteration
through arrays, performing repetitive operations, and controlling the flow of
program execution.
Functions and library functions
• Main function
• Void main, int main..

Library
In C programming language, a library function is a prewritten
piece of code that performs a specific task. These functions are
included in precompiled libraries, which can be linked to a
program to provide additional functionality
Parameters in C functions

• A Parameter is the symbolic name for "data" that goes into a function. There
are two ways to pass parameters in C: Pass by Value, Pass by Reference.
• Pass by Value
• Pass by Value, means that a copy of the data is made and stored by way of
the name of the parameter. Any changes to the parameter have NO affect on
data in the calling function.
• Pass by Reference
• A reference parameter "refers" to the original data in the calling function.
Thus any changes made to the parameter are ALSO MADE TO THE
ORIGINAL variable.
RTOS
• Real-time operating systems (RTOS) are used in environments
where a large number of events, mostly external to the computer
system, must be accepted and processed in a short time or within
certain deadlines. such applications are industrial control, telephone
switching equipment, flight control, and real-time simulations. With
an RTOS, the processing time is measured in tenths of seconds. This
system is time-bound and has a fixed deadline. The processing in this
type of system must occur within the specified constraints.
Otherwise, This will lead to system failure.
• Examples of real-time operating systems are airline traffic control
systems, Command Control Systems, airline reservation systems,
Heart pacemakers, Network Multimedia Systems, robots, etc.
Context Switching
• The context switch procedure consists of getting the values of
the CPU register during the execution and saving them at the
bottom of the stack of the currently running task. Then, restore
the values for the next task, to resume its execution.
• the OS is an interface between the user and computer hardware.
Process control block
• While creating a process, the operating system performs several
operations. To identify the processes, it assigns a process
identification number (PID) to each process. As the operating system
supports multi-programming, it needs to keep track of all the
processes.
• For this task, the process control block (PCB) is used to track the
process’s execution status. Each block of memory contains
information about the process state, program counter, stack pointer,
status of opened files, scheduling algorithms, etc.
• All this information is required and must be saved when the process
is switched from one state to another.
• When the process makes a transition fromone state to another,
the operating system must update information in the process’s
PCB. A process control block (PCB) contains information about
the process, i.e. registers, quantum, priority, etc. The process
table is an array of PCBs, that means logically contains a PCB for
all of the current processes in the system.
1.Pointer: It is a stack pointer that is required to be saved when
the process is switched from one state to another to retain the
current position of the process.
2.Process state: It stores the respective state of the process.
3.Process number: Every process is assigned a unique id known
as process ID or PID which stores the process identifier.
4.Program counter: It stores the counter,: which contains the
address of the next instruction that is to be executed for the
process.
5.Register: Registers in the PCB, it is a data structure. When a
processes is running and it’s time slice expires, the current value of
process specific registers would be stored in the PCB and the process
would be swapped out. When the process is scheduled to be run, the
register values is read from the PCB and written to the CPU registers.
This is the main purpose of the registers in the PCB.
6.Memory limits: This field contains the information about memory
management system used by the operating system. This may include
page tables, segment tables, etc.
7.Open files list : This information includes the list of files opened for
a process.
Rate monotonic scheduling
• Rate monotonic scheduling is a priority algorithm that
belongs to the static priority scheduling category of Real Time
Operating Systems. It is preemptive in nature. The priority is
decided according to the cycle time of the processes that are
involved. If the process has a small job duration, then it has the
highest priority. Thus if a process with highest priority starts
execution, it will preempt the other running processes. The
priority of a process is inversely proportional to the period it will
run for. A set of processes can be scheduled only if they satisfy
the following equation
• Example: An example to understand the working of Rate
monotonic scheduling algorithm.
Processes Execution Time (C) Time period (T)

P1 3 20

P2 2 5

P3 2 10

• n( 2^1/n - 1 ) = 3 ( 2^1/3 - 1 ) = 0.7977


• U = 3/20 + 2/5 + 2/10 = 0.75
• It is less than 1 or 100% utilization. The combined utilization of three
processes is less than the threshold of these processes which means the
above set of processes is schedulable and thus satisfies the above equation
of the algorithm.
1.Scheduling time – For calculating the Scheduling time of the
algorithm we have to take the LCM of the Time period of all the
processes. LCM ( 20, 5, 10 ) of the above example is 20. Thus we can
schedule it by 20-time units.
2.Priority – As discussed above, the priority will be the highest for the
process which has the least running time period. Thus P2 will have
the highest priority, and after that P3 and lastly P1.
• P2>p3>p1
• Representation and flow is as follows:

• The above figure says that Process P2 will execute two times for
every 5 time units, Process P3 will execute two times for every 10
time units and Process P1 will execute three times in 20 time units.
This has to be kept in mind for understanding the entire execution of
the algorithm below.
• Process P2 will run first for 2 time units because it has the highest
priority.
• After completing its two units, P3 will get the chance and thus it will
run for 2 time units.
• As we know that process P2 will run 2 times in the interval of 5 time
units and process P3 will run 2 times in the interval of 10 time units,
they have fulfilled the criteria and thus now process P1 which has the
least priority will get the chance and it will run for 1 time.
• And here the interval of five time units have completed. Because of
its priority P2 will preempt P1 and thus will run 2 times.
• As P3 have completed its 2 time units for its interval of 10 time units,
P1 will get chance and it will run for the remaining 2 times,
completing its execution which was thrice in 20 time units.
• Now 9-10 interval remains idle as no process needs it. At 10
time units, process P2 will run for 2 times completing its criteria
for the third interval ( 10-15 ). Process P3 will now run for two
times completing its execution.
• Interval 14-15 will again remain idle for the same reason
mentioned above. At 15 time unit, process P2 will execute for
two times completing its execution.This is how the rate
monotonic scheduling works.

You might also like