Open In App

Embedded C

Last Updated : 26 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will explore Embedded C in comparison with the C language. We will elaborate on its key characteristics, and delve into its structure and data types. Additionally, we will examine its block diagram and programming steps. Furthermore, we will explore advanced techniques associated with Embedded C. Towards the end, we will review examples, advantages, and disadvantages of Embedded C.

What is Embedded C?

Embedded C is a programming language that is used in the development of Embedded Systems. Embedded Systems are specialized systems designed to perform very specific functions or tasks. Embedded System is the combination of hardware and software and the software is generally known as firmware which is embedded into the system hardware. Embedded C is used to program a wide range of microcontrollers and microprocessors. Embedded C requires less number of resources to execute in comparison with high-level languages such as assembly programming language.

Embedded C has some additional data types and keywords. There are some special datatypes in Embedded C like sbit, sfr which are used for addressing special function registers in memory. Embedded C allows us to work with hardware devices like sensors, and input-output devices. There are various Embedded C compilers to compile the embedded C program such as Keil Compiler, SPJ Compiler, Embedded GNU C Compiler, etc. Embedded Systems can be classified into small-scale, medium-scale, and sophisticated embedded systems. The devices like air conditioners, printers, and mobile phones that we use in our daily lives are programmed by embedded C.

Difference between C and Embedded C

Parameter

C

Embedded C

Definition

C is a versatile programming language that supports structured programming

Embedded C is a set of language extensions for the C programming language designed to program microcontrollers

Development

C is developed by Dennis M. Ritchie

Embedded C is developed by C Standards Committee

Hardware dependency

C language is hardware independent

Embedded C is hardware dependent language

Compiler Execution

A standard compiler facilitates the compilation and execution of a program.

Compilers that are capable of generating microcontroller based output needs to be used to execute Embedded C code

Functionality

C language generates operating system dependent executable files

Embedded C generates hardware dependent files

Applications

Network drivers, interpreters, compilers, operating system and text editors are some of the applications

Robots, Vehicle tracking systems, smart monitoring systems are some of the applications.

Key Characteristics of Embedded C

Efficiency: In Embedded C we can create a efficient code to optimize the limited resources available in embedded systems. It aims to minimize memory usage and maximize performance.

Direct Hardware Interaction: Embedded C allows programmers to interact directly with hardware components, such as microcontrollers, sensors, actuators, and other peripherals. This direct interaction facilitates precise control over the hardware, critical in embedded applications.

Low-level Programming: Embedded C involves low-level programming, which deals with hardware-specific details like memory addresses, I/O ports, and register manipulation. This level of control is essential for efficiently managing hardware resources.

Real-time Operations: Embedded systems often operate in real-time environments, requiring precise timing and response to events. Embedded C allows programmers to handle real-time tasks efficiently.

Structure of Embedded C Program

  • Comments :Comments are readable text written to help user understand the code easily. They are ignored by compiler and do not take up any memory in the final code. There are two types of comments, Single line comments and Multiline comments.
  • Preprocessor Directive :In Embedded C Preprocessor Directives are represented using #include or #define.Preprocessor Directives are used to indicate a header file specific to a microprocessor or microcontroller which contains all the functions, SFR’s and the bits in those SFR’s. reg51 header file is used in case of 8051 microcontroller.
  • Global Variables :Global variables as the name suggests are global to program that is they can be accessed any where in the program. Global variables are static variables and are placed in RAM memory locations.
  • Local Variables :Local variables in contrast to global variables are confined to their respective functions. Normally these variables are placed in stack or registers. It is only valid within the function in which it is declared.
  • Function :Function is a group of statements that together performs a task. A function declaration tells the compiler about the name, return type and parameter of the function. A function definition provides actual body of the function.
  • Main Function :Every Embedded C program has one main function and may contain one or more functions in the main functions. The program execution starts from the main function and it is a core of every execution. If more than one main function is written in the code then compiler will confuse from where to start the program execution.

Standard Embedded C Data Types

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

embedded-

Block Diagram Explanation of Embedded C

