Top Banner
INTRODUCTION TO EMBEDDED PROGRAMMING CONCEPTS
25

Embedded c programming

Jan 19, 2017

Download

Engineering

PriyaDYP
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Embedded c programming

INTRODUCTION TO EMBEDDED

PROGRAMMING CONCEPTS

Page 2: Embedded c programming

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.

Page 3: Embedded c programming

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.

Page 4: Embedded c programming

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.

Page 5: Embedded c programming

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.

Page 6: Embedded c programming

GIVING OUTPUT TO A PORT#include “16F877A.h”void main(){ output_C(85)}

Function output_x() is used to output data on port ‘x’

Page 7: Embedded c programming

EMBEDDED C PROGRAMMING BASICS Data types: - int

- signed int- unsigned int- char- signed char- unsigned char

Page 8: Embedded c programming

PROGRAMMING#include “16F877A.h”void main(){ int x; x=input_A(); output_B(x);}

Page 9: Embedded c programming

FOR LOOPfor(initializations;condition;inc/dec/

update){ - - - statements; - -}

Page 10: Embedded c programming

EXAMPLE#include “16F877A.h”void main(){ int x; for(x=1;x<=10;x++) {

output_B(x); delay_ms(100); }}

Page 11: Embedded c programming

WHILE LOOPwhile(condition){ - - statements; - -}

Page 12: Embedded c programming

EXAMPLE#include “16F877A.h”void main(){ int x; x=1; while(x<=10) { output_B(x); x++; }}

Page 13: Embedded c programming

DO WHILEdo{ - - statements; - -}while(condition);

Page 14: Embedded c programming

EXAMPLE#include “16F877A.h”void main(){ int x; x=1; do { output_B(x); delay_ms(100); x++; }while(x<=10);}

Page 15: Embedded c programming

IF ELSE SELECTIVE STATEMENTif(condition){ - - statements;}else{ - - statements;}

Page 16: Embedded c programming

SIMPLE IF STATEMENTif(condition){ - - statements; - -}

Page 17: Embedded c programming

IF ELSE LADDERif(condition){ statements;}Else{ if(condition) { statements; } else { if(condition) { statements; } }}

Page 18: Embedded c programming

SWITCH CASEswitch(expreesion/variable){ case label1: statements; break; case label2: statements; break; case label3: statements; break; default: statements;}

Page 19: Embedded c programming

BREAK STATEMENTSwhile(condition

){ - - break; - -}

for(init;cond;inc){ - - break; - -}

Page 20: Embedded c programming

CONTINUE STATEMENTS

while(condition)

{ - - continue; - -}

for(init;cond;inc){ - - continue; - -}

Page 21: Embedded c programming

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

Page 22: Embedded c programming

OPERATORSOperator Representation

Unary minus _

Logical not !

Bitwise not ~

Increment operator ++

Decrement operator --

Arithmetic operators +,-,*,/,%

Shift operators <<,>>

Assignment operators =,+=,-=

Page 23: Embedded c programming

VARIABLES Local variables - limited to the function in which it is

defined Global variables - it is accessible anywhere in the

program.

Page 24: Embedded c programming

OTHER PROGRAMMING CONCEPTS Functions/Recursive functions Arrays Multidimensional arrays Pointers Strings

Page 25: Embedded c programming

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