Sunteți pe pagina 1din 24

Chapter 3

Microcontroller System Software


Development
Assoc. Professor Dr. Rosbi bin Mamat
rosbi@fke.utm.my

 

Dept. of Control & Mechatronics Engineering


Faculty of Electrical Engineering
Universiti Teknologi Malaysia

3.1 Software Development Process


Software development process for embedded
systems

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.1/46


3.2 Arduino Software Development


Software development tools for Arduino
Integrated Development Environment (IDE)
Arduino IDE (on PC)

Target Hardware

Text Editor,
GNU C compiler + Assembler
+ Linker + Locator

USB cable

Machine Code
+
Bootloader

Serial Monitor
(debugging)

c 2013 Dr. Rosbi Mamat p.2/46


Microcontroller (SEL4533)

3.2 Arduino Software Development


Source codes to machine code translation using
Arduino IDE
Your source file
myprog.ino

Arduino & Wiring


libraries

GNU C
libraries

GNU C compiler + Assembler


+ Linker + Locator

Machine Code
Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.3/46


3.2 Arduino Software Development


Arduino IDE
Compile
Upload

Open

Serial Monitor

Save
Area for Writing Program
(source code editor)

Area for Error Messages

c 2013 Dr. Rosbi Mamat p.4/46


Microcontroller (SEL4533)

3.3 Arduino Programming Language


Arduino language is based on C/C++ language we
will review the C language.
The basic stucture of an Arduino program:
comments

function name
function
start
function body
function end
Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.5/46


3.3 Arduino Programming Language


Each Arduino program must have at least 2
functions:
setup() is the 1st thing run in the Arduino
prog. This is where things that need to be
initialized should be placed. setup() run once &
only once.
loop() contains anything that needs to run
repeatedly in the prog. Any instructions in loop()
will be run repeatedly until the prog is stopped.

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.6/46


3.4 A Brief Review of C language


A C program consists of :
functions to be executed
variables declaration, definition or allocation
C functions
similar to ASM subroutines or Pascal procedures
a function contains statements that specify the
computing operations to be done & variables
declaration for storing data. Function body are
enclosed in braces { }
main is a special function program execution begins
at main.

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.7/46


3.4 A Brief Review of C language


C functions
Where is the main() function in Arduino progs?
In Arduino, main is defined internally you do not
need to write the main
int main(void)
{
:
setup();
for (;;) {
loop();
:
}
return 0;
}

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.8/46


3.4 A Brief Review of C language


Comments
any characters between /* & */ are ignored by
compiler
anything after // until end of line
blank lines are ignored by compiler

Preprocessor instructions
use to include the contents of other files with
#include<..> or #include ".."
use to define constants & macros with #define ...

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.9/46


3.4 A Brief Review of C language


C variables & data types
variables are used to store data
all variables must be declared before they are used.
Declaration consists of a data type & the symbolic name
for variable:
int ADCreading; /*var ADCreading store integer value */
float ctrsignal; /* var ctrsignal store real value */

signed int data types for variables


Data Types

Size (bytes)

Ranges

char

-128 +127

int

-32768 +32767

short

-32768 +32767

long

-2147483648 +2147483647

c 2013 Dr. Rosbi Mamat p.10/46


Microcontroller (SEL4533)

3.4 A Brief Review of C language


C variables & data types
unsigned int data types for variables
Data Types

Size (bytes)

Ranges

unsigned char

0 255

unsigned int

0 65535

unsigned long

0 4294967295

real data types for variables


Data Types

Size (bytes)

Ranges

float

1.18 1038 3.4 1038

double

9.46 10308 1.79 10308

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.11/46


3.4 A Brief Review of C language


Constant numbers in C
Constant Types
Examples
Decimal number
65
Long Decimal number
65L
Hexadecimal number
0x41 or 0X41
Octal number
0101
Character
A
Real number
65.0, 65E0, 650E-1

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.12/46


3.4 A Brief Review of C language


Character constant in C (must be enclosed in ):

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.13/46


3.4 A Brief Review of C language