Problem :Problem refers to a challenge or task which needs to be addressed by programming. The problem defines the purpose and functionality of the Embedded System. The necessary coding solutions should be achieved by understanding the problem statement. It includes functional requirements, non-functional requirements, Hardware Interfacing and Expected Output.

Algorithm :Algorithm is a set of instructions that tells us that how the task should be executed in step-wise manner in order to solve the problem. Algorithm helps us to visualize and design the logic before implementation of actual program. It is written in step by step order. Let’s take a look at an example for better understanding.

Algorithm to add two numbers and store the result :

  1. Make the Port 1 and Port 2 as Input port .
  2. Take the data from Port 1.
  3. Take the data from Port 2.
  4. Add the content of Port 1 with Port 2
  5. Send result of addition to Port 3.

Flowchart : A flowchart is a graphical representation of an algorithm which shows the steps to be followed using some symbols and arrows. Flowchart makes it easy to understand the structure of program before coding by visualizing the flow of logic. It shows decision points such as conditional statement, loops and code terminations. As it uses symbols and shapes, debugging is easy and it takes less efforts in writing the logic of a program. Flowchart and Algorithm helps in identifying logical errors while troubleshooting the code which is a difficult process in itself.

Compilation and Uploading of Code :Generally programmers write code using high level languages like C where human readable syntax is used. This language is not understandable by CPU of computer. We need to translate it into machine level code which a CPU can easily understand. So a compiler needs to be used for this purpose. Compiler is a specialized software tool which translates the human readable code (source code) into machine code (binary code) according to the specific microcontroller architecture.

The machine code is executed by the CPU by carrying out the instructions step-by-step to perform the tasks written in the actual code written by programmer/use. Machine code consists of instructions which are understandable by the computer’s CPU. It is a string of 0’s and 1’s representing logical, arithmetic operations. A hex file is generated after compilation process that contains the machine code which is uploaded in microcontroller’s flash memory through programmer/debugger. After uploading, the code is tested and verified for correct operation of Embedded System. In this way the solution for the problem statement is achieved in Embedded C

Basic Embedded C Programming Steps

  • Requirement Analysis :Understanding the requirements of the Embedded System to be developed according to the problem | requirement should be done.
  • Selecting Environment Setup :Selection of proper tools such as choosing Integrated Development Environment (IDE) ,compiler, debugger and other necessary tools for Embedded C Programming.
  • Code Development :Writing the Embedded C code based on the system requirements and design specifications must be done in this step. The program should consume less memory space, must be reliable and scalable.
  • Compilation Process :In this stage the compiler translates the embedded C code into assembly language code or machine level code. The machine level code is in the form of 0’s and 1’s. Also the preprocessor handles the directives such as #include, #define.
  • Loading to Target device :Uploading the compiled code onto the target hardware ( microcontroller, FGPA ) using tools like debugger or flash programmers needs to be done.
  • Execution and Debugging :Run the embedded system and execution of code is performed. Employing debugging tools to identify and resolve any errors or issues in the code.
  • Documentation and Maintenance :Creating proper documentation detailing the system architecture, code functionalities and memory usage. Periodically updating and maintaining the codebase to address issues.

Advanced Techniques for Embedded C

  • Pointer manipulation: Pointers in C are powerful but can be complex. They allow direct access to memory locations, aiding in efficient data manipulation. Understanding pointer arithmetic, dynamic memory allocation (malloc/free), and using pointers accessing hardware registers or structures are crucial in embedded C programming.
  • Interrupt handling: In embedded systems, interrupts are used to handle asynchronous events. Mastering ISR involves understanding how to write interrupt service routines, handle interrupt priorities, manage shared resources, and minimize interrupt latency to ensure timely response to events.
  • RTOS (Real-Time Operating Systems): RTOS facilitates multitasking within embedded systems. Understanding concepts like task scheduling, context switching, inter-process communication (IPC), and synchronization mechanisms (semaphores, mutexes) is very much important for developing real-time embedded applications.
  • Peripheral Interfacing: Embedded systems interact with various peripherals. Knowledge of communication protocols (UART, SPI, I2C), handling GPIO pins, configuring timers, and managing interrupts related to peripherals is important for effective interfacing.
  • Low-power Optimization: Embedded devices often run on limited power. Techniques to reduce power consumption involve utilizing low-power modes provided by microcontrollers, selectively shutting down unused peripherals, and optimizing algorithms for energy efficiency.
  • Memory Management: Embedded systems have limited memory. Efficiently managing memory includes minimizing memory fragmentation, choosing appropriate data types, implementing memory pooling, and handling dynamic memory allocation carefully to avoid memory leaks.
  • Debugging and Testing: Embedded system debugging involves using hardware debuggers, emulators, simulators, and printf-style debugging.

