IOT

A Quick Overview on Embedded System C Programming

One of the most well-liked and frequently employed programming languages in the creation of embedded systems is embedded C. Therefore, we will look at some of the Basics of the Embedded C Program and the Embedded C Programming Structure in this post.

The language most frequently used by embedded programmers to create embedded systems is probably embedded C. Assembly, BASIC, C++, Python, and other well-used programming languages are frequently used for creating embedded systems, but Embedded C is still widely utilized because of its effectiveness, speed of development, and portability.

We will first look at what an embedded system is and how important programming languages are in embedded systems before diving into the fundamentals of embedded C programming.

About  Embedded System?

The ideal way to define an embedded system is as a device that combines hardware and software to do a single purpose. A washing machine is an excellent illustration of an embedded system, which is a common home item.

We virtually always use washing machines, but we wouldn’t realise that they are embedded systems made up of software, hardware, and a processor.

Embedded-System-Example

It receives input from the user regarding the wash cycle, type of clothing, additional soaking and rinsing, spin speed, etc., then follows the instructions to complete the washing and drying of the garments. The washing machine will repeat the same set of duties as the previous wash if no new instructions are provided for the subsequent wash.

In addition to being standalone devices, like washing machines, embedded systems can also be a component of a much bigger system. A car is a good illustration of this. A modern car has a number of distinct embedded systems that each carry out their assigned functions in order to ensure a smooth and secure journey.

The Anti-lock Braking System (ABS), Temperature Monitoring System, Automatic Climate Control, Tire Pressure Monitoring System, Engine Oil Level Monitor, etc. are some of the embedded systems in a car.

Programming Embedded Systems

As was already established, hardware and software are both components of embedded systems. The processor is the primary Hardware Module of a basic Embedded System. The processor, which might be a microprocessor, microcontroller, DSP, CPLD (Complex Programmable Logic Device), or an FPGA, is the brain of an embedded system (Field Programmable Gated Array).

All of these gadgets have one thing in common: they are all programmable, which means that we can create a program (which is the software component of the embedded system) to specify how the gadget functions in practice.

Hardware can monitor external events (via inputs and sensors) and respond by controlling external devices (through outputs) thanks to embedded software or programs. Timers, Serial Communications Interface, Interrupt Handling, and I/O Ports, among other components of the internal architecture of the embedded hardware (often the processor), may need to be directly manipulated by the program for an embedded system during this process.

Numerous programming languages, including Assembly (a low-level programming language), C, C++, JAVA (a high-level programming language), Visual Basic, JAVA Script (an application-level programming language), etc. are used for embedded systems.

The programming of the system plays a crucial role in creating a better-embedded system, so choosing the right programming language is crucial

Factors for Selecting the Programming Language

The following are a few things to keep in mind while choosing a programming language for creating embedded systems.

  • Size: Since embedded processors like microcontrollers have a very limited quantity of ROM, the memory that the software occupies is vitally essential (Program Memory).
  • Speed: The programs must run quickly, or as quickly as possible. A slow-running software shouldn’t slow down the hardware.
  • Portability: The same program can be compiled for different processors.
  • Ease of Implementation
  • Ease of Maintenance
  • Readability

Assembly Language was mostly used in the development of embedded systems in the past. Although Assembly Language produces small-sized hex files and is the closest to actual machine code, its lack of portability and the significant amount of resources (time and labor) required to develop the code made it challenging to use.

Other high-level programming languages existed that had the aforementioned features, but none came close to C. The following are some advantages of choosing Embedded C as your primary programming language:

  • Significantly easy to write code in C
  • Consumes less time when compared to Assembly
  • Maintenance of code (modifications and updates) is very simple
  • Make use of library functions to reduce the complexity of the main code
  • You can easily port the code to other architecture with very few modifications

Introduction to Embedded C Programming Language

We will first discuss the C programming language before delving into the specifics of embedded C programming and the fundamentals of embedded C programs.

The most famous and frequently used programming language is C, which Dennis Ritchie created in the late 1960s and early 1970s. The C programming language enabled efficient translation to machine instructions and low level memory access using a simple compiler (a piece of software that transforms programmes into machine code).

The C programming language has become so well-liked that it is utilized in a variety of applications, from supercomputers to embedded systems.

An extension of the C programming language is embedded C programming language, which is frequently used in the creation of embedded systems. The main function, datatype declarations, defining variables, loops, functions, statements, and other programming constructs found in the C programming language are all used in the embedded C programming language in the same syntax and semantics.

Embedded C differs from an ordinary C programming language in a number of ways, including I/O Hardware Addressing, fixed point operations, access to address spaces, etc.

Difference between C and Embedded C

Apart from a few extensions and the operating environment, C and Embedded C truly have few differences. Since C and Embedded C are both ISO Standards, their syntax, datatypes, functions, etc. are essentially identical.

The Standard C Programming Language is effectively expanded with extra capabilities like Addressing I/O, Multiple Memory Addressing, Fixed-Point Arithmetic, etc. in Embedded C.

In contrast to Embedded C, which is used to create applications for microcontrollers, C is typically used to create desktop apps.

Basics of Embedded C Program

We will now go into the fundamentals of an embedded C program after learning a little bit more about embedded systems and programming languages. We’ll start with Keywords and Datatypes, two of the fundamental components of an embedded C programme.

Keywords in Embedded C

A keyword is a unique word that has a particular significance to the compiler (a C Compiler for example, is software that is used to convert programs written in C to Machine Code). For instance, the following are some of the keywords for Keil’s Cx51 Compiler, a well-liked C compiler for microcontrollers based on the 8051 architecture:

  • bit
  • sbit
  • sfr
  • small
  • large

