You are on page 1of 44

Algorithms and Flowcharts

ECET 209 Lecture 7 Introduction to Microcontrollers

Overview
Example Algorithms Flowcharts and C Code

ECET 209

Purdue University

Formal Problem Solving Steps


Understand the Problem Develop an Algorithm Refine the Algorithm Create an Action Plan Generate a Solution Test the Solution
Purdue University 3

ECET 209

Algorithms
Step by step method to solve a problem Must include ALL required information

ECET 209

Purdue University

ECET 209

Purdue University

Flowcharts
Graphical representations of algorithms Few basic symbols
Terminal Processes Decisions

Tool to translate algorithms into software


Flowcharts of C structures Rules for Structured Flowcharting
ECET 209 Purdue University 6

Flowcharting

ECET 209

Purdue University

Decisions

ECET 209

Purdue University

Decisions

ECET 209

Purdue University

Decisions

ECET 209

Purdue University

10

Decisions

ECET 209

Purdue University

11

For Loop vs. While Loop

ECET 209

Purdue University

12

Decision

ECET 209

Purdue University

13

So Many Decisions, How do I choose?


Flexibility
For Example
Any For Loop can be expressed as a While Loop Any Case Switch structure can be replaced by a series of IF/ELSE structures

The Flowchart is your guide!


ECET 209 Purdue University 14

Follow the Flowchart


May have to redraw the flowchart several times to get it into a form that fits one of the C structures. Rules to Flowcharting
A complex task can be shown as a single block Start with the simplest flowchart possible Any process can be replaced by a sequence Any process can be replaced by a control structure
Purdue University 15

ECET 209

Flowchart Rules

Rule 1 Rule 3

Rule 3

Rule 2

ECET 209

Purdue University

16

Flowchart Rules

Configure PortC for Output

Configure I/O PORTS

Configure PortA for Input

Turn on PortA Pull-ups

ECET 209

Purdue University

17

Averaging Example
Averaging a set of numbers sum = 0

Averaging a set of numbers

Average a set of Numbers

i=0

Calculate the Average of the 6 Numbers

Calculate the Sum of the Numbers

i=i+1

sum = sum + ni

Divide the Sum by the Number of Entries

Does i = 6?

No

End
End
Yes

Average = sum / 6

ECET 209

Purdue University

End

18

Averaging a set of numbers

sum = 0

i=0

i=i+1

sum = sum + ni

Does i = 6?

No

Yes

Average = sum / 6

End

ECET 209

Purdue University

19

Relational Operators
Determine how one value relates to another
Equal to == Not equal to != Less than < Greater than > Less than or Equal to Greater than or Equal to
Purdue University

<= >=
20

ECET 209

True vs. False


The result of any relational operation is a True or a False indication
False is defined as a Zero value True is defined as Not False
( if it anything other than zero, it is true!! )

ECET 209

Purdue University

21

More Likely Examples


( number_of_dogs > 3 ) ( value != 0 ) ( counter > 10 )

ECET 209

Purdue University

22

Relational Operators are Typically used with Decisions


For example Problem: Light the upper nibble of the LEDs when the counter is above 10.

ECET 209

Purdue University

23

How do we get from the Flowchart to the C Code??

Is the counter variable greater than 10 ?

Yes

No

Turn on the upper four LEDs

ECET 209

Purdue University

24

How do we get from the Flowchart to the C Code??


What is this??
Is the counter variable greater than 10 ? Yes Turn on the upper four LEDs

No

ECET 209

Purdue University

25

How do we get from the Flowchart to the C Code??


if
Is the counter variable greater than 10 ? Yes Turn on the upper four LEDs

No

ECET 209

Purdue University

26

How do we get from the Flowchart to the C Code??


What??

if
Is the counter variable greater than 10 ? Yes Turn on the upper four LEDs

No

ECET 209

Purdue University

27

How do we get from the Flowchart to the C Code??


if ( counter > 10 )
Is the counter variable greater than 10 ? Yes

No

Turn on the upper four LEDs

ECET 209

Purdue University

28

How do we get from the Flowchart to the C Code??


if ( counter > 10 )
Is the counter variable greater than 10 ? Yes

{
Turn on the upper four LEDs

No

}
ECET 209 Purdue University 29

How do we get from the Flowchart to the C Code??


if ( counter > 10 )
Is the counter variable greater than 10 ? Yes

{
Turn on the upper four LEDs

No

}
ECET 209 Purdue University 30

How do we get from the Flowchart to the C Code??


if ( counter > 10 )
Is the counter variable greater than 10 ? Yes

{
Turn on the upper four LEDs

PORTC = 0xF0;

No

}
ECET 209 Purdue University 31

ECET 209

Purdue University

32

How do we get from the Flowchart to the C Code??


if ( counter > 10 )
Is the counter variable greater than 10 ? Yes

{
Turn on the upper four LEDs

PORTC = 0xF0;

No

}
ECET 209 Purdue University 33

How do we get from the Flowchart to the C Code??


if ( counter > 10 )
Is the counter variable greater than 10 ? Yes

{
Turn on the upper four LEDs

PORTC = ~0xF0;

No

}
ECET 209 Purdue University 34

Translated to C

if ( counter > 10 ) { PORTC = ~ 0xF0; }

// turn on LEDs

ECET 209

Purdue University

35

What if the condition isnt true??

We may or may not want to do something if the expression evaluates as False


The False leg is known as the Else

ECET 209

Purdue University

36

Problem Revisited
Problem: Light the upper nibble of the LEDs when the counter is above 10. When the counter is equal to or below 10, light only the least significant bit of the LEDs.

ECET 209

Purdue University

37

Updated Flowchart

No

Is the counter variable greater than 10 ?

Yes

Turn on the LSB of the LEDs

Turn on the upper four LEDs

ECET 209

Purdue University

38

Updated Flowchart

else
No

{
Turn on the LSB of the LEDs

Is the counter variable greater than 10 ?

Yes

Turn on the upper four LEDs

}
ECET 209 Purdue University 39

Updated Flowchart

else
No

{
Turn on the LSB of the LEDs

Is the counter variable greater than 10 ?

Yes

Turn on the upper four LEDs

}
PORTC = ~0x01;
ECET 209 Purdue University 40

Transformation to C Code
if (counter > 10) else
No

{
Turn on the LSB of the LEDs

Is the counter variable greater than 10 ?

Yes

{
Turn on the upper four LEDs

}
PORTC = ~0x01;
ECET 209

}
PORTC = ~0xF0;
41

Purdue University

Updated C Code
if ( counter > 10 ) { PORTC = ~ 0xF0; // turn on LEDs } else { PORTC = ~ 0x01; // turn on LSB } ECET 209 Purdue University

42

Updated C Code
if ( counter > 10 ) { PORTC = ~ 0xF0; // turn on LEDs } else { PORTC = ~ 0x01; // turn on LSB } ECET 209 Purdue University

43

Updated C Code
if ( counter > 10 ) { PORTC = ~ 0xF0; // turn on LEDs } else { PORTC = ~ 0x01; // turn on LSB } ECET 209 Purdue University

44

You might also like