You are on page 1of 17

Embedded C

Compilers produce hex files that is


downloaded to ROM of microcontroller

It is easier and less time consuming to write in


C than Assembly.

C is easier to modify and update.

 C code is portable to other microcontroller


with little of no modification
Getting Started With C
include headers; these are modules
#include<Reg51.h> that include functions that you may
use in your
program;

Global variable declaration region


Void main()
{ Local variable Declaration Region

Main body of the program

}
Variable declaration
syntax;
Data type variable name ;

Where type can be:


– int //integer
– double //real number
– char //character
Example:
int a, b, c;
double x;
int sum;
char my-character;
Variables
C has the following simple data types:
If statements
True False
if (condition) { condition

S1;
S1 S2
}
else {
S2; S3

}
S3;
Boolean conditions
• Comparison operators
== equal
!= not equal
< less than
> greater than
<= less than or equal
>= greater than or equal
• Boolean operators
&& and
|| or
! not
Examples
Assume we declared the following variables:
int a = 2, b=5, c=10;

Here are some examples of Boolean conditions


we can use:
• if (a == b) …
• if (a != b) …
• if (a <= b+c) …
• if(a <= b) && (b <= c) …
• if !((a < b) && (b<c)) …
These are three methods by way of which we
can repeat a part of a program in C

Using a for loop


Using a while loop
Using a do-while loop
For loop
syntax-:-

For(initialization exp; test exp; update exp)


{
Body of loop;
}
example-:-

For(i=0; i<2000; i++)


{
// Null body
}
While loop
syntax-:- example-:-

While(test exp) while(1) // infinite loop


{ {
Body of loop; // Null body
} }

This loop is used


When we want to execute an instruction
infinite time.
do while loop
syntax-:- example-:-
do
{ do
Body of loop; {
}while(test exp); P0=0x0f;
}while(i<100);

This loop is used where we have to


run a instruction at least once.
Functions

•A function is a block of code that has a


name and it has a property that it is
reusable i.e. it can be executed from as
many different points in a C Program as
required.

•main( ) is also a function.


Functions having 3 parts:-

1.Function declaration

2. function definition

3. function calling
1.Function declaration

Syntax:-

Return type function name (parameter data type parameter name);

Example:-

void delay (unsigned int x);

NOTE:- Always use declaration before function


calling i.e. before main function.
1.Function definition
Syntax:-

Return type function name (parameter data type parameter name)


{
Body of function.
}

Example:-
void delay (unsigned int x)
{
for(i=0;i<x;i++);
}
NOTE:- Always use after function declaration.
No need to declare function if definition is before main.
1.Function calling

Syntax:-

function name (parameter value);

Example:-

delay (2000);

NOTE:- function name should be same.

You might also like