 Main concern is program memory space.
 Assembly language programming produces
hex file that is much smaller than that
produced by programming in C.
 Assembly language is tedious and time
consuming.
 Programming in C is less tedious and less
time consuming as compared to assembly
language
 Easier to modify and update
 Portable to microcontrollers with little
modifications
 C also provides some library functions.
 We write a program in C, we need a compiler
to convert it into machine language.
 here we use Pentium processor.
 But our compiler has to make code for
PIC18F877 processor for C program.
 Such a compiler is called as cross compiler.
i.e produces code for another processor and
not the system in use.
 MPLAB is such a compiler used for
microcontrollers of PIC series.
 MPLAB has header files for every
microcontroller.
 Header files contain variable declarations,
macro definitions, special function registers
etc.
 “16F877A.h” is the header file for PIC
18F877.
 This file has many inbuilt C functions to
perform various tasks like giving output to
port and read data from port.
#include “16F877A.h”
void main()
{
output_C(85)
}
 Function output_x() is used to output data on
port ‘x’
 Data types:
- int
- signed int
- unsigned int
- char
- signed char
- unsigned char
#include “16F877A.h”
void main()
{
int x;
x=input_A();
output_B(x);
}
for(initializations;condition;inc/dec/update)
{
-
-
-
statements;
-
-
}
#include “16F877A.h”
void main()
{
int x;
for(x=1;x<=10;x++)
{
output_B(x);
delay_ms(100);
}
}
while(condition)
{
-
-
statements;
-
-
}
#include “16F877A.h”
void main()
{
int x;
x=1;
while(x<=10)
{
output_B(x);
x++;
}
}
do
{
-
-
statements;
-
-
}while(condition);
#include “16F877A.h”
void main()
{
int x;
x=1;
do
{
output_B(x);
delay_ms(100);
x++;
}while(x<=10);
}
if(condition)
{
-
-
statements;
}
else
{
-
-
statements;
}
if(condition)
{
-
-
statements;
-
-
}
if(condition)
{
statements;
}
Else
{
if(condition)
{
statements;
}
else
{
if(condition)
{
statements;
}
}
}
switch(expreesion/variable)
{
case label1: statements;
break;
case label2: statements;
break;
case label3: statements;
break;
default: statements;
}
while(condition)
{
-
-
break;
-
-
}
for(init;cond;inc)
{
-
-
break;
-
-
}
while(condition)
{
-
-
continue;
-
-
}
for(init;cond;inc)
{
-
-
continue;
-
-
}
Library functions Operation done
delay_us(x) Generates delay of x microseconds
delay_ms(x) Generates delay of x milliseconds
output_p(x) Outputs parameter ‘x’ on output port ‘p’
Five ports available are: A,B,C,D,E
input_x() Returns the value on input port x
Operator Representation
Unary minus _
Logical not !
Bitwise not ~
Increment operator ++
Decrement operator --
Arithmetic operators +,-,*,/,%
Shift operators <<,>>
Assignment operators =,+=,-=
 Local variables
- limited to the function in which it is
defined
 Global variables
- it is accessible anywhere in the program.
 Functions/Recursive functions
 Arrays
 Multidimensional arrays
 Pointers
 Strings
 Address of operator (“&”)
p=&x;
address of variable x is copied in pointer p
 Value of operator (“*”)
y=*p;
value of variable pointed by pointer p is
stored in variable y

Embedded c programming