Вы находитесь на странице: 1из 21

PROGRAMMING IN

EMBEDDED C
By
Leena Aroza
25-NOV-10

1
TOPICS COVERED
 C v/s Embedded C
 Compile v/s Cross-Compiler
 Programming in C

2
C
C’ is a well structured, well defined and standardized general purpose

programming language .
‘C’ offers a combination of the features of high level language and assembly and

helps in hardware access programming (system level programming) as well as
business package developments (Application developments like pay roll systems,
banking applications etc)
ANSI standard language.
Platform based Compiler.

EMBEDED C
Subset of conventional ‘C’ language
 ‘C’ instructions and with few target processor specific functions/
Instructions.
‘Cross-compiler’ is used for the conversion of programs written in Embedded C
to target processor/controller specific instructions.
eg:keil C51
The target processor/controller specific functions/instructions depends upon the

processor/controller
3
C

Keywords & Storage class


identifiers Data types

Arithmetic Logical Relational


operations operations operations

Branching Loop Arrays &


instructions instructions pointers

4
C Constants

Primary Constants Secondary Constants

Array
Integer Constants Pointer
Real Constants Structure
Character Constants Union
Enum

5
KEYWORDS
Keywords

auto double int struct

break else long switch

case enum register typedef

char extern return union

const float short unsigned

continue for signed void

default goto sizeof volatile

do if static while

6
DATA TYPES
Data Type Size (Bits) Range Comments

char 8 -128 to +127 Signed Character


signed char 8 -128 to +127 Signed Character
unsigned char 8 0 to +255 Unsigned Character
short int 8 -128 to +127 Signed short integer
signed short int 8 -128 to +127 Signed short integer
unsigned short int 8 0 to +255 Unsigned short integer
int 16 -32,768 to +32,767 Signed integer
signed int 16 -32,768 to +32,767 Signed integer
unsigned int 16 0 to +65,535 Unsigned integer
long int 32 -2147,483,648 to Signed long integer
+2,147,483,647
signed long int 32 -2147,483,648 to Signed long integer
+2,147,483,647
unsigned long int 32 0 to +4,294,967,295 Unsigned long integer
float 32 3.4E-38 to 3.4E+38 Signed floating point
double 64 1.7E-308 to Signed floating point
1.7E+308 (Double precision)
long double 80 3.4E-4932 to Signed floating point
3.4E+4932 (Long Double
precision)
7
STORAGE CLASS
Storage Class Meaning Comments
Variables declared inside a Scope and accessibility is restricted within
auto function. Default storage the function where the variable is declared.
class is auto No initialization. Contains junk values at the
time of creation

Variables stored in the CPU Same as auto in scope and access. The
register register of processor. decision on whether a variable needs to be
Reduces access time of kept in CPU register of the processor
variable depends on the compiler

Retains the value throughout the program.


static Local variable with life time By default initializes to zero on variable
same as that of the creation. Accessibility depends on where the
program variable is declared

Variables accessible to all Can be modified by any function within a


extern functions in a file and all file or across multiple files (variable needs
files in a multiple file to be exported by one file and imported by
program other files using the same)

8
Arithmetic Operator

Arithmetic Operator Operation Comments

+ Addition Adds variables or numbers

- Subtraction Subtracts variables or numbers

* Multiplication Multiplies variables or numbers

/ Division Divides variables or numbers

% Remainder Finds the remainder of a division

9
LOGICAL OPERATORS
Operator Operation Comments
Performs logical AND operation. Output is true
&& Logical AND (logic 1) if both operands (left to and right to of
&& operator) are true

Performs logical OR operation. Output is true


|| Logical OR (logic 1) if either operand (operands to left or
right of || operator) is true

! Logical NOT Performs logical Negation. Operand is


complemented (logic 0 becomes 1 and vice
versa)

10
Branching Instructions
//if statement //if else statement
if (expression) if else statement
{ if (expression)
statement1; {
statement2; if_statement1;
………….; if_statement2;
} …………….;
statement 3; }
…………..; else
{
else_statement1;
else_statement2;
……………….;
}
statement 3;

11
Conditional branching Instruction

//switch case statement Conditional operator


switch (expression) // ?exp1 : exp2
{ (expression) ?exp1: exp2
case value1: E.g.
break; if (x>y)
case value2: a=1;
break; else
default: a=0;
break; can be written using conditional
} operator as
a=(x>y)? 1:0

//unconditional branching
Goto label

12
Looping Instruction
1) while LOOP

//while statement //do while loop

while (expression) do
{ {
body of while loop body of do loop
} }
while (expression);

13
Looping Instruction
2) For loop

//for loop
//exiting from loop

for(initialization;condition;increment)
break;
{
goto label
body of for loop
}

14
ARRAYS
 Array is a collection of related elements (data types)
 Array declaration

data _type arr_name [num_of_arr_elements];

Eg: char arr[4];

0x20 0x30 0x40 0x50

0x9001 0x9002 0x9003 0x9004


arr[0] arr[1] arr[2] arr[3]

15
Array Initializations
//Initialization of array at //Selective initialization
the time of declaration
unsigned char arr[5];
unsigned char arr[5] = {5,
10, 20, 3, 2}; arr[0] = 5;
unsigned char arr[ ] = {5, arr[1] = 10;
arr[2] = 20;
10, 20, 3, 2};
arr[3] = 3;
arr[4] = 2;

16
Pointers
 Pointer is a memory pointing based technique for variable access and
modification.
 Egg:
char input; =34 //Declaring input as character variable
char *p; //Declaring a character pointer p (* denotes p is a pointer)
p = &input

Variable name Memory Content


address

0x34
input 0x7000

17
Equivalence of pointers and arrays
 E.g. for the character array char arr[5], arr[ ] is equivalent to a
character pointer pointing to the first element of array arr
 The array features like accessing and modifying members of an array
can be achieved using a pointer and pointer increment/decrement
operators
 Arrays and pointer declarations are interchangeable when they are
used as parameters to functions

18
REFERECE
 Shibu K.V-Introduction to Embedded System
 Yashavant Kanetkar-Let us C
 Balaguru Swamy-programming in C

19
?

20
THAK YOU

21

Вам также может понравиться