37
Training Of Trainers (TOT) program on Embedded Software Engineer Job role www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] Developing Software Module C Programming Language 1

Develop Embedded Software Module-Session 1

Embed Size (px)

Citation preview

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Developing Software Module

C Programming Language

1

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

By the end of training session,you will be able to

1. Develop Software modules to meet the requirements specification

2. Develop a software design as per requirement specification

3. Use the C language as programming tool

4. Understand code generation tool and testing/debugging methods

2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

AgendaSession 1

History of C Fundamentals of C

Data Types Variables, Constants and Arrays Keywords Decision Control (if-else)

Session 2 Loops Functions (Overview) Arrays

Session 3 Pointers & String

3www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

History of C

C is a high level language It was developed by Dennis Ritchie at Bell Laboratory, USA (Now AT&T) in 1972. It was developed to be used in UNIX Operating System. Now C is such a powerful language that LINUX Kernel is written in C. Almost all Embedded Software has been developed in C language (Known as Embedded C).

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Development Tools Data FlowC Compiler

Compiler

C Source File C Header FilePreprocessor

.exeExecutable File

.h.c

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Example

A Simple C Program

#include <stdio.h>

#define PI 3.14159

int main(void){

float radius, area;

//Calculate area of circleradius = 12.0;area = PI * radius * radius;printf("Area = %f", area);

}

Header File

Function

Variable Declarations

Constant Declaration (Text Substitution Macro)

Comment

Preprocessor Directives

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Definition

Comments

Two kinds of comments may be used: Block Comment/* This is a comment */

Single Line Comment // This is also a comment

Comments are used to document a program's functionality and to explain what a particular block or line of code does. Comments are ignored by the compiler, so you can type anything you want into them.

7www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Example

Variables and Data Types

Variable DeclarationsData

Types

Variables in use

#include <stdio.h>

#define PI 3.14159

int main(void){

float radius, area;

//Calculate area of circleradius = 12.0;area = PI * radius * radius;printf("Area = %f", area);

}

8www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Variables

A variable may be thought of as a container that can hold data used in a program

Definition

A variable is a name that represents one or more memory locations used to hold program data.

int myVariable;

myVariable = 5;myVariable

5

9www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Example of Identifiers in a Program

Identifiers

Names given to program elements: Variables, Functions, Arrays, Other elements

#include <stdio.h>

#define PI 3.14159

int main(void){

float radius, area;

//Calculate area of circleradius = 12.0;area = PI * radius * radius;printf("Area = %f", area);

}

10www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Identifiers

Valid characters in identifiers:

Case sensitive! Only first 31 characters significant*

I d e n t i f i e rFirst Character

‘_’ (underscore) ‘A’ to ‘Z’ ‘a’ to ‘z’

Remaining Characters‘_’ (underscore)

‘A’ to ‘Z’ ‘a’ to ‘z’ ‘0’ to ‘9’

11www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

ANSI C Keywordsauto break case char const continue default do

double else enum extern float for goto if

int long register return short signed sizeof static

struct switch typedef union unsigned void volatile while

Some compiler implementations may define additional keywords

12www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Data Types Fundamental Types

Type Description Bits

charintfloatdouble

single characterintegersingle precision floating point numberdouble precision floating point number

168

3264

13www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Syntax

Variables How to Declare a Variable

Variables may be declared in a few ways:

type identifier;

type identifier = InitialValue;

type identifier1, identifier2, identifier3;

type identifier1 = Value1, identifier2 = Value2;

One declaration on a line

One declaration on a line with an initial value

Multiple declarations of the same type on a line

Multiple declarations of the same type on a line with initial values

14www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Literal Constants

Basic types of literals: Integer Floating Point Character

Integer and Floating Point are numeric type constants: Commas and spaces are not allowed Value cannot exceed type bounds May be preceded by a minus sign

15www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Integer Literals Decimal (Base 10)

Cannot start with 0 (except for 0 itself) Cannot include a decimal point Valid Decimal Integers:

Invalid Decimal Integers:

0 5 127 -1021 65535

32,767 25.0 1 024 0552

16www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Integer Literals Hexadecimal (Base 16)

Must begin with 0x or 0X (that’s zero-x) May include digits 0-9 and A-F / a-f Valid Hexadecimal Integers:

Invalid Hexadecimal Integers:

0x 0x1 0x0A2B 0xBEEF

0x5.3 0EA12 0xEG 53h

17www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Integer Literals Octal (Base 8)

Must begin with 0 (zero) May include digits 0-7 Valid Octal Integers:

Invalid Octal Integers:

0 01 012 073125

05.3 0o12 080 53o

While Octal is still part of the ANSI specification, almost no one uses it anymore.

18www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Integer Literals Binary (Base 2)

Must begin with 0b or 0B (that’s zero-b) May include digits 0 and 1 Valid Binary Integers:

Invalid Binary Integers:

0b 0b1 0b0101001100001111

0b1.0 01100 0b12 10b

ANSI C does not specify a format for binary integer literals. However, this notation is supported by most compilers.

19www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Symbolic Constants Text Substitution Labels Using #define

Defines a text substitution labelSyntax

#define label text

Example

#define PI 3.14159 #define mol 6.02E23 #define MCU "PIC24FJ128GA010" #define COEF 2 * PI