Identifier symbolic names given to variables,
functions, constants or labels.
Valid identifier must:
start with a letter or underscore (_)
consists of letters, numbers or _ only
not one of these C keywords:

C identifier is case sensitive !


auto
break
case
char
const
continue
default
do
double
else
enum
Microcontroller (SEL4533)

extern
float
for
goto
if
int
long
register
return
short
signed

ADCreading , adcreading

sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
while
c 2013 Dr. Rosbi Mamat p.14/46

3.4 A Brief Review of C language


C statements
specify actions to be performed, such as assignment
operation or function calls.
are free-form i.e. can start & finish at any column
must be terminated with semicolon (;)
are executed in sequence
block of statements are enclosed in { & } & not
terminated by a semicolon
a statement with only a semicolon is an empty statement
& does not peform any operation
for (i = 0; i < 100; i++)
;
/* empty statement */
Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.15/46


3.4 A Brief Review of C language


Arithmetic operators

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.16/46


3.4 A Brief Review of C language


Relational operators

Assignment operators

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.17/46


3.4 A Brief Review of C language


Bitwise operators

Logical operators

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.18/46


3.4 A Brief Review of C language


Memory accessing operators

Other operators

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.19/46


3.4 A Brief Review of C language


Program control structures for decision making &
controlling program flow such as doing selections,
iterations & loops.
if . . . else creates conditional jump
TRUE

FALSE
condition?
if (condition)
statements; TRUE
statements

FALSE
condition?

if (condition)
statementT;
else
statementF; statementT

statementF

condition = 0 FALSE, condition , 0 TRUE


c 2013 Dr. Rosbi Mamat p.20/46

Microcontroller (SEL4533)

3.4 A Brief Review of C language


if . . . else if . . . else for multiple alternatives
conditional jumps
if (condition1)
statement1;
else if (condition2)
statement2;
else if (condition3)
statement3;
:
else
statementn;
switch (condition)
{
case condition1:
statement1; break;
case condition2:
statement2; break;
case condition3:
statement3; break;
default:
statementn;
}
Microcontroller (SEL4533)

TRUE
condition1?

statement1

FALSE
TRUE
condition2?

statement2

FALSE
TRUE
condition3?

statement3

FALSE
statementn

c 2013 Dr. Rosbi Mamat p.21/46


3.4 A Brief Review of C language


Iteration & loops with while & do . . . while:
while (condition)
{
statement1;
statement2;
:
statementn;
}

FALSE
condition?
TRUE
statements

do
{

statements

statement1;
statement2;
:
statementn;
} while (condition)

condition?
TRUE
FALSE

c 2013 Dr. Rosbi Mamat p.22/46


Microcontroller (SEL4533)

3.4 A Brief Review of C language


Syntax of for loop:
Initialization
part

Update part

for (expr_init; condition; expr_update)


statements
Conditional
part

Actions of for keyword:


Initialization part is executed once at the start of the
loop
Conditional part is tested, if TRUE the statements are
executed
Update part is executed at the end of each loop iteration
Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.23/46


3.4 A Brief Review of C language


for loop:
initialization
for (expr_init; condition; expr_update)
statements

condition?

FALSE

TRUE

||
expr_init;
while (condition)
{
statements
expr_update;
}

statements

update

c 2013 Dr. Rosbi Mamat p.24/46


Microcontroller (SEL4533)

3.4 A Brief Review of C language


C functions
General form of a function
output_type name (par_type parameters)
{
statements
return(out_value);
}

parameters

name

output/
return value

output_type the type of the functions return


value/ouput (int, char, float etc.). If no output
keyword void is used.
name is the name of the function. Follow the rules for
identifier for writing name.
Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.25/46


3.4 A Brief Review of C language


C functions
par_type parameters the declaration of variables
that are passed to the function to do the work. If more
than 1 parameters, separate them with ,.
If the output_type is not void, the last statement in the
function is return(out_value).
Example:.
/* calc the area of a rectangle */
float calc_area_rect (float width, float length)
{
float area;
area = width * length;
return(area);
}
void main(void)
{
float area;
area = calc_area_rect(2.5, 6.0);
}

