Browse by Domains

Embedded C

Introduction

Before the C language was introduced, developers used basic programming (assembly-level) to develop embedded applications. These assembly level programming were not portable in nature, which acted as a restriction for the developers, as they were not able to fully engage in the process of the development of embedded applications. With the improvements in the embedded programming language many new programming languages were introduced such as C, BOCOL and Pascal, these relatively newer languages helped in overcoming many problems that were faced by the developers when working with assembly level programming. One of the most noticeable breakthroughs in the development of embedded programming was seen with the introduction of the C programming language. There are many advantages that can be exploited by developers when developing embedded applications such as the embedded c programming provides portability and is easy to work with due to its simplicity. Embedded c programming is more reliable and scalable when compared to assembly level programming. Embedded systems are able to perform various assigned tasks with the help of embedded c programming, as the programming guides the processor of the respective embedded systems. Embedded C language is one of the most widely used embedded programming languages for programming microcontrollers. 

About the C programming language

Dennis Ritchie developed the C programming language in 1969. The C programming language is structured in a collection of a single function or multiple functions. Uniquely specified tasks are performed by statements, and these statements are collectively called a function. C language is sometimes also referred to as a middle level programming language, as the language provides the developers the freedom to develop high level applications as well as low level languages. 

The advantages to work with C programming language are:

There are various keywords and data types, also different variables, constants, etc are used to design the software of C language. 

The programming language which is used in the development of hardware architecture is also commonly known as embedded C as it is written C Programming language. 

Header Files were introduced in Embedded C, these header files can be changed depending upon the type of controller being used for the development of embedded systems. 

For example: #include<reg51.h> header file is used for the microcontroller 8051. 

Embedded System

A system incorporating both the hardware side as well as the software, which is designed to perform pre specified tasks are known as embedded systems. Examples of embedded systems that are widely used in day to day life are mobile phones, refrigerators, machine machines, etc.

We might be exploiting these embedded systems without realizing that these embedded systems are designed to have their own hardware as well as the software to perform the assigned tasks properly and efficiently. 

There are various actions that are performed when you use your mobile phones such as you run different applications on a single phone, you use your phone to watch videos or maybe play music and many such other tasks. All the tasks that are being performed on a mobile device require a proper set of instruction and execution protocol to run smoothly. 

Also, embedded systems cannot be restricted only to stationary devices or stand alone devices, they can be scaled to relatively larger systems. For example airplanes have the required hardware assisted by a supportive software that is used by the pilots to perform various maneuvers, also providing the feature of auto pilot, etc. These actions are taken care of by the embedded systems that are installed on an airplane. These embedded systems provide safety as they operate on an incredibly low rate of errors, and these systems can be trained for any type of situations by exposing the system to the desired condition by using simulators. 

Some of the embedded systems that are installed on an airplane are temperature sensors for monitoring the temperature of various critical components, engine oil level monitoring systems, autopilot mode, etc. 

How do you program an embedded system?

We have made it clear by now that embedded systems are designed to have their unique hardware and software. For an embedded system considering that the respective system is of a simple design, the processor of the respective embedded system is considered to be the main hardware module of the system. The processor of an embedded system is considered to be the soul of the respective embedded system. For example the microcontroller, microprocessor, Complex Programmable Logic Device (CPLD), DSP or Field Programmable Gated Array (FPGA) can be considered as processors. 

The above-mentioned embedded systems are similar to one another as they can be programmed easily, in simple words for any embedded system to perform certain predefined tasks you can write the program for it. This is considered to be the software side of an embedded system. All the instructions and the execution protocols are defined for an embedded system to perform properly. 

There has to be a link or a connection or a well defined path for operating and communicating between the events happenings externally and internally of an embedded system, the programmed software also known as the embedded software is used for the above mentioned task as it operates the hardware to monitor and then analyze the external events and perform the specified actions by controlling the output of the respective embedded system. During the execution of the entire process, the embedded program usually interacts with the embedded hardware of the embedded system usually referred to as a processor to enable the system to perform the specified tasks. 

