Sei sulla pagina 1di 20

UNIT III

INTRODUCTION TO EMBEDDED
PROGRAMMING CONCEPTS
Introduction to Embedded
Programming in C
Main concern in microcontroller programming is
program memory space
Assembly language program produces hex file that is
much smaller than that produced by C
C has following advantages
Is less tedious & less time consuming as compared to
assembly
Easier to modify & update
Portable to other microcontrollers with little modification
C also provides some library functions
Introduction to Embedded
Programming in C
For embedded C we can use Pentium processor based
compiler
Compiler has to make code of PIC18F877 processor from C
program given to it
Such a compiler is called cross compiler i.e. it produces the
machine code for another processes & not as that used in
the system
Cross compiler is used for PIC18 &PIC series in MPLAB
MPLAB has header file for every microcontroller
Header file contains variable declaration, macro definition
& definitions for special function registers(SFRs)
Header file for PIC18F877 is 16F877A.h
Simple output embedded C program
#include 16F877A.h
void main()
{ output_C(85); }
Output_x() is used to output a data to a port x
Port value is to be passed as a parameter to this
function in the brackets

PIC16 Embedded C programming
Basics
Data types

Data type Size in bits Range or usage
Int1 1 0 to 1
Unsigned int8 8 0 to 255
Signed int8 8 -128 to +127
Unsigned int16 16 0 to 65535
Signed int16 16 -32767 to 32768
Unsigned int32 32 0 to 4294967296
Signed int32 32 -2147483648 to
+2147483647
Exponent Sign Mantissa
8 bit 1 23 bits
Total 32 bit
#include 16F877A.h
Void main()
{
int x;
x=input_A();
ouput_B(x);
}
Control statements
Control statements divided into
Iterative
Conditional
Iterative
For loop
While loop
Do-while loop
Conditional
If-else statements
Switch-case statements
For loop
For(initialization;
condition;
increment/decrement/
update)
{
-
-
statements
-
-
}

#include 16F877A.h
Void main()
{
int x;
for(x=1;x<=10; x++)
{
output_B(x)
delay_ms(100)
}
}
While & Do-While loop
While(condition)
{
-
Statements;
-
}
Entry controlled loop
If condition is false,
statements inside the loop
are never executed
No ; after condition
Do
{
-
Statements;
-
} while( condition);
Exit controlled loop
Statements inside loop
are aleast executed once
even if the condition is
false
; after the condition
While & Do-While loop
#include 16F877A.h
Void main()
{
int x;
X=1;
While(x<=10)
{
Output_B(x);
X++;
}
}

#include 16F877A.h
Void main()
{
int x;
x=1;
do
{
output_B(x);
delay_ms(100);
x++;
}while(x<=10);
}
Example of endless loop
#include 16F877A.h
Void main()
{
int x;
x=0;
While(1)
{
output_B(x);
delay_ms(100);
x++;
}
}



If-else statements
If(condition)
{
-
Statements 1;
-
}
Else
{
-
Statements 2;
-
}
If(condition)
{
-
Statements
-
}
If-else Ladder or if-else if
If(condition)
{
Statements ;
}
Else
{
If(condition)
{
Statements ;
}
Else
{
If(condition)
{
Statements ;
}


Else
{
If(condition)
{
Statements ;
}
|
|
|
Else
{
Statements;
}
} } } }
#include 16F877A.h
Void maain()
{
int1 ip;
while(1)
{
ip= input(PIN_CO);
if(ip==1) output_high(PIN_DO);
}
}
Switch- case selective
statements
Switch(expression/variable)
{
case label1: statements;
break;
case label2: statements;
break;
case labeln: statements;
break;
default: statements;
}
Operators
Unary operators
Minus
Logical Not operator !
Bitwise not operator ~
Increment operator ++
Decrement operator
Binary operators
Arithmetic operator
*, /,%,+,-
Bitwise operator
~,&,|,^,<<,>>

Operators
Logical operators
| |, &&
Relational operators
==, !=, <, >, <=, >=
Assignment operators
=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
Selection operators
[ ], ( ), ,


Functions, pointers, arrays,
strings & enumeration
Functions
Return_type function_name(argument_list)
{ statements; }
Parameters passed by caller function : actual
parameters
Parameters received by called function: formal
parameters
Recursive functions
Function that calls itself
Condition is present in the function itself, if the
condition is true it will call the function & if false, exit
the loop
Functions, pointers, arrays,
strings & enumeration
Arrays
Collection of multiple data of same type
Static memory
Data_type array_name [ array_size];
Multi-dimensional Arrays
Stores data that requires references
Example matrix uses 2 dimensional array
Strings
Array of characters
Terminated with a null character to indicate the
termination

Functions, pointers, arrays,
strings & enumeration
Pointers
Stores the address of another variable
Data_type *ptr_name
Referencing & De-referencing (operations in
pointers)
Address of operator (&)
Also called as referencing operator
Value of operator (*)
Also called as dereferencing operator

Potrebbero piacerti anche