Date post: | 19-Jan-2017 |
Category: |
Engineering |
Upload: | priyadyp |
View: | 132 times |
Download: | 0 times |
INTRODUCTION TO EMBEDDED
PROGRAMMING CONCEPTS
INTRODUCTION 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.
ADVANTAGES OF C PROGRAMMING 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.
CROSS COMPILER 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 HEADER FILES 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.
GIVING OUTPUT TO A PORT#include “16F877A.h”void main(){ output_C(85)}
Function output_x() is used to output data on port ‘x’
EMBEDDED C PROGRAMMING BASICS Data types: - int
- signed int- unsigned int- char- signed char- unsigned char
PROGRAMMING#include “16F877A.h”void main(){ int x; x=input_A(); output_B(x);}
FOR LOOPfor(initializations;condition;inc/dec/
update){ - - - statements; - -}
EXAMPLE#include “16F877A.h”void main(){ int x; for(x=1;x<=10;x++) {
output_B(x); delay_ms(100); }}
WHILE LOOPwhile(condition){ - - statements; - -}
EXAMPLE#include “16F877A.h”void main(){ int x; x=1; while(x<=10) { output_B(x); x++; }}
DO WHILEdo{ - - statements; - -}while(condition);
EXAMPLE#include “16F877A.h”void main(){ int x; x=1; do { output_B(x); delay_ms(100); x++; }while(x<=10);}
IF ELSE SELECTIVE STATEMENTif(condition){ - - statements;}else{ - - statements;}
SIMPLE IF STATEMENTif(condition){ - - statements; - -}
IF ELSE LADDERif(condition){ statements;}Else{ if(condition) { statements; } else { if(condition) { statements; } }}
SWITCH CASEswitch(expreesion/variable){ case label1: statements; break; case label2: statements; break; case label3: statements; break; default: statements;}
BREAK STATEMENTSwhile(condition
){ - - break; - -}
for(init;cond;inc){ - - break; - -}
CONTINUE STATEMENTS
while(condition)
{ - - continue; - -}
for(init;cond;inc){ - - continue; - -}
FUNCTIONS IN MPLAB COMPILERLibrary 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
OPERATORSOperator Representation
Unary minus _
Logical not !
Bitwise not ~
Increment operator ++
Decrement operator --
Arithmetic operators +,-,*,/,%
Shift operators <<,>>
Assignment operators =,+=,-=
VARIABLES Local variables - limited to the function in which it is
defined Global variables - it is accessible anywhere in the
program.
OTHER PROGRAMMING CONCEPTS Functions/Recursive functions Arrays Multidimensional arrays Pointers Strings
REFERENCING/DEREFERENCING 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