Home » C programming language

C programming tips for Embedded Development



Tips for Embedded Development in C: C programming language is widely used programming language; it’s specially designed for low level programming (Embedded programming). This article contains some of the best Tips that may help you for embedded development.
Submitted by IncludeHelp, on May 18, 2018

Tips for Embedded Development in C

Let’s understand what’s an Embedded Development?

Development of Softwares, Device Drivers for Electronics Devices to control the Hardware/Machine is known as Embedded Development.

C programming language is the most popular programming language that is used to develop embedded systems, the version of the C for Embedded Development is known as Embedded C programming.

Quick links:

  1. Assignment of Binary, Octal and Hexadecimal Numbers
  2. Printing the value of a variable in different format
  3. signed and unsigned data types
  4. Use of ‘for(;;)’ or ‘while(1)’ as infinite loop
  5. Quickly switch (updating variable with two values like 0 and 1) the value (flags) using Bitwise XOR (^) operator
  6. Setting and Clearing bit of a number
  7. Initialize arrays (integers, floats, characters etc) with default values quickly

1) Assignment of Binary, Octal and Hexadecimal Numbers

In C programming language we can assign binary, octal and hexadecimal values (Generally, we assign Decimal values), this way of initialization/assignment is helpful to the Embedded programmers.

An embedded programmer writes code which directly interact with the system/hardware/machine i.e. we can say he writes code to instruct the machine’s component.

Examples of embedded codes are: writing drivers, memory management related codes, designing compilers, designing firmware etc.

While writing a low level code, we need to use binary and hexadecimal values because machine level instruction are written in the form of binary, hexadecimal.

How to assign Binary, Octal and hexadecimal values?

A decimal value can be used without any prefix character, but other types need prefix characters, like...

  1. Binary value: A binary value can be assigned/representedby using "0b" (Zero, lowercase character ‘b’). Example: 0b00001010.
  2. Octal value: An octal value can be assigned/representedby using "0" (zero). Example: 0753.
  3. Hexadecimal value: A hexadecimal value can be assigned/represented by using "0x" or "0x" (zero, lowercase or uppercase character ‘x’). Example: 0x123ABC, 0x62726, 0Xaab345.

Example:

#include<stdio.h>

int main(void) 
{
	int value;
	
	//assigning value in Decimal format
	value = 3624;
	
	//printing value in decimal, octal and Hexadecimal format 
	printf("Decimal: %d, octal: %o, Hexadecimal: %x\n",value,value,value);
	
	//assigning value in binary format
	value = 0b111000101000;
	//printing value in Decimal, octal and Hexadecimal format 
	printf("Decimal: %d, octal: %o, Hexadecimal: %x\n",value,value,value);
	
	//assigning value in Octal format 
	value = 07050;
	//pringing value in Decimal, Octal and Hexadecimal format
	printf("Decimal: %d, octal: %o, Hexadecimal: %x\n",value,value,value);
	
	//assigning value in Hexadecimal format
	value = 0xe28;
	//printing value in Decimal, Octal and Hexadecimal format
	printf("Decimal: %d, octal: %o, Hexadecimal: %x\n",value,value,value);
	
	return 0;
}

Output

    Decimal: 3624, octal: 7050, Hexadecimal: e28
    Decimal: 3624, octal: 7050, Hexadecimal: e28
    Decimal: 3624, octal: 7050, Hexadecimal: e28
    Decimal: 3624, octal: 7050, Hexadecimal: e28



2) Printing the value of a variable in different format

Following format specifiers are used to print values in specified format, like...

  1. Binary - There is no defined format specifier to print the value in Binary format.
  2. Octal - "%o" is used to print the value in Octal format.
  3. Decimal - "%d" is used to print the value in decimal format.
  4. Hexadecimal - "%0x" or "%0x" is used to print the values in Hexadecimal format.

3) signed and unsigned data types

A signed data stores negative and positive both type of values, while unsigned type can store only positive values.

How to calculate maximum value of a unsigned type?

If a signed data type can store value from - MIN to MAX, then a unsigned data type can store 0 to (MAX*2)+1.

Consider an example of char data type: A singed char/char value from: – 128 to 127, while an unsigned char can store values from 0 to 255, which is 0 to (MAX*2)+1.

signed declaration:

    signed char var1;
    char var2;

Here, var1 and var2 both are signed character type variables. Note that, variable declaration without using signed or unsigned is considered as signed type.

Why it is important for the Embedded Development?

While working on the boards/machines (with the device drivers) most of the values are 1 byte, 2 bytes or 4 bytes positive integers. So what’s the need to keep space for negative values? Most of the values in the embedded system take 1 bytes i.e. 8 bits.

