embedded system.ppt

20

Upload: venkat7186

Post on 28-Dec-2015

16 views

Category:

Documents


0 download

DESCRIPTION

Embedded Systems working principle

TRANSCRIPT

Page 1: Embedded System.ppt
Page 2: Embedded System.ppt

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

Page 3: Embedded System.ppt

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

Page 4: Embedded System.ppt

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

Page 5: Embedded System.ppt

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

Page 6: Embedded System.ppt

#include “16F877A.h”Void main(){

int x;x=input_A();ouput_B(x);

}

Page 7: Embedded System.ppt

Control statements

Control statements divided into Iterative Conditional

Iterative For loop While loop Do-while loop

Conditional If-else statements Switch-case statements

Page 8: Embedded System.ppt

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)}

}

Page 9: Embedded System.ppt

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

Page 10: Embedded System.ppt

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);}

Page 11: Embedded System.ppt

Example of endless loop#include “16F877A.h”Void main(){

int x;x=0;

While(1){

output_B(x);delay_ms(100);x++;

}}

Page 12: Embedded System.ppt

If-else statementsIf(condition){-Statements 1;-}Else{-Statements 2;-}

If(condition){-Statements-}

Page 13: Embedded System.ppt

If-else Ladder or if-else ifIf(condition)

{Statements ;}Else

{If(condition)

{Statements ;}

Else{If(condition)

{Statements

;}

Else{If(condition)

{Statements ;}

|||Else

{Statements;

}} } } }

Page 14: Embedded System.ppt

#include “16F877A.h”Void maain(){

int1 ip;while(1){

ip= input(PIN_CO);if(ip==1) output_high(PIN_DO);

}}

Page 15: Embedded System.ppt

Switch- case selective statementsSwitch(expression/variable){

case label1: statements;break;case label2: statements;break;case labeln: statements;break;default: statements;

}

Page 16: Embedded System.ppt

Operators Unary operators

Minus – Logical Not operator ! Bitwise not operator ~ Increment operator ++ Decrement operator –

Binary operators Arithmetic operator

*, /,%,+,- Bitwise operator

~,&,|,^,<<,>>

Page 17: Embedded System.ppt

Operators

Logical operators | |, &&

Relational operators ==, !=, <, >, <=, >=

Assignment operators =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=,

>>= Selection operators

[ ], ( ), ,

Page 18: Embedded System.ppt

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

Page 19: Embedded System.ppt

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

Page 20: Embedded System.ppt

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 de—referencing operator