Embedded Systems
Workshop
C Programming
ENG. Zeyad Hesham
Embedded Systems
❑ Agenda:
▪ Introduction to Embedded Systems.
▪ C Programming.
▪ Micro-controller Interfacing (Part I).
▪ C for Embedded Applications.
▪ Micro-controller Interfacing (Part II).
▪ Introduction to Real Time Operating Systems.
About C programming.
• In 1970 at “AT & T’ Bell Laboratory” Dennis Ritchie write C.
• The main purpose was designing UNIX OS.
• C is Function Oriented Language.
C programming Features.
• Few keywords.
• Provide user defined data types and compound data types.
• Standard Library.
• Pointers
• Macros and Preprocessors.
C Applications
• Operating systems
• Microcontrollers programming and Embedded applications
Notes
• C program varies from 3 lines : millions.
• C Project contain .c and .h files.
• C Project must contain 1 function at least which is main function.
• Every Statement must be terminated with semicolon(;).
What is a Program?
• Computers only do what they're told.
To perform any meaningful task, they require detailed, explicit instructions—these
are called programs.
The person who writes and updates these instructions is known as a programmer or
developer.
C Programming
❑ Agenda:
▪ Compilation Process.
▪ Data Types and Casting.
▪ Operators.
▪ Decision Making Statements.
▪ Loops.
▪ Function.
▪ Arrays and Strings.
▪ Pointers.
▪ User Defined Data Types in C.
▪ Typedef keyword.
C Programming
❑ Agenda:
▪ Compilation Process.
▪ Data Types and Casting.
▪ Operators.
▪ Flow Control.
▪ Loops.
▪ Function.
▪ Arrays and Strings.
▪ Pointers.
▪ User Defined Data Types in C.
▪ Typedef keyword.
Compilation Process
• The compilation is the process of converting the source code of the C
language into machine code.
• To run a C program, you have to cross following stages:
• Editor (.c and .h): to edit C code even .c files or .h ones.
• Preprocessor (.i): handles Preprocessors (#include).
• Compiler (.s): that convert your pure C code into assembly
language.
• Assembler (.o): that convert assembly into machine Language.
• Linker (.exe): final stage
It links all object codes to library code and produce one exe.
Compilation Process
• Integrated Development Environment (IDE):
• All in one Solution
• Editor, preprocessor, Complier, assembler, linker and debugger.
• It’s a software application that provides tools to make writing, testing, and
debugging code easier — all in one place.
C Programming
❑ Agenda:
▪ Compilation Process.
▪ Data Types and Casting.
▪ Operators.
▪ Flow Control.
▪ Loops.
▪ Function.
▪ Arrays and Strings.
▪ Pointers.
▪ User Defined Data Types in C.
▪ Typedef keyword.
Data types and Casting
• Variable:
• variable in C is a named piece of memory which is used to store data and access it
whenever required.
• To create a variable in C, we have to specify a name and the type of data it is going to
store in the syntax.
• type of data determines the size (range of values) and layout of memory.
• Rules for Naming Variables in C:
• A variable name must only contain letters, digits, and underscores.
• It must start with an alphabet or an underscore only. It cannot start with a digit.
• No space is allowed within the variable name.
• A variable name must not be any reserved word or keyword.
• The name must be unique in the program.
Data types and Casting
• Signed:
• Not signed:
Data types and Casting
Range Formula
Unsigned = 0 to (2^n) - 1
Signed= - (2^(n-1)) to (2^(n-1)) - 1
Data Type Size (Bytes) Size (Bits) Minimum Value Maximum Value
char 1 8 -128 127
unsigned char 1 8 0 255
int 2/4 32 -2,147,483,648 2,147,483,647
unsigned int 4 32 0 4,294,967,295
float 4 32
Fraction Numbers
double 8 64
long 4/8 32 / 64 -2,147,483,648 2,147,483,647
long long 8 64 -9,223,372,036,854,775,808 9,223,372,036,854,775,807
Data types and Casting
• In embedded Applications:
• No float if your target hardware doesn't contain FPU.
• No integer as integers are data path dependent.
• What will be the output of the next code in case of 2/4 bytes integer size?
#include <stdio.h>
Void main (void) 2 bytes:
{ x= -32768
int x= 32767; 4 bytes:
x++; x= 32767
printf(“x= %d\n”,x);
}
Data types and Casting
• C Data Types:
• 1- int a; a is an integer
• 2-const int a; a is a const integer
• 3-int *a; a is a pointer to integer
• 4- int **a; a is a pointer to a pointer to an integer
• 5- int a[5]; a is an array of 5 integers
• 6- int *a[5]; a is an array of 5 pointers to integers
• 7- int (*a)[5]; a is a pointer to an array of 5 integers
• 8- int (*a)(int) a is a pointer to a function that takes integer
argument and return integer
• 9- int (*a[5])(int) a is an array of 5 pointers to function that takes
integer argument and return integer
Data types and Casting
• Casting: changing the of certain variable type during evaluation a certain
statement.
• Casting types:
• Implicit Casting (compiler casting)
• Doesn’t require a casting operator.
• This casting is normally used when converting data from smaller integral types to
larger.
• Explicit Casting (programmer casting)
• Requires a casting operator.
• This casting is normally used when converting data from variable type to another
one.
Data types and Casting
• Implicit casting: When variables are promoted to higher precision, data is
preserved. This is automatically done by the compiler for mixed data type
expressions.
int i = 2;
float f = i + 3.14159 ; //i is promoted to float
• The compiler downcast the variable in case of the assignment operation from
higher precision variable to a lower one.
short x = 0xAB07;
char y = x; => y = 0x07 // y will contain the least byte only.
Data types and Casting
• Implicit casting
#include <stdio.h>
Void main (void)
{
int i=17; Sum= 116
char c=‘c’; //ascii = 99
int sum = i+c;
Printf(“Sum= %d”,sum);
}
Data types and Casting
• explicit casting
#include <stdio.h>
Void main (void)
{
int sum=3, count=2; mean= 1.5
float mean ;
mean = (float) sum/count
Printf(“mean= %f”,mean);
}
• Note : if we do not use the casting operator the result will be 1 !!!
C Programming
❑ Agenda:
▪ Compilation Process.
▪ Data Types and Casting.
▪ Operators.
▪ Flow Control.
▪ Loops.
▪ Function.
▪ Arrays and Strings.
▪ Pointers.
▪ User Defined Data Types in C.
▪ Typedef keyword.
Operators
• An operator is a symbol that tells the compiler to perform
specific mathematical or logical functions. C language is
rich in built-in operators and provides the following types of
operators:
• 1. Arithmetic Operators. Operator Example
• 2. Increment and Decrement Operators.
• 3. Relational Operators. + A+B= 30
• 4. Logical Operators.
- A-B= 10
• 5. Bitwise Operators.
• 6. Assignment Operators.
* A*B= 200
• 1. Arithmetic Operators: / A/B= 2
• Most C programs perform arithmetic calculations.
• The following table shows all the arithmetic operators % A%B= 0
supported by the C language. Assume variable A holds 10 and
variable B holds 20, then ++ A++= 11
-- B--= 19
Operators
• 1. Arithmetic Operators:
• The results of any arithmetic operations on Integer numbers is always Integer.
For Example, Integer division yields an integer result. For example, the
expression 7 / 4 evaluates to 1 and the expression 17 / 5 evaluates to 3.
• C provides the remainder operator, %, which yields the remainder after
integer division. The remainder operator is an integer operator that can be
used only with integer operands.
• The expression x % y yields the remainder after x is divided by y. Thus, 7 % 4
yields 3 and 17% 5 yields 2.
Operators
• 1. Arithmetic Operators:
• Order of Arithmetic operations:
• 1. Operators in expressions contained within pairs of parentheses are evaluated
first.
• 2. Multiplication, division and remainder operations are applied first. If an
expression contains several multiplication, division and remainder operations,
evaluation proceeds from left to right. Multiplication, division and remainder are
said to be on the same level of precedence.
• 3. Addition and subtraction operations are evaluated next. If an expression contains
several addition and subtraction operations, evaluation proceeds from left to right.
Addition and subtraction also have the same level of precedence, which is lower
than the precedence of the multiplication, division and remainder operations.
Operators
• 2. Increment and Decrement Operators:
• C also provides the unary increment operator ++, and the unary decrement operator –
• If a variable c is incremented by 1, the increment operator ++ can be used rather than the
expressions c= c + 1 or c += 1
• If a variable c is decremented by 1, the decrement operator -- can beused rather than the
expressions c= c- 1 or c -= 1
• If increment or decrement operators are placed before a variable (i.e.,pre-fixed), they're
referred to as the pre-increment or pre-decrement operators.
• If increment or decrement operators are placed after a variable (i.e.,post-fixed), they're
referred to as the post-increment or post-decrement operators.
Operators
• 2. Increment and Decrement Operators:
Operator example Explanation
++ ++a Increment by 1, then use the new value of a
in the expression in-which a resides.
++ a++ Use Current Value in the expression in-
which a resides, then increment a by 1
-- --a Decrement by 1, then use the new value of a
in the expression in-which a resides.
-- a-- Use Current Value in the expression in-
which a resides, then decrement a by 1
Operators
• 3. Relational Operators:
• The relational operators all have the same level of precedence and they associate left to
right.
• The equality operators have a lower level of precedence than the relational operators and
they also associate left to right.
• Note: In C, a condition may be any expression that generates a zero (false) or nonzero (true)
value.
Operators
• 4. Logical Operators:
• To test multiple conditions in the process of making a decision, we had to
perform these tests in separate statements.
• C provides logical operators that may be used to form more complex
conditions by combining simple conditions.
• The logical operators are && (logical AND), II (logical OR) and ! (logical NOT)
also called logical negation.
Operators
• 4. Logical Operators:
• The following table shows all the relational operators supported by [Link]
variable A holds 1 and variable B holds 0 then
Operator Description Example
&& Called Logical AND operator. If both the operands (A&&B) is false
are non-zero, then the condition becomes true.
|| Called Logical OR Operator. If any of the two (A||B) is true
operands is non-zero, then the condition becomes
true.
! Called Logical NOT Operator. It is used to reverse !(A&&B) is true
the logical state of its operand. If a condition is
true, then Logical NOT operator will make it false.
Operators
• 5. Bitwise Operators:
• Apply to any "integral" data type like char, short, Int an long.
• Arguments are treated as bit vectors.
• Operations applied bitwise.
Operators
• 5. Bitwise Operators:
• C= A&B A=01100110
B=10110011
C= 00100010
• C= A|B A=01100110
B=10110011
C= 11110111
• C= A^B A=01100110
B=10110011
C= 11010101
• C= ~A A=01100110
C= 10011001
Operators
• 5. Bitwise Operators:
• C= A<<3 A=01100110
C= 00110000
• C= A>>3 A=01100110
C= 00001100
• C= (B<<4) | (A&0X0F) A=01100110 => 00000110
B=10110011 => 00110000
C= 00110110
Operators
• 5. Bitwise Operators:
• Don't confuse between & (bit wise operator) and (logical operator)
• 1 (01) & 2 (10) is zero
• 1 (01) && 2 (10) is True
• Note: >> is equivalent to divide by 2
<< is equivalent to Multiply by 2
• How can I multiply number by 9 without use the multiplication operation?
• X= (X<<3)+X;
Operators
• 5. Bitwise Operators:
• What will be the output of the next code?
#include <stdio.h>
Void main (void) Result1= 0
{ Result2= 1
Printf(“Result1= %d”,(-1^!0);
Printf(“Result2= %d”,(5||0);
}
Operators
• 5. Bitwise Operators:
• What will be the output of the next code?
#include <stdio.h>
Void main (void)
{
unsigned char x=0x21;
unsigned char y;
y=x>>4;
y=y<<3;
printf(“%d”,y);
printf(“\n%d”,(1|4)^13);
}
Operators
• 6. Assignment Operators.
• =
• += A+=5 => A= A+5
• -= A-=5 => A= A-5
• *= A*=5 => A= A*5
• /= A/=5 => A= /-5
• %= A%=5 => A= A%5
• <<= A<<=5 => A= A<<5
• >>= A>>=5 => A= A>>5
• &= A&=5 => A= A&5
• |= A|=5 => A= A|5
• ^= A^=5 => A= A^5
THANK
YOU