Embedded c

32
•••••••••••••••••••••••••••• ••••• Embedded C

description

A brief overview of Embedded C. Please download it to enjoy watching it with animations.

Transcript of Embedded c

Page 1: Embedded c

••••••••••••••••••••••••••••••••••

Embedded C

Page 2: Embedded c

Ami Prakash- Dayalbagh Educational Institute

WHAT IS EMBEDDED C?

Whenever the conventional ‘C’ language and its extensions are used for programming embedded systems, it is referred to as “Embedded C” programming.

Page 3: Embedded c

C V/S EMBEDDED C ‘C’ is a well structured,

well defined and standardised general purpose programming language.

A platform specific application , known as , compiler is used for the conversion of programs written in C to the target processor specific binary files.

Embedded ‘C’ can be considered as a subset of conventional ‘C’ language.

A software program called ‘Cross compiler’ is used for the conversion of programs written in Embedded ‘C’ to target processor/controller specific instructions(machine language).

Page 4: Embedded c

COMPILER V/S CROSS COMPILER

Compiler is a software tool that converts a source code written in a high level language on top of a particular operating system running on a specific target processor architecture.

Cross compilers are software tools used in cross platform development applications. (In cross platform development , the compiler running on a particular target processor/OS converts the source code to machine code for a target processor whose architecture and instruction set is different from the current development environment OS).

Page 5: Embedded c

Ami Prakash- Dayalbagh Educational Institute

KEYWORDS

These are the reserved names used by the

‘C ’ language. All keywords should be written in

‘lowercase’ letters.Examples- int, char, double, float, void, while,

for, long etc. ANSI ‘C’ supports 32 such keywords.

Page 6: Embedded c

Ami Prakash- Dayalbagh Educational Institute

IDENTIFIERS

Identifiers are user defined names and labels.

Can contain letters of English alphabet(both upper and lower case) and numbers.

Note: The starting character of an identifier should be a letter and the only special character allowed in identifier is underscore(_).

Page 7: Embedded c

Ami Prakash- Dayalbagh Educational Institute

DATATYPES

Datatype represents the type of data held by a variable.

Datatype Size(Bits)

char 8

int 16

float 32

double 64

Note: The storage size may vary for data type depending on the cross compiler in use for embedded applications.

Page 8: Embedded c

Ami Prakash- Dayalbagh Educational Institute

STORAGE CLASS

Keywords related to storage class provide information on the scope i.e. visibility or accessibility and lifetime i.e. existence of a variable.

‘C’ supports four types of storage classes .

Page 9: Embedded c

Ami Prakash- Dayalbagh Educational Institute

Storage Class Meaning Comments

auto Variables declared inside a function.Default storage class is auto.

Scope and accessibility is restricted within the function where the variable is declared.No initialization.

register Variables stored in the CPU register of processor.Reduces access time of variable.

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

static Local variable with lifetime same as that of the program.

Retains the value throughout the program.By default initialises to zero on variable creation.

Page 10: Embedded c

Ami Prakash- Dayalbagh Educational Institute

CONTD..Storage Class Meaning Comments

static Accessibility depends on where the variable is declared.

extern Variables accessible to all functions in a file and all files in a multiple file program.

Can be modified by any function within a file or across multiple files.

Page 11: Embedded c

Ami Prakash- Dayalbagh Educational Institute

ARITHMETIC OPERATIONS

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

Page 12: Embedded c

Ami Prakash- Dayalbagh Educational Institute

LOGICAL OPERATIONS

For decision making and program control transfer.

Operator Operation Comments

&& Logical AND Performs logical AND operation.Output is true(logic 1) if both operands(left to right of && operator) are true.

Page 13: Embedded c

Ami Prakash- Dayalbagh Educational Institute

LOGICAL OPERATIONS(CONTD)..Operator Operation Comments

|| Logical OR Performs logical OR operation.Output is true(logic 1) if either operand is true.

! Logical NOT Performs logical negation.Operand is complemented.

Page 14: Embedded c

Ami Prakash- Dayalbagh Educational Institute

RELATIONAL OPEATIONS

For decision making and program control transfer on the basis of comparison. Operator Operation Comments

< Less than Checks whether the operand on the left side of ‘<’ operator is less than the operand on the right side.If yes return logic one, else return logic zero.

Page 15: Embedded c

Ami Prakash- Dayalbagh Educational Institute

RELATIONAL OPERATIONS(CONTD)Operator Operations Comments

> Greater than Checks whether the operand on the left side of ‘>’ operator is greater than the operand on the right side.If yes return logic one else return logic zero.

<= Less than or equal to

Checks whether the operand on the left side of ‘<=’ operator is less than or equal to the operand on the right side.If yes return logic one, else return logic zero.

Page 16: Embedded c

Ami Prakash- Dayalbagh Educational Institute

RELTIONAL OPERATIONS(CONTD)Operator Operation Comments

== Checks equality Checks whether the operand on the left side of ‘==’ operator is equal to the operand on the right side.If yes return logic one, else return logic zero.

!= Checks non-equality

Checks whether the operand on the left side of ‘!=’ operator is not equal to the operand on the right side.If yes return logic one, else return logic zero.

Page 17: Embedded c

BRANCHING INSTRUCTIONS

CONDITIONAL BRANCHING

Depends on certain conditions and if the conditions are met, the program execution is diverted accordingly.

UNCONDITIONAL BRANCHING

These instructions divert program execution unconditionally.

Page 18: Embedded c

Ami Prakash- Dayalbagh Educational Institute

