0% found this document useful (0 votes)
33 views38 pages

Chapter 5

Uploaded by

Fenan Zeinu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views38 pages

Chapter 5

Uploaded by

Fenan Zeinu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Chapter Five: Embedded C

Programming
Introduction
 C programming is one of most widely used high level programming
language in embedded system application software development
Here elements of the C programming language that are essential to
develop embedded systems will be covered
Good programming style and some important considerations are
crucial for developing programs that are robust, reliable, and
maintainable
1
Embedded C Programming …
Continued
Fundamentals of the C language, specifically variables, their type,
and scope will also be revisited here
Understanding structure of a C program and details of working with
multiple-file programs is necessity for larger applications
Understanding and implementing Bit operators and functions are
important in embedded software development

2
Data Types, Operators and
Expressions Revisited
Variables are the basic data objects manipulated in a program
Declarations list
The variables to be used,
The types of data variables are defined for and
Perhaps what their initial values are
Operators specify what is to be done to them
 Examples: +, - , x, /, = ….etc.
3
Data Types, Operators and
Expressions Revisited
Expressions combine variables and operators to produce new
values
Exmaple: tempCentigrade = tempKelvin + 273
Type of an object determines the set of values it can have and what
operations can be performed on it
Some of data types widely used are: integer (signed, unsigned,
short, long), char, float, string, bool … etc.

4
Identifiers Revisited

Name of a variable or a function in C is called an identifier


An identifier is case-sensitive
main != Main
The first character of an identifier must be an alphabetic character
or an underscore
Identifiers cannot be a C keyword
Typically, variable and function identifiers begin with a lower case
letter, if not underscore, and should be descriptive 5
const Qualifiers

It is used to declare or specify a constant


One immediate application of the const qualifier is as a replacement
for the #define preprocessor directive
Example: const int tempAcquirePin = A0 is equivalent with
#define TEMPACQUIREPIN A0

6
Type Conversions

As we work with those types, we will find that at times we have to
convert from one type to another
Example: to display values on monitors, in most cases the value
need to be type of string, that means there is need of conversion
from and particular data type to string
(targetType) sourceExpression

7
Type Conversions

Example code fragment below illustrate type casting or type


conversion
unsigned int anInt = 0;
unsigned char aChar = ‘b';
anInt = (unsigned int) aChar;

8
Scope

Scope specifies a variable’s visibility within the program


The structure or architecture of a program describes how a program
is organized
One can have one rather large main function containing the entire
program, which is difficult for debugging and future modifications
For those who find the all-in-one model underwhelming, there are
two possibilities to deal with

9
Scope

The first option is the multiple module single-file approach


Each module contains related functions and subroutines
Decompose the program into multiple files, each of which may
contain multiple modules
Individual implementation files can be compiled and linked
either at the same time or at different times
Debugging and maintenance drastically simplify things
10
Scope

For multiple module or multiple file system one critical question


will raise with regard to shared data
These question relate to what is defined as the scope of the
variable’s name
Scope establishes how widely a variable name is known within
an application

11
Scope

C specifies the three primary scopes

12
Local Scope

Local variables are only visible within the block in which they
have been declared and defined
example: int main(){
}
int readSensor1(){
int rawRead;
}
13
Local Scope

In the above code block fragment particularly in the readSensor1


function, the variable rawRead is local and only visible in that
module
Variables in a local scope are not initialized upon declaration
unless special provisions are made

14
Global Scope

Global variables are visible everywhere in the program or file(s)


When they are declared, they are initialized to 0 by default in
most compilers
They provide a means to share data between functions or tasks
without the overhead of physically moving that data (or a
reference to it) from one place to another
No need to function call

15
BITWISE OPERATORS

Bitwise operators in C are more commonly found in the


embedded systems developer’s tool box rather than in that of
the traditional application developer’s
Such operators are intended for work at the hardware level –
that is, with the registers and input or output ports on a target
machine

16
BITWISE OPERATORS

Table below shows some common operators

17
BITWISE OPERATORS

Examples:
PORTBbits.PB0 = ~PORTBbits.PB0; bit toggling
PORTBbits.PB0 = 1 ; set bit zero of portb
Logical bitwise operators can be used to determine
whether a specific bit within a byte is set or reset – that
is, has a value of logical 1 or logical 0

