Embedded C Programming
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.
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.
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>
int main()
{
bool a = true;
bool b = false;
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;
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
• 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.