BRANCHING INSTRUCTIONS(CONTD)Conditional Branching instruction

Explanation

//if statement

if(expression){Statement 1;Statement 2;………………… ;}Statement 3;………………… ;

Evaluates the expression first and if it is true executes the statements given within the { } braces and continue execution of statements following the closing curly brace(}).Skips the execution of the statements within the curly brace{ } if expression is false and continue execution of statements following the closing curly brace (}).

One way branching

Page 19: Embedded c

Ami Prakash- Dayalbagh Educational Institute

BRANCHING INSTRUCTION(CONTD)Conditional Branching Instruction

Explanation

//if else statementif(expression){if_statement1;if_statement2;………………….. ;}else{else_statement1;else_statement2;………………………. ;}statement 3;

Evaluates the expression first and if it is true executes the statements given within the { } braces following if (expression) and continue execution of the statements following the closing curly brace (}) of else block.Executes the statements within the curly brace { } following the else, if the expression is false and continue execution of statements following the closing curly brace(}) of else.

Page 20: Embedded c

Ami Prakash- Dayalbagh Educational Institute

BRANCHING INSTRUCTION(CONTD)Conditional Branching instruction

Explanation

//switch case statement

switch(expression){case value 1: break;case value 2: break;default: break;}

Tests the value of a given expression against a list of case values for a matching condition.The expression and case values should be integers.value1, value2, etc. are integers.If a match found, executes the statement following the case and breaks from the switch.If no match found, executes the default case.

Used for multiple branching.

Page 21: Embedded c

Ami Prakash- Dayalbagh Educational Institute

BRANCHING INSTRUCTIONS(CONTD)Conditional branching instruction

Explanation

//conditional operator//?exp1:exp2(expression) ?exp1:exp2

E.g.if(x>y)A=1;elseA=0;

Can be written using conditional operator as

A=(x>y)1:0

Used for assigning a value depending on the (expression).(expression) is calculated first and if it is greater than 0, evaluates exp1 and returns it as a result of operation else evaluate exp2 and returns it as result.The return value is assigned to some variable.It is a combination of if else with assignment statement.

Used for two way branching.

Page 22: Embedded c

Ami Prakash- Dayalbagh Educational Institute

BRANCHING INSTRUCTIONS(CONTD)Unconditional Branching instruction

Explanation

goto Goto is used as an unconditional branching instruction.goto transfers the program control indicated by a label following the goto statement.the label inicated by goto statement can be anywhere in the program either before or after the goto label instruction.

goto is generally used to come out of deeply nested loops in abnormal conditions or errors.

Page 23: Embedded c

Ami Prakash- Dayalbagh Educational Institute

GOTO LABEL EXAMPLE#include<stdio.h>void main() { int n=0; loop: ; printf("\n%d", n);

  n++;  if (n<10) {  goto loop;  }  getch();  return 0;

}

Page 24: Embedded c

SAMPLE OUTPUT

Page 25: Embedded c

Ami Prakash- Dayalbagh Educational Institute

LOOPING INSTRUCTIONS

For executing a particular block of code repeatedly till a condition is met or wait till an event is fired.

Used to check the status of certain I/O ports, registers, etc. and also for producing delays.

Certain devices allow write/read operations to and from some registers of the device only when the device is ready and the device ready is normally indicated by a status register or by setting/clearing certain bits of status registers.Hence the program should keep on reading the status register till the device ready indication comes.

Page 26: Embedded c

Ami Prakash- Dayalbagh Educational Institute

LOOPING INSTRUCTION(CONTD)Looping instruction explanation

//while statement

while (expression){

body of while loop }

Entry controlled loop statement.The expression is evaluated first and if it is true the body of the loop is entered and executed.Execution of ‘body of while loop’ is repeated till the expression becomes false.

Page 27: Embedded c

Ami Prakash- Dayalbagh Educational Institute

SAMPLE CODE//using while loop

char *status_reg=char(*)0x3000 ; /*Declares memory mapped register*/

while(*status_reg!=0x01); /*Wait till status_reg=0x01 i.e. device ready state*/

Page 28: Embedded c

Ami Prakash- Dayalbagh Educational Institute

LOOPING INSTRUCTIONS(CONTD)Looping instruction explanation

//do while loop

do{

body of do loop

}while(expression)

The ‘body of the loop’ is executed at least once.At the end of each execution of the ‘body of the loop’, the while condition (expression) is evaluated and if it is true the loop is repeated, else loop is terminated.

Page 29: Embedded c

Ami Prakash- Dayalbagh Educational Institute

SAMPLE CODE//using do while loop

char *status_reg=(char*)0x3000;do{

// body of do loop

}while(*status_reg!=0x01); /* loop till status_reg=0x01 */

Page 30: Embedded c

Ami Prakash- Dayalbagh Educational Institute

LOOPING INSTRUCTIONS(CONTD)Looping instruction explanation

//for loop

for(initialisation;test for condition;update variable){

body of for loop

}

Entry controlled loop.Enters and executes ‘the body of loop’ only if the test for the condition is true. for loop contains a loop control variable which may be initialised within the initialisation part of the loop.Multiple variables can be initialised with ‘,’ operator.

Page 31: Embedded c

Ami Prakash- Dayalbagh Educational Institute

SAMPLE CODE// using for loop

char *status_reg=(char*) 0x3000;for(;(*status_reg!=0x01););

Page 32: Embedded c

Ami Prakash- Dayalbagh Educational Institute

REFERENCES Introduction to Embedded Systems by Shibu KV