We can easily conclude that both the software part and the hardware part of an embedded system are important for the system to perform tasks efficiently. Compromising in one part or overcompensating in another part won’t help as both the parts of an embedded system have to be well balanced for the system to work properly. 

Various levels of programming languages are used for embedded system programming such as low level programming languages (Assembly), high level programming languages (C++, C, JAVA) or assembly level programming languages, etc.

If you want the embedded system to be a well designed system that functions properly, the selection of a programming language to program the embedded system is important. 

Important Factors to consider for programming language selection

When selecting a programming language it is important to consider the follow factors:

  • Program memory plays a very important role in the smooth functioning of an embedded system. We are aware of the fact that microcontrollers have limited program memory (ROM). So, it is important for the embedded program to occupy limited memory. 
  • The speed at which the embedded software operates should be fast timed. The action time of a hardware should not be hampered by an embedded program that takes forever to execute actions. The embedded software should be continuously optimized to perform as fast as possible. 
  • In today’s world time cannot be wasted to code a processor from scratch, the embedded software should be portable, meaning the programs that run on a particular processor should be easily compiled by another processor. 
  • The process of implementation should not be time consuming as it could negatively affect the development cycle. 
  • A proper maintenance process should be designed.
  • The programming language should be easily readable. 

Advantages of Embedded C programming language

Before the implementation of embedded C programming language, developers used assembly level programming language to program the embedded system. There is no denying the fact that assembly level programming languages are considered to provide actual code (machine) instructions. But it had many disadvantages associated with it such as portability of the programs was a big issue, also developing an embedded program required manpower and consumed more time. In simple words huge amounts of resources were required for developing embedded programs using assembly level programming language. 

The C programming language was a game changer. It was favored above all the other existing embedded programming languages. Following are the advantages of embedded C programming language:

  • The time consumed by the development cycle of an embedded program reduced significantly.
  • Developers were able to code easily.
  • Modifications and updates were made easily, resulting in more successful maintenance cycles. 
  • Library functions were introduced that eliminated the need to write the same block of code again and again for each program. 
  • The compatibility of a particular program with various architectures increased as it only required a few modifications. 
  • The hardware cost used in embedded C programs is very low.
  • The embedded applications that are designed are highly efficient for industrial usage. 
  • The complexity of the code is reduced significantly.

Disadvantages of Embedded C programming language

Following are the disadvantages of embedded C programming language. 

  • It executes one task at a time, it does not support multiple task executions.
  • When we make any changes in the program then the appropriate changes are to be made in the hardware as well. 
  • Only hardware systems are supported. 
  • It cannot be scaled efficiently, there are still some issues faced while doing it. 
  • There are also memory related restrictions or sometimes it has compatibility issues with certain computers. 

List a few known differences between C language and Embedded C

When talking about the differences between the C language and the Embedded C, there are not many to discuss except for a few extensions and the interface environment. They both have the same syntax meaning learning the syntax for any one language will automatically help to operate in the other language. Also, they have the same data types, functions, they make use of the same libraries, dictionaries etc. 

It can be said that the Embedded C is an extension of the C programming language. Many features such as addressing I/O, fixed point arithmetic, multiple memory addressing and many more features are an addition to the artillery of the Embedded C. 

Also, many desktop applications are developed using C programming language, on the other hand applications that are microcontroller based are developed using Embedded C. 

Optimizing, modifying and debugging an application developed in C language is easier when compared to Embedded C. As the format used in C language is a free format whereas the format used in the Embedded C language  depends on the type of microcontroller used. Also, the usage of specific compilers in Embedded C languages results in complex situations, but no such situations are faced when developing applications in C language as they use standard compilers. 

Embedded C Program: Basics

It is important to understand the basics of Embedded C programs. 