Embedded C Program Examples

1. Write a Program to read the number 1 from port 1, number 2 from port 2 , then add them ,store the result ,send it to Port 3.

C




#include<reg.51.h>
void main()
{
unsigned char a,b,c ;
P1 = 0XFF ;     //make port 1 as input port
P2 = 0XFF ;     //make port 2 as input port
a=P1;
b=P2;
c= a+b ;
P3= c;
}


2. Write a program to Turn on and off the LED with some delay.

C




#include<reg51.h>
sbit LED=P1.1;
void delay(void);
void main(void)
{
while(1)
{
LED=1;
delay();
LED=0;
delay();
}
}
 
void delay(void)
{
unsigned char i,k;
for(i=0;i<70;i++)
for(k=0;k<255;k++);
}


3. Write a program to transfer the data from port P0 to port P1.

C




#include<reg51.h>
void main (void )
{
unsigned char X;
P0=0XFF;    // P0 as input port
P1=0X00;   //  P1 as output port
while(1) 
{
X = P0;    //  read port0
P1 = X;   // output data to port1
}


Advantages of writing a program in Embedded C

  • It is easy and less time consuming to write code in Embedded C instead of assembly programming language.
  • Embedded C program is easier to modify and update.
  • Code available in function libraries can be used by the programmer.
  • Embedded C code is portable to other microcontrollers with little or no modifications.
  • Embedded C code tends to be more readable and maintainable than assembly language.

Disadvantages of Embedded C Programming

  • It can only perform one task at a time, it cannot perform several activities. We need to update the hardware if we changed the application.
  • Only the hardware system is supported.
  • It is not scalable, scalability is an problem with Embedded C.
  • It has limitations such as restricted RAM which affects the computer’s compatibility.

Conclusion

In summary, Embedded C is a special kind of C programming language that’s super important for embedded systems. In this article we will have seen how Embedded C is different from regular C, we gone through its basic data types, and highlights its main features. we have seen how crucial Embedded C is for getting the most out of embedded systems. By looking at its pros and cons, it’s clear that this language gives a lot of control and works really efficiently, even though it can be tricky to use in systems with limited resources. Whether you’re just starting out or you’re an expert, using Embedded C helps programmers deal with hardware and time constraints, making strong and efficient apps. As technology changes, Embedded C stays super adaptable and powerful, making it a big player in shaping the future of embedded systems.

FAQs on Embedded C

1. How is memory management handled in Embedded C ?

Memory management in embedded C is crucial task due to reasons such as limited resources. Techniques like static memory allocation or memory mapping I|O is done to efficiently manage memory in Embedded C.

2. How are timing operations performed in Embedded Systems using Embedded C ?

Timing and Counting operations in Embedded system are performed using in-built timer in microcontroller. In case of 8051 microcontroller there are two 16-bit timers, Timer 0 and Timer 1. TMOD register, a 8-bit register is used to set the various timer operation modes for both timers.

3. What is the role of interrupts in Embedded C Programming ?

A interrupt plays an important role in embedded systems by allowing the processor to give service to external events by setting priorities. An interrupt represents an external or internal occurrence that interrupts the normal operation of a microcontroller, signaling that a device requires attention. Each interrupt necessitates the presence of an Interrupt Service Routine (ISR) or handler. Upon occurrence of an interrupt, the microcontroller executes the corresponding interrupt service routine.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads