Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 36

Embedded Systems

Engineering

R.A. Prabhath Buddhika
B.Sc.Eng(Hons)(Moratuwa)
Department of Electronic & Computer Engineering
SLIIT
C Programming

Lecture 02

--Embedded Systems Engineering-- 2


Objectives

Refresh the C programming skills
Select the appropriate data types for
variables
Use the operators effectively
Write optimal C functions
Explain the use of structures, unions and
arrays
Apply pointers effectively in C programs
--Embedded Systems Engineering-- 3
Software Programming Tools

4th Generation CASE
Language Source

3rd Generation *.c (C Source Code)


Compiler
Language Source *.h (C Header File)
*.s or *.asm
Assembly Code
Source Assembler

*.o
Object Code
Linker
(Library Files)
*.lst, *.map, *.s19
--Embedded Systems Engineering-- 4
‘C’ Characteristics

 “Low-level” Language
 Stays very close to a processor’s hardware.
 Minimalistic
 Basic single-thread control flow constructs + optional
libraries.
 “Lightly-Typed” Language
 You can do almost anything, including shoot yourself
in the foot!
 Function-Based
 Emphasizes structured design and modularity

--Embedded Systems Engineering-- 5


Global & Local Variables
 A variable is a storage place in memory to hold values
during execution of a program. 
 A variable could be a location in memory (RAM or ROM) or
even a register.
 There are 2 types of variables
 Local
 Accessible by the function where local variables are declared
 Usually appear in stack
 Global
 Accessible all the functions
 Validity and accessibility of the variable depends on the
scope of declaration.
 The identifier scope covers variables, functions, structure
components, type tags, and preprocessor macros and more.
--Embedded Systems Engineering-- 6
Global & Local Variables

 A variable that remains constant during the
execution of a program is called a constant variable
and it should reside in ROM.

 A non-constant variable should reside in RAM.

--Embedded Systems Engineering-- 7


‘C’ Variable Types

 char
 Always a single byte. Also used for text.
 Int
 “Usually” matches the size of basic storage unit. Often 2
bytes.
 Float
 Single precision floating point (Used for fractional math)
 Double
 Double precision floating point (Wider range of numbers)

--Embedded Systems Engineering-- 8


Variable Modifiers

Short
 Remains a 2 byte integer.
Long
 Increases number of bytes used for an integer (4
bytes).
signed
 Two’s complement numbers (DEFAULT).
Unsigned
 Allows unsigned arithmetic.
--Embedded Systems Engineering-- 9
Variable Modifiers

Static
 Directly allocates memory to remember a value
between function calls.
Extern
 Allows access by other files.
Const
 Assigns a constant value to variable.

--Embedded Systems Engineering-- 10


Types of Data found in C for the
68HC12 Using
ICC12 Software
Type Description  Memory Size in
68HC12/ICC12
Range in
68HC12/ICC12
char Character 1 byte -128 to 127
unsigned char Character 1 byte 0 to 255
short int Integer 2 bytes -32,768 to 32,767
int integer 2 bytes -32,768 to 32,767
unsigned int integer 2 bytes 0 to 65,535
long int integer 4 bytes -2,147,483,648 to
2,147,483,647
float floating point 4 bytes Not recommended *
double double 4 bytes Not recommended *
* Manipulation of float and double takes up a considerable number of
instructions and impedes efficient program execution
--Embedded Systems Engineering-- 11
Derived Data Types in C

 Array: A set of data elements of the same data type.

 Pointer: A variable that contains the address of a data type.

 Structure: A set of data elements where each element can be


a different data type.

 Union: A memory element that is shared by two different


data types.

 Function: A function being it self a data type can generate a


data type and can return a data type.
--Embedded Systems Engineering-- 12
Declaration & Initialization

access(and or)storage-type
type-specifier
identifier([optional array element count])
= value;
Example
static int number = 5;

--Embedded Systems Engineering-- 13


Declaration & Initialization
 access (and or) storage-type Dictates the type

of memory used to store the variable

 type-specifier Should contain one of the data types


shown in the previous table

 identifier is the user defined variable name

 The equate operator along with a numerical value or a


character string is used to define or initialize the variable.

--Embedded Systems Engineering-- 14


access (and or) storage-type
auto
 Default storage type. In 68HC12 stack is used to store this type.
Static
 Stored in RAM 
Register
 If possible store in a CPU register
Const
 A variable that does not change during execution. Stored in ROM.
volatile
 Used for variables that could change its values without specific
instructions from the program. For example by hardware input
ports.
extern
 Specifies a variable that has been declared and defined in some
other file. --Embedded Systems Engineering-- 15
Operators
 Arithmetic 
+ - * /
% modulo (remainder) operator
EX] 11%3 -> 2

 Relational
< <= > >=
== != && ||
EX] if (num <= 0) || (num > 100)
{
:
}
--Embedded Systems Engineering-- 16
Operators
 Bit-wise
&
EX]
| ^ ~ 
unsigned char a, b;
a = b & 0x0F; //0x: hexadecimal

>> <<
EX]
unsigned char a, b;
a = b >> 2; //right shift b twice

 Increment/Decrement
