8/8/2019 Programming in Embedded c
1/21
BY
LEENA AROZA
25-NOV-10
1
8/8/2019 Programming in Embedded c
2/21
C v/s Embedded C
Compile v/s Cross-Compiler
Programming in C
2
8/8/2019 Programming in Embedded c
3/21
C is a well structured, well defined and standardized general purposeprogramming language .
C offers a combination of the features of high level language andassembly and helps in hardware access programming (system levelprogramming) as well as business package developments (Applicationdevelopments 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 inEmbedded C to target processor/controller specific instructions.eg:keil C51
The target processor/controller specific functions/instructions dependsupon the processor/controller
3
8/8/2019 Programming in Embedded c
4/21
C
C
Keywords &identifiers
Keywords &identifiers
Arithmeticoperations
Arithmeticoperations
Branchinginstructions
Branchinginstructions
Loopinstructions
Loopinstructions
Logicaloperations
Logicaloperations
Data types
Data types
Arrays &pointers
Arrays &pointers
Relationaloperations
Relationaloperations
Storage class
Storage class
4
8/8/2019 Programming in Embedded c
5/21
5
Primary Constants
Primary Constants
Array
PointerStructure
UnionEnum
Array
PointerStructure
UnionEnum
Secondary Constants
Secondary Constants
Integer ConstantsReal ConstantsCharacter Constants
Integer ConstantsReal ConstantsCharacter Constants
C
CONSTANTS
C
CONSTANTS
8/8/2019 Programming in Embedded c
6/21
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
8/8/2019 Programming in Embedded c
7/21
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
+2,147,483,647
Signed long integer
signed long int 32 -2147,483,648 to
+2,147,483,647
Signed long integer
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
1.7E+308
Signed floating point
(Double precision)
long double 80 3.4E-4932 to
3.4E+4932
Signed floating point
(Long Double
precision) 7
8/8/2019 Programming in Embedded c
8/21
Storage Class Meaning Comments
autoVariables declared inside afunction. Default storageclass is auto
Scope and accessibility is restricted withinthe function where the variable is declared.No initialization. Contains junk values at thetime of creation
registerVariables stored in the CPUregister of processor.
Reduces access time ofvariable
Same as auto in scope and access. Thedecision on whether a variable needs to be
kept in CPU register of the processordepends on the compiler
static Local variable with life timesame as that of theprogram
Retains the value throughout the program.By default initializes to zero on variablecreation. Accessibility depends on where thevariable is declared
externVariables accessible to allfunctions in a file and allfiles in a multiple fileprogram
Can be modified by any function within afile or across multiple files (variable needsto be exported by one file and imported byother files using the same)
8
8/8/2019 Programming in Embedded c
9/21
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
8/8/2019 Programming in Embedded c
10/21
LOGICAL OPERATORSOperator Operation Comments
&& Logical ANDPerforms logical AND operation. Output is true(logic 1) if both operands (left to and right to of
&& operator) are true
|| Logical ORPerforms logical OR operation. Output is true(logic 1) if either operand (operands to left or
right of || operator) is true
! Logical NOT Performs logical Negation. Operand iscomplemented (logic 0 becomes 1 and vice
versa)
10
8/8/2019 Programming in Embedded c
11/21
//if statementif (expression){statement1;statement2;
.;}statement 3;..;
if else statementif (expression){
if_statement1;if_statement2;.;}else{else_statement1;else_statement2;.;}statement 3;
//if else statement
BRANCHING INSTRUCTIONS
11
8/8/2019 Programming in Embedded c
12/21
//switch case statementswitch (expression){
case value1:break;case value2:break;default:break;
}
Conditional operator// ?exp1 : exp2(expression) ?exp1: exp2
E.g.if (x>y)a=1;elsea=0;
can be written using conditionaloperator asa=(x>y)? 1:0
CONDITIONAL BRANCHING
INSTRUCTION
//unconditional branchingGoto label
12
8/8/2019 Programming in Embedded c
13/21
//while statement
while (expression){body of while loop
}
//do while loop
do{body of do loop}while (expression);
LOOPING INSTRUCTION
1) WHILE LOOP
13
8/8/2019 Programming in Embedded c
14/21
//exiting from loop
break;goto label
//for loop
for(initialization;condition;increment){body of for loop}
LOOPING INSTRUCTION
2) FOR LOOP
14
8/8/2019 Programming in Embedded c
15/21
Array is a collection of related elements (data types)Array declaration
data _type arr_name [num_of_arr_elements];
Eg: char arr[4];
0x200x20 0x300x30 0x400x40 0x500x50
0x9001 0x9002 0x9003 0x9004
arr[0] arr[1] arr[2] arr[3]
15
8/8/2019 Programming in Embedded c
16/21
ARRAY INITIALIZATIONS
//Initialization of array atthe time of declaration
unsigned char arr[5] = {5,10, 20, 3, 2};unsigned char arr[ ] = {5,10, 20, 3, 2};
//Selective initialization
unsigned char arr[5];
arr[0] = 5;arr[1] = 10;arr[2] = 20;arr[3] = 3;arr[4] = 2;
16
8/8/2019 Programming in Embedded c
17/21
Pointer is a memory pointing based technique for variable access andmodification.
Egg:
char input; =34 //Declaring input as character variable
char *p; //Declaring a character pointer p (* denotes p is a pointer)p = &input
MemoryaddressMemoryaddress
ContentContentVariable nameVariable name
inputinput0x340x34
0x70000x7000
17
8/8/2019 Programming in Embedded c
18/21
E.g. for the character array char arr[5], arr[ ] is equivalent to acharacter 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/decrementoperators
Arrays and pointer declarations are interchangeable when they areused as parameters to functions
18
8/8/2019 Programming in Embedded c
19/21
Shibu K.V-Introduction to Embedded System
Yashavant Kanetkar-Let us C
Balaguru Swamy-programming in C
19
8/8/2019 Programming in Embedded c
20/21
20
8/8/2019 Programming in Embedded c
21/21
21