18
BITWISE OPERATORS

Assume that PORTA pin three is used to receive input from


switch
When this switch is closed notification should be sent to person
in charge via buzzer which is connected to PORTB pin 0
if(PINAbits.RA3 == 1){
PORTBbits.PB0 = 1;
}
19
THE FUNCTION

A C program is simply a collection of functions


We create a function by defining it, which involves specifying
and designing a function header and a function body
Function header specifies the function name, the return type,
and a parameter list. The header is also called a prototype
For temperature monitoring and controlling system you an define
and use two functions besides your main function like

20
THE FUNCTION

void temperatureMonitor(void);
void temperatureControl(void);
The above two functions fulfilled the function definition protocol
and they are both prototype
In this case we should have global variable(s) to be used in the
main function to execute intended task(s)

21
THE FUNCTION

Function could have arguments to deal with some input


If we assume the previous temperature monitor example and
want to provide input as argument, then we can modify the
function as follows
void temperatureMonitor(int);
Another method by which data can be returned from a function
is to use its return statement

22
THE FUNCTION

When a value is returned from a function in such a way, the


function is said to have a return value
The type of data returned is called the return type
Example of function with return
double temperatureMonitor(int);
We can use the above function to return temperature calculated
from raw data

23
THE FUNCTION

24
THE FUNCTION

Reading assignments:
 Value passing
 Pointers
 Nested functions
 Function scope

25
Dynamic memory allocation and
management

In embedded applications, memory management is concerned with


managing the process stack(s) and the static and dynamic
allocation of memory
Static allocation addresses the partition of memory into the code
and data segments used by both the application and system
Dynamic allocation addresses the allocation and management of
memory resources for the processes at runtime

26
Dynamic memory allocation and
management

When managing memory in embedded systems, the major


concerns are
Avoiding dangerous allocation and
Minimizing or reducing overhead during memory allocation
Improper allocation can result in the loss of deterministic
behavior and potentially create deadlock situations at
runtime
27
DYNAMIC MEMORY ALLOCATION

Dynamic memory allocation is usually interpreted as the process of


allocating memory at runtime
It is associated with some extensible data structure such as a
linked list or heap
The linked-list-based scheme works well for an embedded system
In embedded systems we generally use two kinds of memory, ROM
and RAM (which may be cache or main memory)

28
DYNAMIC MEMORY ALLOCATION

Here we are more concerned with managing main memory to


accommodate
Programs larger than main memory
Multiple processes in main memory
Multiple programs in main memory

29
DYNAMIC MEMORY ALLOCATION

Some of schemes for dynamic memory allocation that can work


well in embedded systems are:
Swapping
Overlays
Multiprogramming

30
Swapping

Simplest method for accommodating multiple programs


System remains resident in memory and further assumes that only
A single-user program is resident in memory at a time and
A single task in memory at any one time
Non executing task is suspended and swapped to a secondary
storage device
Here we will use the word “secondary” to refer to non-runtime
memory
31
Swapping

32
Overlays
The overlay will be in ROM and used to accommodate a program
that is larger than main memory
The program is segmented into a number of sections called
overlays
The main section of overlays are:
Top level routine
Code to perform overlay process
Data segment for shared data
Overlay segment 33
Overlays

34
Overlays

When the overlay process is executed, a new overlay segment


replaces the current one in main memory
The code in each overlay must be selected carefully
Usually the segmentation is hand tailored

35
Multiprogramming

Multiprogramming permits one to run multiple programs in the


same memory space (RAM)
Program could have fixed or variable tasks
Fixed number of tasks/threads look very similar to paging systems
To implement the design, the user space is divided into a number of
fixed size partitions and is presented

36
Multiprogramming

Multiprogramming permits one to run multiple programs in the


same memory space (RAM)
Program could have fixed or variable tasks
Fixed number of tasks/threads look very similar to paging systems
To implement the design, the user space is divided into a number of
fixed size partitions and is presented

37
Multiprogramming

Programs with a variable number of tasks are treated in a similar


manner to segments in a traditional virtual memory scheme
Memory allocation for a variable number of tasks is similar to a
paged virtual memory scheme
Allocation is determined by the process’s requirements when it is
loaded into memory

38

You might also like