++ --
EX] i++;
equivalent to i--Embedded
= i + Systems
1; Engineering-- 17
Bit Twiddling - controlling
individual bits
 a|b bitwise or - used to turn on specific bits
EX] PORTA | = 0x80; 
//turn on bit 7 (msb)

 a&b bitwise and - useful for checking if specific bits are set
EX] if ((PORTA & 0x81) == 0) //check bit 7 and 0

 a^b bitwise exclusive OR - useful for complementing a bit


EX] PORTA ^= 0x80; //flip bit 7

 ~a bitwise complement - used to turn off specific bits


EX] PORTA &= ~0x80; //turn off bit 7

--Embedded Systems Engineering-- 18


Operator precedence

--Embedded Systems Engineering-- 19


while{} Loops
while(expression)

k = -10;

{
while(k < 41)
statement1;
{
statement2;
Temp = k * 9/5 + 32;
}
k++;
printf(“Temp: %f \n”,
Temp);
Loop continues to run till }
expression is non-zero

--Embedded Systems Engineering-- 20


for{} Loop
for
for(k=-10; k<=40; k++)
(exp1;exp2;exp3) {
{ Temp = k * 9/5 + 32;
statement1; printf(“Temp: %f \n”,
statement2; Temp);
} }

Loop continues to run till exp2 is non-zero. If it is empty, TRUE is


assumed.

--Embedded Systems Engineering-- 21


do/while{} loop
do k = -10;
{ do
statement1; {
statement2; Temp = k * 9/5 + 32;
} while k++;
(expression); printf(“Temp: %f \n”,
Temp);
} while (k < 41);
Loop continues to run till expression is non-zero. Loop body is
executed first.

--Embedded Systems Engineering-- 22


if-else-if Statement

if(expression) if(c < min_temp)
statement1; flag = TOO_LOW;
else if(expression) else if (c> max_temp)
statement2; flag = TOO_HIGH;
else
else
{
{
temp = v * m + b;
statement3;
flag = IN_BOUNDS;
statement4;
}
}
--Embedded Systems Engineering-- 23
switch-case statement

switch(expression){
case const-expr:
switch(a){
case ‘A’:
statements x= 5;
case const-expr: break;
statements case ‘B’:
default: x= 3;
statements break;
} default:
X=1;
Break;
}
break makes an immediate exit from the switch block. It can also
be used to exit while, for or do-while loops
--Embedded Systems Engineering-- 24
Functions
 Function Prototype
<output type> func_name(<input type1> <variable name1>, ...);

 Function Call

<variable> = function(<variable name1,...>);

 Function
output type func_name(<input type1> <variable name1>, ...)
{
<Local variables>
statement1;
:
:
return(output variable);
}

--Embedded Systems Engineering-- 25


Function Example
Prototype:


long int square(int number);

Call:
squared_val = square(1000);

Function:
long int square(int number)
{

Fill the function body…

}
--Embedded Systems Engineering-- 26
Arrays

 Collection of the same type of data, stored in
consecutive memory locations.

 Declaration Examples:
int temp[NUM_SAMP];
char msg[] = {“Hello World\n”};

 Assignment Example:
temp[2] = 50;

--Embedded Systems Engineering-- 27


Structures

 Collection of multiple variables, possibly of different
types.

 Declaration Example:
struct circle struct circle circle1;
{
int radius;
int x_center;
int y_center;
};

--Embedded Systems Engineering-- 28


Structures (Cont.)

 Assignment Example:

circle.radius = 5;

Write a function to return the perimeter of


the circle.

--Embedded Systems Engineering-- 29


Unions

 Unions provide a way to manipulate different types
of data in a single area of storage

union u-tag {
int ivalue;
float fvalue;
int i = u-tag.ivlaue;
char *svalue;
} float f = u-tag.fvalue;

--Embedded Systems Engineering-- 30


Pointers

 Variables assigned to specific addresses.

 Declaring pointers
int x;
int *px, *pa; //address of
integers

pointers --Embedded Systems Engineering-- 31


Pointer Syntax

px = &x; //assign pointer to the address of x

address of

x = *px; //set x equal to contents of px

dereference operator, contents of (indirection operator)

--Embedded Systems Engineering-- 32


Pointer example

int m,n; //integers
int *pm; //pointer to integer type

m = 10; //set m equal to 10


pm = &m; //set integer pointer to address of
m
n = *pm; //set n equal to contents of pm
//n = 10

--Embedded Systems Engineering-- 33


Pointer Syntax

 Placing data at specific locations:
* (char *) 0x102B = 0x30;

Hexadecimal value

Hexadecimal value

Pointer of character size

Contents of the address


--Embedded Systems Engineering-- 34
Pointer Syntax - header files
#define _IO_BASE = 0

#define _P(off) *(unsigned char volatile*)
(_IO_BASE + off)
#define _LP(off) *(unsigned short
volatile*) (_IO_BASE + off)

#define PORTA _P(0x00)


:
:
 Allows you to use register file by name in code
 Must include header file with code

--Embedded Systems Engineering-- 35



Thank you!

--Embedded Systems Engineering-- 36

You might also like