Chapter 5
Chapter 5
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
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
8
Scope
9
Scope
11
Scope
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
14
Global Scope
15
BITWISE OPERATORS
16
BITWISE 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
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
22
THE FUNCTION
23
THE FUNCTION
24
THE FUNCTION
Reading assignments:
Value passing
Pointers
Nested functions
Function scope
25
Dynamic memory allocation and
management
26
Dynamic memory allocation and
management
28
DYNAMIC MEMORY ALLOCATION
29
DYNAMIC MEMORY ALLOCATION
30
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
35
Multiprogramming
36
Multiprogramming
37
Multiprogramming
38