Saturday, 4 June 2016

C Programming for Embedded Systems


Embedded Software is a key element in every embedded project that is used to run the micro-controller to perform the desired operations. In our daily life, we frequently use many electronic devices such as washing machines, refrigerators, mobile phones, security system, digital camera so on which will be controlled using embedded C program. If you press a button to take a photo with your digital camera, then micro-controller will perform the functions essential to capture the image and store it. This article presents basics of embedded systemsmicro-controller consists of many ports toconstruct the embedded C programming tutorial.

7-Steps to Building Embedded C Programming Tutorial

The embedded C programming is a collection of one or more functions. Every function is a collection of statements that are used to perform some specific tasks. The embedded C programming tutorial is similar to a C language is constructed with some basic elements such as character set, variables, constants, data types, keywords, variable declaration, statements, expressions etc. that are used to write the program easily. However, we are providing 7-steps with embedded C programming tutorial to easily write the program such as:
  1. Comments
  2. Preprocessor directives
  3. Port configuration
  4. Global variables
  5. main() function or core function
  6. Variable declaration
  7. Program Logic
7 Steps to Build Embedded C Programming Tutorial
7 Steps to Build Embedded C Programming Tutorial

Step1: Comments

The comments are important to the programming languages which describes function of program. Comments are non-executable code that is used to provide documentation to the program. The comments make an easy way to understand function of the program. There are two types of comments in embedded C programming tutorial such as:
  • Single Line Comment
  • Double Line Comment or Multi Line Comment

Single Line Comment

Generally single line comments are useful for the programming languages that can be used to explain a part of the code. The single line comments starts with double slash(//) which can be placed anywhere in the program. Single line comments are used to ignore complete line in a program.
Single Line Comment
Single Line Comment

Multi Line Comment

The multi line comments starts with single slash and an asterisk (/*) that can be used to explain a block of code. The multi line comments can be placed any where in the program. The multi line comments are used to ignore a complete block of code in a program.
Multi Line Comment
Multi Line Comment

Step2: Processor Directives

Preprocessor directives are lines integrated in the code of programs which can be followed by a hash sign (#). These lines are not programmed statements, but directives for the preprocessor. The preprocessor inspects the code before actual compilation of code begins and resolves all these directives before any code is actually generated by regular statements. Even though there are many different preprocessor directives, but two directives are very useful in the embedded C programming tutorial such as
  • #include
  • #define
P2Which can be called as a header file, containing C declarations and macro definitions to be shared between several source files. The #include directive is normally used to include standard library such as study. h that can be used to access I/O functions from the C library. The #define directive normally used to define the string of variables and to assign the values by performing the operations in a single instruction it can be defined as macros.

Step 3:Port Configuration

In every micro-controller consists of many ports, each port contains many pins which can be used to control the interfacing devices. These pins are declared in a program using keywords. The embedded C has consist standard and predefined keywords such as bit, sbit, SFR which can be used to declare the single pin and bits in a program.
Port Configuration
Port Configuration

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: name of bit variable;
  • 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 to get the SFR register peripheral pots 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=0×80;
  • Explanation: If we assign 0×80 as ‘port0’, then we can use 0×80 instead of port0 anywhere in the program, which reduces the complexity of the program.

SFR Register:

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

Step4: Global Variables

The variable declared before the main function is called a global variable, that can be accessed on any function in the program. The life time of the global variable depends on the program until program comes to an end.

Step5: Main Function or Core Function

The main function is a core of every program execution, starts with main function only. Every program uses only one main function because if program contains more than one main function, then the compiler will get confused where to start the program execution.
Main Fucntion
Main Fucntion

Step6: Variable Declaration

The variable is a name that can be used to store the values. That variable must be declared before used in the program. The declaration of a variable specifies its name and data type. The storage representation of data is called data type. The embedded C programming uses four basic data types such as float, integer, character, etc. used to store the data in the memory. The size and range of data type defined based on compiler.
Variable Declaration
Variable Declaration

Step7: Program Logic

The plan of path is called a program logic that presents the theory behind and expected outcomes of a program’s actions. It describes the assumption or theory about why the program will work, showing the acknowledged effects of activities or resources.
Program Logic
Program Logic

LED flash light Program

LED flash light Program
LED flash light Program
Hope this article gives basic information to the beginners of embedded C programming. Good understanding of the Embedded C programming is most essential for designing embedded based projects. 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.

No comments:

Post a Comment