Basics of Embedded C Program and Programming Structure for Beginners

Embedded C Programming is the soul of the processor functioning inside each and every embedded system we come across in our daily life, such as mobile phone, washing machine, and digital camera.

Each processor is associated with an embedded software. The first and foremost thing is the embedded software that decides functioning of the embedded system. Embedded C language is most frequently used to program the microcontroller.

Embedded C Programming
Embedded C Programming

Earlier, many embedded applications were developed using assembly level programming. However, they did not provide portability. This disadvantage was overcome by the advent of various high level languages like C, Pascal, and COBOL. However, it was the C language that got extensive acceptance for embedded systems, and it continues to do so. The C code written is more reliable, scalable, and portable; and in fact, much easier to understand.

About C Language

C language was developed by Dennis Ritchie in 1969. It is a collection of one or more functions, and every function is a collection of statements performing a specific task.
C language is a middle-level language as it supports high-level applications and low-level applications. Before going into the details of embedded C programming, we should know about RAM memory organization.

Salient features of the language

  • C language is a software designed with different keywords, data types, variables, constants, etc.
  • Embedded C is a generic term given to a programming language written in C, which is associated with a particular hardware architecture.
  • Embedded C is an extension to the C language with some additional header files. These header files may change from controller to controller.
  • The microcontroller 8051 #include<reg51.h> is used.

The embedded system designers must know about the hardware architecture to write programs. These programs play prominent role in monitoring and controlling external devices. They also directly operate and use the internal architecture of the microcontroller, such as interrupt handling, timers, serial communication and other available features.

Differences between C and Embedded C

Differences between C and Embedded C
Differences between C and Embedded C

The basic additional features of the embedded software

Data types

The data type refers to an extensive system for declaring variables of different types like integer, character, float, etc. The embedded C software uses four data types that are used to store data in the memory.
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.
The size and range of different data types on a 32-bit machine is given in the following table. The size and range may vary on machines with different word sizes.

Data types
Data types

Keywords

There are certain words that are reserved for doing specific tasks. These words are known as keywords. They are standard and predefined in the Embedded C.
Keywords are always written in lowercase. These keywords must be defined before writing the main program. The basic keywords of an embedded software are given below:

Keywords
Keywords

sbit: This data type is used in case of accessing a single bit of SFR register.

  • Syntax: sbit variable name = SFR bit ;
  • Ex: sbit a=P2^1;
  • Explanation: If we assign p2.1 as ‘a’ variable, then we can use ‘a’ instead of p2.1 anywhere in the program, which reduces the complexity of the program.

Bit: This data type is used for accessing the bit addressable memory of RAM (20h-2fh).

  • Syntax: bit variable name;
  • Ex: bit c;
  • Explanation: It is a bit sequence setting in a small data area that is used by a program to remember something.

SFR: This data type is used for accessing a SFR register by another name. All the SFR registers must be declared with capital letters.

  • Syntax: SFR variable name = SFR address of SFR register;
  • Ex: SFR port0=0x80;
  • Explanation: If we assign 0x80 as ‘port0’, then we can use 0x80 instead of port0 anywhere in the program, which reduces the complexity of the program.

SFR Register: The SFR stands for ‘Special Function Register’. Microcontroller 8051 has 256 bytes of RAM memory. This RAM is divided into two parts: the first part of 128 bytes is used for data storage, and the other of 128 bytes is used for SFR registers. All peripheral devices like I/O ports, timers and counters are stored in the SFR register, and each element has a unique address.

The Structure of an Embedded C Program

  • comments
  • preprocessor directives
  • global variables
  • main() function

{

  • local variables
  • statements
  • …………..
  • …………..

}

  • fun(1)

{

  • local variables
  • statements
  • …………..
  • …………..

}

Comments: In embedded C programming language, we can place comments in our code which helps the reader to understand the code easily.
C=a+b; /* add two variables whose value is stored in another variable C*/

Preprocessor directives: All the functions of the embedded C software are included in the preprocessor library like “#includes<reg51.h>, #defines”. These functions are executed at the time of running the program.

Global variable

A global variable is a variable that is declared before the main function, and can be accessed on any function in the program.

Global variable
Global variable

Local variable

A local variable is a variable declared within a function, and it is valid only to be used within that function.

Local variable
Local variable

Main () function

The execution of a program starts with the main function. Every program uses only one main () function.

Advantages of embedded C program

  • Its takes less time to develop application program.
  • It reduces complexity of the program.
  • It is easy to verify and understand.
  • It is portable in nature from one controller to another.

Examples of a few Embedded C Programs

The following are a few simple Embedded C programs used for microcontroller-based projects.

Example-1
Example-1
Example-2
Example-2
Example-3
Example-3
Example-4
Example-4