So, in that case, you can use unsigned char, it will store value from 0 to 255 (in hexadecimal, 0x00 to 0xFF).

A new programmer declares an integer variable to store the integer type of value without considering the range of the value to be stored.

Remember that an integer variable is not a solution for integer type of values. Analyze the value range and declare variable according to them. There are many other data types like, char, unsigned char, short and unsigned short

Data type (with signed and unsigned) Required memory Value rang
char 1 byte (8 bits) -128 to 127
unsigned char 1 byte (8 bits) 0 to 255
short 2 bytes (16 bits) -32, 768 to 32767
unsigned short 2 bytes (16 bits) 0 to 65,535



4) Use of ‘for(;;)’ or ‘while(1)’ as infinite loop

In Embedded system, we need a main loop which must be executed infinite times. The old method is to run an infinite loop is:

    START:
	    System_init();
	    ...;
	    ...;
	    Other_statements();
	    ...;
	    ...;
	    goto START;    

In C, C++ and even other programming languages, we should not use goto statement to reach at a particular statement, due to its behavior (more jumps from one statement to another may confuse the compiler or it will take more time to complete the task).

There is a standard way to run infinite loop is: to use loop statements with TRUE (non-zero number) condition.

Infinite for loop: (leave blank all three parts of loop statement)

    for(; ;)
    {
	    System_init();
	    ...;
	    ...;
	    Other_statements();
	    ...;
	    ...;
    }	   

Infinite while loop:

    while(1)
    {
	    System_init();
	    ...;
	    ...;
	    Other_statements();
	    ...;
	    ...;
    }	   

5) Quickly switch (updating variable with two values like 0 and 1) the value (flags) using Bitwise XOR (^) operator

Whenever we are using a single variable to maintain the flags, like after executing one part of code the value should be 1 and again it should be 0 and so on...

In that case, we can use XOR (^) operator...

Example:

#include <stdio.h>

int main()
{
	unsigned char flag =0;
	
	printf("Step 1: flag value is: %d\n",flag);
	//XORing
	flag ^= 1;
	printf("Step 2: flag value is: %d\n",flag);

	//XORing
	flag ^= 1;
	printf("Step 3: flag value is: %d\n",flag);	

	//XORing
	flag ^= 1;
	printf("Step 3: flag value is: %d\n",flag);	

	//XORing
	flag ^= 1;
	printf("Step 3: flag value is: %d\n",flag);	
	
	return 0;
}

Output

    Step 1: flag value is: 0
    Step 2: flag value is: 1
    Step 3: flag value is: 0
    Step 3: flag value is: 1
    Step 3: flag value is: 0

Only single statement using XORing is updating values to 0 and 1, flag ^= 1;




6) Setting and Clearing bit of a number

The quick way is to set or clear a bit is: [Read more: C program to set/clear (high/low) bits of a number]

    //setting a bit
    variable |= (1 << bit_index);
    //clearing a bit
    variable &= ~ (1 << bit_index);

Here, bit_index is the bit number (counting starts with 0)

Example:

#include <stdio.h>

int main()
{
	unsigned char value = 16;

	//binary of 16 is: 0001 0000
	printf("value is: %d\n",value);

	//not setting 0th bit
	value |= (1<<0);
	//value will be 0001 0001 = 17
	printf("value is: %d\n",value);

	//now clearing 4th bit
	value &= ~(1<<4);
	//value will be 0000 0001 = 1
	printf("value is: %d\n",value);    

	return 0;
}

Output

    value is: 16
    value is: 17
    value is: 1

7) Initialize arrays (integers, floats, characters etc) with default values quickly

If you are using any type of arrays, you can quickly initialize all elements while declaring them.

consider the example:

#include <stdio.h>
#define MAX	10

int main()
{
	int 	iarr[MAX]	={0};
	float 	farr[MAX]	={0.0f};
	char	carr[MAX]	={' '}; //initializing with space

	int     i;  //for loop counter

	//printing values

	printf("iarr: ");
	for(i=0; i<MAX; i++)
		printf("%d ",iarr[i]);
	printf("\n");

	printf("farr: ");
	for(i=0; i<MAX; i++)
		printf("%.02f ",farr[i]);	
	printf("\n");		

	printf("carr: ");
	for(i=0; i<MAX; i++)
		printf("%c ",carr[i]);		
	printf("\n");

	return 0;
}

Output

    iarr: 0 0 0 0 0 0 0 0 0 0 
    farr: 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
    carr:            

Since, there may thousands of Tips/hacks for the embedded development in C programming language. If you know other tips & tricks, which may helpful for the embedded developers. Please share in the comment.






Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions





© https://www.includehelp.com (2015-2018), Some rights reserved.




close Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.