Basics of C language

 

Body of the C code for embedded systems

#include<reg51.h> \\ header file used for 8051            
                                   \\microcontroller
delay(unsigned int )\\ function prototype
void main()               \\ initializing the main function
{  

            P1=0x00;                   \\body of the main
                                              \\ program
           delay(65000);
            P1=0x0AA;
            delay(65000);                  
        }
    
      delay(unsigned int x)   \\subroutine for delay function
      {
           unsigned int a;          \\variable declaration
           a=x;
           while(a!=0)
              {              \\while loop
                  a--;
              }                              \\deccrement operator
       }

Loops used in C :

Super loop or forever loop:
while(1)
for(; ;)

                 This loop will run infinite time.
This loop is used to  repeat the same code infinite 
 times.

         while(1)  {
code which is repeated again and again.
     }

       for(; ;){
Statement
    }

While loop:

The code written in this loop will run if until the
Condition is true.

For example:as used in delay function.

while(a!=0){
a--;}

               This loop will run until a is non zero.

In embedded  system this loop is also used for waiting .

For example :

while(RI==0);

The MC will wait here until RI becomes 1.    
  
For loop:

The body of the for loop is shown below.

for(i=0;i<10;i++)
            {
    statement;
             }

Where i=0 –lower limit
      i<10 – upper limit
      i++  - operation

if else statement:

This is the conditional statement .
The body of this statement is shown below.

if(statement 1)
              {
      Statement 2
               }
else 
               {
    Statement 3
                }

In this case if statement 1 is true then statement 2 will run or if it is not true then       
               statement 3 will run.

Switch statement:

Switch is a multiway decision statement that tests
            whether a expression matches one of a number of
 constant integer values.

Body of the switch statement is –

Switch(expression)
             {
      case1: statement 1
      case2: statement 2
      case3: statement 3
            }


Arrays:
There are two types of arrays
1.one dimensional array
2.two dimensional array

1.   One dimension array:

One dimensional array is also known as string. Body of string is-

Unsigned char name of the string[size of string]= “message”;

    How to use string in embedded system programing
    is shown below-
  
void main()
{
unsigned char str[20]= “Manpreet Singh Akali”;
unsigned char i;
for(i=0;i<20:i++){
P0=str[i];}
}

This program will send the string to port 0.


2.   Two dimension array:

Two dimension array consists of rows and columns.

Unsigned char array[a][b]={‘a’, ‘b’, ‘c’,
                                              ‘d’, ‘e’, ‘f’,
                                              ‘g’, ‘h’, ‘I’};
To send any value to port0 from this array we write

P0=array[a][b];

Where a,b are two integers representing the row and column of the value in array.




Functions :

C language is a mid level language with nhigh level features . It also supports the function .

For example:

strcmp(s1,s2):
this function is used to compare two strings s1 and s2.



This function helps in setting the password in embedded system applications.




For more details contact:

Comments

Popular Posts