Embedded C Program: Keywords

You cannot make use of any random words for defining any specific actions because the compiler won’t be able to understand the word input provided by you. It is important for the compiler to first identify the word and then perform the specific action assigned (if any). For this to happen there are various predefined words that have a specific meaning defined in accordance with the compiler, known as keywords. Following is the table consisting of all the keywords that are used in Cx51 C Compiler ( compilation of the programs that are developed for the 8051 based microcontroller is done by the respective compiler).

_at_alienbdatabit
codecompactdatafar
idatainterruptlarge pdata
_priority_reentrantsbitsfr
small_task_using xdata

Embedded C Program: Data Types

When we want to declare any variables in the program we make use of data types. Following is the table consisting of all the data types that are used in Keil’s Cx51 Compiler.

Data Type Bits(bytes)Range
bit 10 or 1 (it is the bit addressable part of RAM) 
signed bit16(2)-32768 to +32767
unsigned bit 16(2)0 to 65535
signed char 8(1)-128 to +127
unsigned 8(1)0 to 255
float32(4)±1.175494E-38 to ±3.402823E+38
double 32(4)±1.175494E-38 to ±3.402823E+38
sbit10 or 1 (it is the bit addressable part of RAM)
sfr8(1)RAM Addresses (considering 80h to FFh)
sfr1616(2)0 to 65535

What is the basic structure or a template of an Embedded C program?

It is important to have a basic framework in mind before writing any Embedded C program. Following is a practice template that can be used to write your initial Embedded C programs.

  1. Multiline Comments 
  2. Single Line Comments 
  3. Preprocessor Directives 
  4. Global Variables
  5. Function Declarations 
  6. Main Function 
  7. Local Variables 
  8. Function Calls 
  9. Infinite Loop 
  10. Statements 
  11. Function Definitions 
  12. Local Variables 
  13. Statements 

Embedded C Program: Comments

The code of any program should be easily understood by the reader. Sometimes the code of a program can be so lengthy or complicated that it becomes for the developers as well as the reader to make any sense out of it. To eliminate the above stated problems comments are used. Comments are introduced in the code of a program to make it more readable. With the help of comments the reader can understand what is happening in the program. They are mainly used to explain the functioning of the program. But, how does comments not interfere in the execution process? Any idea…? This is because the comments are ignored by the compiler, or we can say that they are non executable. A proper documentation of the code can be done with the help of comments for future reference. In complex embedded programming, comments are very useful. Embedded C provides two types of comments: single line comments and multiline comments.

For example:

E=x*y; /* the value of the multiplication of the two variables x and y is stored in another variable E*/

Embedded C Program: Single line Comments 