We hope that we have been successful in providing an easy and approachable way for the beginners of Embedded C programming. Better understanding of the Embedded C programming is the most essential prerequisite for designing embedded based projects. In addition to this, a better understanding and proper knowledge about embedded C programming help students immensely in the selection of a rewarding career.
We encourage and welcome queries, suggestions and comments from our readers. Therefore, you can post your queries and feedback about this article in the comments section given below. Follow the below link for: Solderless projects

Photo Credit:

44 Comments

  1. Hi, thanks for this useful information, but I have some doubts about the data-types size you’re showing. I guess they fit better with a 16 bit word machine, tell me if I’m wrong. And also with the keywords, are they specific for the 8051 processor?. Have a nice day and thank you in advance.

    1. Tarun Agarwal says:

      Hi Ivan
      Thank you so much for your feedback
      For any technical queries or for customization of projects please email us on team@elprocus.com

  2. sridharan says:

    its really a nice explanation for the beginners who are interested in embedded good work.

  3. Leeladhar Muchhal says:

    Hii sir what is the mean of 0x00

  4. Anurag Singh says:

    Thanks for this nice and useful example.

  5. very good and clear explanation

  6. Akshay Wagh says:

    I think this is one of the best article for understanding embedded programming in easy way………….

    Thanks

    1. Tarun Agarwal says:

      Hi Akshay Wagh
      I sincerely appreciate your kind response regarding my article

      And once again please visit our domestic website https://www.elprocus.com
      For any technical queries or for customization of projects please email us on team@elprocus.com

  7. amit jangid says:

    it’s nice article for understanding embedded programming in easy way………….
    than you sir!

    1. Tarun Agarwal says:

      Hi Amit Jangid
      I sincerely appreciate your kind response regarding my article
      And once again please visit our domestic website https://www.elprocus.com
      For any technical queries or for customization of projects please email us on team@elprocus.com

  8. Happy.Arava says:

    Hi,This is Happy.
    I wanna learn Embedded Systems Programming in C-Language.
    So,Please guide me to do it…

    1. Tarun Agarwal says:

      Hi Happy
      I sincerely appreciate your kind response regarding my article
      And once again please visit our domestic website https://www.elprocus.com
      For any technical queries or for customization of projects please email us on team@elprocus.com

  9. Nice explanation, can u please explain about timers and delays concept

  10. jatin sharma says:

    do you have project on electronic weigh machine on 8051 ,inerfaced with load cell and 7-segment output.pls let me know if you have that …

    1. Tarun Agarwal says:

      Hi Jatin Sharma
      We are sorry to say that we do not have the exact project of your requirement.
      For any technical queries or for customization of projects please email us on team@elprocus.com

  11. thanks ..buddy it really helps me…..

  12. Syed Naseer says:

    Hi sir,
    it’s really very useful info for the beginners of embedded C programming..

  13. nice explanation……..every program also very clear to understand… steps for doing program also nice explanation

  14. Vishnu jadhav says:

    Thank you so much sir,
    This is exact tutorial which i was looking for..!!

  15. How to attach an IC to the computer for implementing the programme into the chip?

  16. hi sir,
    can u plz suggest me embedded c code for lpc2148 microprocesser.

  17. hi sir can u suggest embeded c code for lpc2148 micro processor

    1. Tarun Agarwal says:

      Hi Chandu
      We are very sorry to inform you that Once you purchase the project we can provide the source code, documentation, audio & video visuals
      And once again please visit our website https://www.elprocus.com
      For any other technical queries or for customization of projects please email us on team@elprocus.com

    1. Tarun Agarwal says:

      Hi Joby,
      Thanks for your valuable appreciation.
      And also,please send us your requirements to our official mail id:team@elprocus.com

  18. ineed code in c&c++ 1-position sensor tirgger hall effect X_position at 70 action happening
    2-seed sensor y_rpm calculate frequency
    3-pwm
    4-mass sensor meassurement
    5-temperature sensor
    all above to be ready to burn source file

    1. Tarun Agarwal says:

      Hi Adil

      Regret
      We are very sorry to inform you that we can’t provide the circuit diagram, documentation, coding program part but, once you purchase the project we can provide.
      For any other technical queries or for customization of projects please email us on team@elprocus.com

  19. Ashish Patil says:

    nice explaination. it clears the basic things. really helpfull…

    1. Tarun Agarwal says:

      Hi Ashish Patil,
      Thanks for your Appreciation.

    2. super explanation i can understand very easily

      1. Tarun Agarwal says:

        Hi Bharathi
        Thanks for your compliment
        once again please visit our website https://www.elprocus.com
        For any other technical queries or for customization of projects please email us on team@elprocus.com

  20. aman pandey says:

    can u please give the code

    1. Tarun Agarwal says:

      Hi Aman Pandey,
      We are sorry to say that we can not provide the code and documentation unless you buy a project from us.
      For customization of projects please email us on team@elprocus.com

  21. really nice explanation….helpful to begginer…and i really appritiate you because u are not motivated by money for this stuff..

    1. Team Elprocus says:

      Hi Konkyo,

      Thanks for your appreciation.

Add Comment