/* call function to do the work */

c 2013 Dr. Rosbi Mamat p.26/46


Microcontroller (SEL4533)

3.4 A Brief Review of C language


Array in C.
Arrays are used to store large numbers of data of the
same type & only 1 variable is used to reference them.
The format for declaring an array:

data_type name [array_size]


data_type char, int, float, etc.
name name of array that will be used to access the
array. Follow the rules of identifier.
array_size the number of item the array can store.
array can be initialized when declaring. In this case
array_size can be ommited.
each item/element of an array is referenced using the
name & index: name[0] is the 1st item & name[array_size-1]
is the last item.
Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.27/46


3.4 A Brief Review of C language


Array in C.
Exercise: what are the values of sum, average & the arrays
length & len elements after this program is executed ?
How many bytes of memory are used to store the array len?
void main(void)
{
int i, sum, average;
int length[] = {7, 6, 4, 7, 2, 10};
int len[6];
sum = 0;
for (i = 0; i < 6; i++)
{
sum += length[i];
len[5 i] = length[i];
}
average = (length[5] + length[3])/2;
length[3] += 3;
length[2] = length[2] + average;
}
Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.28/46


3.4 A Brief Review of C language


Pointers in C.
A pointer is a variable which can store an address of a
variable of function.
A pointer refers to a location in memory, & its type
indicates how the data at this location is to be
interpreted.
The unary operator & gives the address of a variable.
The unary operator * is the indirection / dereferencing
operator; it accesses the variable the pointer points to.
2 arithmetic operations can be performed on pointers:
An integer can be added to or subtracted from a
pointer.
One pointer can be subtracted from another pointer
of the same type.
Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.29/46


3.4 A Brief Review of C language


Pointers in C.
Example:
RAM (before)

char c;
char *cptr;
int x;
int *iptr;

/* pointer to an integer */

cptr = &c;
*cptr = 0x45;
++cptr;

/* cptr = 0x1000
/* c = 0x45
/* cptr = 0x1001

*/
*/
*/

iptr = &x;
*iptr = c;
++iptr;

/* iptr = 0x1002
/* x = 0x0045
/* iptr = 0x1004

*/
*/
*/

/* pointer to a char

*/

Address

Contents

Variables

0x1000

0x90

0x1001

0x78

0x1002

0x56

0x1003

0x34

RAM (after)
0x1000

0x45

0x1001

0x78

0x1002

0x00

0x1003

0x45

c 2013 Dr. Rosbi Mamat p.30/46


Microcontroller (SEL4533)

3.4 A Brief Review of C language


Pointers in C.
Example: Code fragment to copy 5 bytes from RAM
starting at address 0x0C00 & store in an array.
#define RAM_start (char *) 0x0C00
char * ptr2ram;
char ramcopy[5];
int i;

/* declare a ptr to RAM


*/
/* array to store copy of RAM */

ptr2ram = RAM_start;
for (i=0; i< 5; i++)
{
ramcopy[i] = *ptr2ram;
++ptr2ram;
}

/* ptr2ram points to 0x0C00


/* do it 5 times

Microcontroller (SEL4533)

*/
*/

/* copy a byte to array


*/
/* update ptr to next location*/

c 2013 Dr. Rosbi Mamat p.31/46


3.5 Program Design & Construction


Program development cycle:

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.32/46


3.5 Program Design & Construction


Planning the logic of a program involves planning
the steps of the program, deciding what steps to
include and how to order them algorithm.
Algorithm is the recipe how to solve a problem with
computer programs. Without algorithm, there is no
program. Without program, there is no solution.
Some of the tools used in planning the logic &
expressing algorithm: language statement, pseudo code
& flowcharts.

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.33/46


3.5 Program Design & Construction


Some important symbols in flowchart:
Terminator

Process

Subroutine

Add Sugar

Calc average

START
END

Decision

Continuation

Input/Output

A
A

yes

Need sugar?

no

c 2013 Dr. Rosbi Mamat p.34/46


Microcontroller (SEL4533)

3.5 Program Design & Construction