Each instance of label will be replaced with text by the preprocessor unless label is inside a string No memory is used in the microcontroller

20www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Negative- (unary)

Subtraction-

Positive+ (unary)

Modulo%

Addition+

Operators Arithmetic

NOTE - An int divided by an int returns an int: 10/3 = 3 Use modulo to get the remainder: 10%3 = 1

Multiplication*

Division/

Operator ResultOperation Example

-x

x - y

+x

x % y

x + y

x * y

x / y

Negative value of x

Difference of x and yValue of x

Remainder of x divided by ySum of x and y

Product of x and yQuotient of x and y

21www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Example: Integer Divide Example: Floating Point Divide

Operators Division Operator

If both operands are an integer type, the result will be an integer type (int, char) If one or both of the operands is a floating point type, the result will be a floating point type (float, double)

int a = 10; int b = 4; float c; c = a / b;

int a = 10; float b = 4.0f; float c; c = a / b;

c = 2.000000 Because: int / int ! int

c = 2.500000 Because: float / int ! float

✗ ✓22www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Operators Arithmetic: Increment and Decrement

x = 5; y = (x++) + 5; // y = 10 // x = 6

x = 5; y = (++x) + 5; // y = 11 // x = 6

Operator ResultOperation Example

Decrement--

Increment++

x--

--x

x++

++x

Use x then decrement x by 1Decrement x by 1, then use x

Use x then increment x by 1Increment x by 1, then use x

Postfix Example Prefix Example

23www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Operators- Relational

In conditional expressions, any non-zero value is interpreted as TRUE. A value of 0 is always FALSE.

Operator Result (FALSE = 0, TRUE ≠ 0)Operation Example

Equal to==

Not equal to!=

Greater than>

Greater than or equal to>=

Less than<

Less than or equal to<=

x == y

x != y

x > y

x >= y

x < y

x <= y

1 if x equal to y, else 0

1 if x not equal to y, else 0

1 if x greater than y, else 0

1 if x greater than or equal to y, else 0

1 if x less than y, else 01 if x less than or equal to y, else 0

24www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Operators- Logical

Logical NOT!

Logical OR||

Logical AND&&

!x

x || y

x && y

1 if x = 0, else 0

0 if both x = 0 and y = 0, else 1

1 if both x ≠ 0 and y ≠ 0, else 0

Operator Result (FALSE = 0, TRUE ≠ 0)Operation Example

In conditional expressions, any non-zero value is interpreted as TRUE. A value of 0 is always FALSE.

25www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Operators- BitwiseOperator Result (for each bit position)Operation Example

Bitwise XOR^

Bitwise NOT (One's Complement)

~

Bitwise AND&

Bitwise OR|

x ^ y

~x

x & y

x | y

1, if 1 in x or y but not both 0, if 0 or 1 in both x and y

1, if 0 in x 0, if 1 in x

1, if 1 in both x and y 0, if 0 in x or y or both1, if 1 in x or y or both 0, if 0 in both x and y

The operation is carried out on each bit of the first operand with each corresponding bit of the second operand

26www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Operators- Shift

Shift Left Example: x = 5; // x = 0b00000101 = 5 y = x << 2; // y = 0b00010100 = 20

In both shift left and shift right, the bits that are shifted out are lost For shift left, 0's are shifted in (Zero Fill)

Operator ResultOperation Example

Shift Left<<

Shift Right>>

x << y

x >> y

Shift x by y bits to the left

Shift x by y bits to the right

27www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Making Decisions

28

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Note

Syntax

if Statement

expression is evaluated for boolean TRUE (≠0) or FALSE (=0) If TRUE, then statement is executed

if (expression) statement

if (expression) { statement1 statement2 }

Whenever you see statement in a syntax guide, it may be replaced by a compound (block) statement.

Remember: spaces and new lines are not significant.

29www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Syntax

if Statement- Flow Diagram

expression statement

START

END

if (expression) statement

TRUE

FALSEexpression = 0

expression ≠ 0

30www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Example{ int x = 5;

if (x) { printf("x = %d\n", x); } while (1); }

if Statement

If x is TRUE (non-zero)…

…then print the value of x.

What will print if x = 5? … if x = 0? …if x = -82? …if x = 65536?

31www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Syntaxif-else if Statement

expression1 is evaluated for boolean TRUE (≠0) or FALSE (=0) If TRUE, then statement1 is executed

If FALSE, then expression2 is evaluated

If TRUE, then statement2 is executed

If FALSE, then statement3 is executed

if (expression1) statement1 else if (expression2) statement2 else statement3

32www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

expression 1

START

END

statement2

statement 3

expression 2

statement1

if-else if Statement- Flow Diagram

TRUE

FALSE

FALSE

TRUE

Syntax

if (expression1) statement1 else if (expression2) statement2 else statement3

33www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Example

if-else if Statement

if ((freq > 144) && (freq < 148))

printf("You're on the 2 meter band\n");

else if ((freq > 222) && (freq < 225))

printf("You're on the 1.25 meter band\n");

else if ((freq > 420) && (freq < 450))

printf("You're on the 70 centimeter band\n");

else

printf("You're somewhere else\n");

34www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

HANDS-ON PRACTICE

35www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Conclusion

In this Session, We learn Fundamentals of C

Data Types Variables Operators Macro

Decision making statements if statement If-else if statements

36www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Thank you!

37www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]