When programming embedded systems there are many fractions of code that require proper explaining for future reference and to increase readability. When you want to only explain a certain fraction of a program it is best to use single line comments. Single line comments begin with (//) double slash, and have to be written in a single line because if the comment is continued in the next line the compiler will not ignore the comment and the program will fail to execute. Single line comments are also used to mention the beginning and ending of a certain block of program, promoting a proper structuring of the embedded program. 

For example:

// the equation below defines the value of E variable 

E=x+y;

Embedded C Program: Multiline Comments 

There are points in the program where explanation cannot suffice into a single line. Then you can use multiline comments. A multi line comment starts with a slash (/), then followed by a asterisk (/*) and ended by an asterisk (*) mark followed by a slash (*/). The compiler ignores the whole block of code where the multiline comments are written, meaning you can explain the code in multiple lines and these lines of explanation will be ignored by the compiler. 

For example 

E=x-y; /* the value of the subtraction of the two variables x and y is stored in another variable E*/

Embedded C Program: Directives of Processors 

About Preprocessor Directive

Preprocessing is the first page of the C code compilation phase. The scanning of the provided code and the modifications of the scanned code is done by the preprocessor. The compiler stage receives the code from the processor after the completion of the scanning and modifying process. The commands that are given to the preprocessor are known as the C Preprocessor directives. Hash symbol (#) is used in the beginning of the preprocessor directives. 

In the first step the preprocessor scans the complete code and identifies the token # (if any), when the preprocessor identifies a token it modifies the source code, these modifications are done by taking actions that are based on preprocessor directives. When the source code is modified, any preprocessor directives present are eliminated (the directives that begins with the hash symbol (#)). After the termination of the preprocessor directives the code is given to the compiler  to complete the process of compilation. 

Declaration of new preprocessor directives cannot be done by any programs as the set of directives names (valid) declared are fixed. Before an identifier (which is the name of the directive) hash symbols (#) are used. 

It is important to notice that there are no semicolons at the end of a preprocessor directive. If you want to declare multi line preprocessor directives then backslashes (\) are used for it. 

What are the functions of preprocessor directives?

The preprocessor directive used to include files is #include. 

Note: before the directive name the symbol hash (#) is used. 

When including files from standard directory the preprocessor #include<filename.h> is used. The path of the standard directory is defined in IDE (Integrated Development Environment).

#include is identified as a command by the preprocessor, also the file’s content is included in the C source code. Similarly, in embedded C the header files of the microcontroller are included. When the preprocessing process is completed the content from the header files are included in the course code. 

For example 

The most used program to test the microcontroller: The blinking of Led using 8051 microcontroller. 

#include<reg51.h> void main()
{
unsigned int x;
for(;;)
{
P2=0x55;
for( x=0;x<400;x++); {
} P2=0xAA;
for( x=0;x<400;x++); {
}
}
}

OUTPUT- When the execution process of the program is completed, the Led will start to blink alternatively at odd and even positions, after some delay.

In the following example the preprocessor directive #include is used to include the file reg51 from the standard directory. The .h is used for enabling the usage of C library by the I/O functions. 

Embedded C Program: Configuration of Port

There are several ports present in a microcontroller, and each port has its different pins. Interfacing devices are controlled with the help of these pins. Keywords are used by the developers for the declaration of the pins within the program. The declaration of the bits and single pins within the program is done with the help of standard (the embedded C program has these keywords predefined) keywords: bit, sbit, SFR etc. 

As mentioned earlier keywords are specific words that are already defined in the embedded C language, that are reserved to perform specific tasks. They are always mentioned or written in lowercase letters. It is important to declare these keywords before writing the main code of the program. 

Embedded C Program: sbit

When there is a need to access a single bit within the SFR register then sbit data types are defined. 

sbit variable name = SFR bit; is the syntax used to declare the variable of the sbit data type. 

For example: 

sbit x=P2^2;

The variable x is used to define p2.2, which means that the developers can now use the variable x in place of p2.2 in the code of the embedded program. The declaration of the variables helps in reducing the increasing complexity of the embedded program. 

Embedded C Program: bit

Bit data type is used when the bit is to be allowed to access the memory of the random access memory (RAM) like 20h to 2fh. 

name of bit variable; is the syntax used to declare the variable of the bit data type. 

For example: bit a; 

When the program is required to memorize data, then bit data types are being declared. The data region for the bit series is limited and smaller when compared to other data types of the embedded C. 

Embedded C Program: SFR

When the peripheral ports of the SFR register need to be accessed with the help of a substitutional name, then SFR data types are defined. Capital letters are used to declare the SFR registers. 

SFR variable name = SFR address for SFR register; is the syntax used to declare the variable of the SFR data type. 

For example: SFR P0= 0×81;

The allocation of the port 0x81 is done by the name P0, which means that we can use P0 anywhere in the embedded program to declare the port 0x81. This reduces the complexity in the embedded programming language. 

Embedded C Program: SFR Register

The RAM memory with 256 bytes of the 8051 microcontroller is divided into two important elements, where the first part is specifically used to store data and the second part is used for SFR registers. These two parts are equally divided and are 128 bytes each. The attached peripheral devices such as I/O ports along with timers and counters are secured in the SFR registers and a unique address is assigned to each and every element. 

Embedded C Program: Global Variables

It is very time consuming to declare a variable for each and every block of code, the above problem is eliminated by declaring the variable before declaring the key function of the program, these variables are referred to as global variables. The global variables can be used throughout the entire code and the lifetime of such variables exist until the embedded C programming reaches its end.

For example: 

#include<reg51.h>
Unsigned int x, y =5;
Main()
{

}

Embedded C Program: Main function

The main block of code from the embedded c program that is executed is the main function. The execution of an embedded C program starts from the main function. Usually while developing embedded C programs only one main function is written for the respective program because if there are more than one main functions then the compiler of the embedded C will get confused on countering the second or the other main functions. 

For example:

#include<reg51.h>
Main()
{
____
}

Embedded C Program: Declaring Variables

Developers can just not randomly use any character for the embedded C program as the compiler won’t understand the function of that specific character and prompt an error. To eliminate the above stated problem variables were intro used, when a variable is declared they can be used to store the values that can later be used in the coding of the embedded C program. But there are different variables that can be used such as numbers, characters and symbols. It is important for the compiler to know what type of variables are being declared. For this we deploy data types that are assigned with these variables allowing the compiler to analyze and implement the data that the respective variable carries. These data types are predefined in the Embedded C compiler. The data type is basically the representation of  the type of data being stored in the variable. The main data types (fundamental in nature) being used by the embedded C programming are integer, float, character etc. The size and the range of the data type depends upon the compiler being used for embedded c programming. 

The data type ‘char’ is mainly used to store single character values such as alphabets, symbols, etc. The data type ‘int’ is used to store any integer value. The data type ‘float’ is used to store values that are floating point. 

The ‘char’ is used to store any single character; ‘int’ is used to store integer value, and ‘float’ is used to store any precision floating-point value. 

For example:

Main();
{
int x, y, z;
}

Embedded C Program: Program’s logic

The logic of an embedded C program is defined by the theoretical study and predictable outcomes of the respective program. The logic is used to explain the statements that are declared in the embedded c program and the otherwise theory. They describe why the embedded C program will generate the output results when the program is exposed to a particular situation. 

For example:

Main
{
LED = 0x0f;
delay(250);
LED = 0x00;
delay(250);
}

Now we can understand the flow of an embedded c program.

comments (single line/ multiline)
preprocessor directives
global variables (mentioned before the main function)
main() function
{

local variables (mentioned after the main function)
statements
____
}

fun(a)
{

local variables
statements
____
}

What are the applications of the Embedded C Program?

With the continuous improvement of the embedded programming languages the applications of embedded C systems has increased. There are various different basic and other important uses of embedded c systems. 

One of main applications of the embedded system is in industries. They are used for different machines that are used in industries. Also, important devices such as speed checkers on the highways that are used to calibrate the current speed of the vehicles to ensure that no vehicles passes the speed limit for that particular highway uses embedded c systems, also traffic lights along with the street lights are controlled with the help of embedded systems, tracking of vehicles is also made possible due to embedded systems, embedded systems plays a important role in the field of artificial intelligence, systems that are auto intensity controlled are also operated with the help of embedded systems.

We hope that you are able to grasp the basic idea of embedded systems and embedded C programming. If you want to develop or work on embedded based programs it is very important to have a thorough knowledge of embedded C programming and how it is connected with embedded systems hardware. For more information you can visit our website Great Learning: Great Learning.

Avatar photo
Great Learning Team
Great Learning's Blog covers the latest developments and innovations in technology that can be leveraged to build rewarding careers. You'll find career guides, tech tutorials and industry news to keep yourself updated with the fast-changing world of tech and business.

Leave a Comment

Your email address will not be published. Required fields are marked *

Great Learning Free Online Courses
Scroll to Top