The following table lists out all the keywords associated with the Cx51 C Compiler.

_at_ alien bdata
bit code compact
data far idata
interrupt large pdata
_priority_ reentrant sbit
sfr sfr16 small
_task_ using xdata

Data Types in Embedded C

Any programming language, including C, has data types that assist us in declaring variables in our programs. The C programming language supports a wide variety of data types, including signed int, unsigned int, signed char, unsigned char, float, double, etc. There are a few other data types in Embedded C in addition to these.

The additional data types in Embedded C connected to Keil’s Cx51 Compiler are listed below.

  • bit
  • sbit
  • sfr
  • sfr16

Some of the Cx51 Compiler’s data types are displayed in the following table along with their respective ranges.

Data Type Bits (Bytes) Range
bit 1 0 or 1 (bit addressable part of RAM)
signed int 16 (2) -32768 to +32767
unsigned int 16 (2) 0 to 65535
signed char 8 (1) -128 to +127
unsigned 8 (1) 0 to 255
float 32 (4) ±1.175494E-38 to ±3.402823E+38
double 32 (4) ±1.175494E-38 to ±3.402823E+38
sbit 1 0 or 1 (bit addressable part of RAM)
sfr 8 (1) RAM Addresses (80h to FFh)
sfr16 16 (2) 0 to 65535

Basic Structure of an Embedded C Program (Template for Embedded C Program)

The fundamental organization, or Template, of an embedded C program is the next concept to comprehend in the Basics of Embedded C Programming. This will enable us to comprehend the construction of an embedded C program.

The following part shows the basic structure of an Embedded C Program.

  • Multiline Comments . . . . . Denoted using /*……*/
  • Single Line Comments . . . . . Denoted using //
  • Preprocessor Directives . . . . . #include<…> or #define
  • Global Variables . . . . . Accessible anywhere in the program
  • Function Declarations . . . . . Declaring Function
  • Main Function . . . . . Main Function, execution begins here
    {
    Local Variables . . . . . Variables confined to main function
    Function Calls . . . . . Calling other Functions
    Infinite Loop . . . . . Like while(1) or for(;;)
    Statements . . . . .
    ….
    ….
    }
  • Function Definitions . . . . . Defining the Functions
    {
    Local Variables . . . . . Local Variables confined to this Function
    Statements . . . . .
    ….
    ….
    }

Before seeing an example with respect to the 8051 Microcontroller, we will first see the different components in the above structure.

Different Components of an Embedded C Program

Comments are a readable language that is written to make it easier for us (the reader) to understand the code. The compiler ignores them, and the final code does not use any memory for them (after compilation).

You can add comments in two different ways: as single lines represented by / and as multiline comments denoted by /*….*/.

Preprocessor Directive: In Embedded C, a preprocessor directive instructs the compiler to search this file for symbols that are not defined in the program.

Preprocessor directives are typically represented in the C programming language (and in Embedded C) by the # symbol, such as #include… or #define….

Preprocessor directives are typically used in embedded C programming to designate a header file that is specific to the microcontroller and contains all the SFRs and their associated bits.

Every embedded C program for the 8051 must include the file “reg51.h,” which is available in the Keil Compiler.

Global Variables: Global Variables are accessible from anywhere in the program because, as their name implies, they are global to the program.

Local variables are restricted to their specific function, as opposed to global variables.

Every C or Embedded C program has a main function, which is where the program’s execution starts.

Basic Embedded C Program

We have already covered some of the fundamentals of embedded C programming, such as the distinction between C and embedded C, the basic framework or template of an embedded C program, and its various parts.

As we move forward, we will use an example to explore the fundamentals of embedded C programming. In this example, we’ll use an 8051 microcontroller to control the blinking of LEDs that are connected to PORT1 on the device.

Example of Embedded C Program

The circuit diagram for the example circuit can be seen in the image below. It includes an 8051-based Microcontroller (AT89S52) and all of its essential parts, including those for blinking LEDs and the RESET and oscillator circuits (LEDs and Resistors).

Basics of Embedded C Program Image 2

We will use the Keil C Compiler to create the embedded C programme for the circuit mentioned above. The Keil Vision IDE includes this compiler. The schedule is displayed below.

Code

#include<reg51.h> // Preprocessor Directive
void delay (int); // Delay Function Declaration
void main(void) // Main Function
{
P1 = 0x00;
/* Making PORT1 pins LOW. All the LEDs are OFF.
 * (P1 is PORT1, as defined in reg51.h) */
while(1) // infinite loop
{
P1 = 0xFF; // Making PORT1 Pins HIGH i.e. LEDs are ON.
delay(1000);
/* Calling Delay function with Function parameter as 1000.
 * This will cause a delay of 1000mS i.e. 1 second */
P1 = 0x00; // Making PORT1 Pins LOW i.e. LEDs are OFF.
delay(1000);
}
}
void delay (int d) // Delay Function Definition
{
unsigned int i=0; // Local Variable. Accessible only in this function.
 
/* This following step is responsible for causing delay of 1000mS
 * (or as per the value entered while calling the delay function) */
for(; d>0; d–)
{
for(i=250; i>0; i – -);
for(i=248; i>0; i – -);
}
}

Conclusion

I hope all of you had understand the basics of Embedded C programming. We MATHA ELECTRONICS will be back soon with more informative blogs soon.

Leave a Reply

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