Pseudo codes & their flowchart structures:
Pseudo Codes
step_1
step_2
. . .
. . .
step_n

Flow Charts
step_1

step_2

.
.

condition then
true_part
else
false_part
endif

step_n

if

Microcontroller (SEL4533)

no

yes
Condition?

false_part

true_part

c 2013 Dr. Rosbi Mamat p.35/46


3.5 Program Design & Construction


Pseudo codes & their flowchart structures:
Pseudo Codes

while condition do
repeated_part
endwhile

Flow Charts

condition?

yes

repeated_part

no

repeat
repeated_part
until condition

Repeated_part

no

condition?
yes

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.36/46


3.5 Program Design & Construction


Example: Write/draw the algorithm for making a cup of
coffee.
The algorithm in language statements form:
1.
2.
3.
4.
5.

boil water
put coffee in the cup
if want sugar then add sugar
if want cream then add cream
pour hot water into the cup

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.37/46


3.5 Program Design & Construction


Example: The algorithm in flowchart:
START
boil water
put coffee
Yes

sugar?

add sugar
No
Yes

cream?
add cream
No

pour hot water


END
c 2013 Dr. Rosbi Mamat p.38/46

Microcontroller (SEL4533)

3.5 Program Design & Construction


Example: The algorithm in pseudo codes:
boil water
put coffee in the cup
if wantSugar is true then
add sugar
else
dont add sugar
endif
if wantCream is true then
add cream
endif
pour hot water into the cup

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.39/46


3.5 Program Design & Construction


Example: The coffee making algorithm is expressed in
general statements. This is the Top Down approach of
solving problem. Some of these statements may require
further detailing for implementation. For example the boil
water process can be further detailed out as:
boil water

put water in the kettle


light the fire
repeat
wait
until water is boiling

This process of further detailing is called step-wise


refinement.
Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.40/46


3.5 Program Design & Construction


Sructured Programming: is a logical programming
method that enforces a logical structure on the
program being written to make it more efficient &
easier to understand & modify.
A Structured Program has the following charac.:
Includes only combinations of the 3 basic structures
sequence, selection, & loop.

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.41/46


3.5 Program Design & Construction


A Structured Program:
Structures can be connected to one another only at
their entry or exit points
Any structure can be nested within another structure
Does not requires goto or jump statement

Example: a structured flowchart with pseudo code:

Yes

No
cond1?
No

Yes
cond2?
stmt

while cond1 is true do


if cond2 is true then
stmt
endif
endwhile

c 2013 Dr. Rosbi Mamat p.42/46


Microcontroller (SEL4533)

3.5 Program Design & Construction


Exercise: This is an unstructured (spaghetti) flowchart.
Why is this unstructured? Write the pseudo or C code for
it. Convert the flowchart to a structured one & write the
structured pseudo or C code.

stmt1
Yes

No
cond1?
No
stmt2

Microcontroller (SEL4533)

Yes
cond2?
stmt3

c 2013 Dr. Rosbi Mamat p.43/46


3.6 Debugging with Arduino IDE


Serial Monitor can be used to display values &
messages for debugging Arduino programs.

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.44/46


3.6 Debugging with Arduino IDE


Serial Monitor commands for using it:
Serial.begin(9600) to enable input/output to
serial monitor. Must be written in setup()
Serial.println(val) to display val value to serial
monitor with newline added.
Serial.print(val) as above but without newline.
Serial.print(Error) display message Error
without newline.

Make sure USB cable is connected to PC & the same


baud rate value (9600) is selected in serial monitor!

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.45/46


3.6 Debugging with Arduino IDE


Example: displaying values & messages for
debugging Arduino programs.
int debugMode = true;
int result;

// set to false to turn off


// variable to be monitored

void setup() {
Serial.begin(9600);
}
void loop() {
:
if (debugMode) {
Serial.println(In Debug"); // these messg will be
Serial.print(Result = "); // printed only
Serial.println(result);
// in debug mode
}
:
}

Microcontroller (SEL4533)

c 2013 Dr. Rosbi Mamat p.46/46

S-ar putea să vă placă și