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 2


Formal Problem Solving Steps

• Understand the Problem


• Develop an Algorithm
• Refine the Algorithm
• Create an Action Plan
• Generate a Solution
• Test the Solution

ECET 209 Purdue University 3


Algorithms

• Step by step method to solve a problem


• Must include ALL required information

ECET 209 Purdue University 4


ECET 209 Purdue University 5
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 7


Decisions

ECET 209 Purdue University 8


Decisions

ECET 209 Purdue University 9


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
ECET 209 Purdue University 15
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

Average a set of i=0


Averaging a set of Numbers
numbers

i=i+1
Calculate the Sum of
the Numbers
Calculate the
Average of the 6 sum = sum + ni
Numbers
Divide the Sum by the
Number of Entries Does No
i = 6?
End
Yes
End

Average = sum / 6

ECET 209 Purdue University 18


End
Averaging a set of
numbers

sum = 0

i=0

i=i+1

sum = sum + ni

Does No
i = 6?

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 >=

ECET 209 Purdue University 20


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 Yes


variable greater
than 10 ?

Turn on the upper


No four LEDs

ECET 209 Purdue University 24


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

What is
this??
Is the counter Yes
variable greater
than 10 ?

Turn on the upper


No four LEDs

ECET 209 Purdue University 25


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

if
Is the counter Yes
variable greater
than 10 ?

Turn on the upper


No four LEDs

ECET 209 Purdue University 26


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

What??
if
Is the counter Yes
variable greater
than 10 ?

Turn on the upper


No four LEDs

ECET 209 Purdue University 27


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

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

Turn on the upper


No four LEDs

ECET 209 Purdue University 28


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

if ( counter > 10 )
Is the counter Yes
variable greater
than 10 ?
{
Turn on the upper
No four LEDs

ECET 209 Purdue University 29


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

if ( counter > 10 )
Is the counter Yes
variable greater
than 10 ?
{
Turn on the upper
No four LEDs

ECET 209 Purdue University 30


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

if ( counter > 10 )
Is the counter Yes
variable greater
than 10 ?
{ PORTC = 0xF0;
Turn on the upper
No four LEDs

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 Yes
variable greater
than 10 ?
{ PORTC = 0xF0;
Turn on the upper
No four LEDs

ECET 209 Purdue University 33


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

if ( counter > 10 )
Is the counter Yes
variable greater
than 10 ?
{ PORTC = ~0xF0;
Turn on the upper
No four LEDs

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 isn’t 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 Yes


variable greater
than 10 ?

Turn on the LSB Turn on the upper


of the LEDs four LEDs

ECET 209 Purdue University 38


Updated Flowchart

else
No Is the counter Yes
variable greater

{ than 10 ?

Turn on the LSB Turn on the upper


of the LEDs four LEDs

ECET 209 Purdue University 39


Updated Flowchart

else
No Is the counter Yes
variable greater

{ than 10 ?

Turn on the LSB Turn on the upper


of the LEDs four LEDs

}
PORTC = ~0x01;
ECET 209 Purdue University 40
Transformation to C Code

if (counter > 10)


else No Is the counter Yes
variable greater
{ than 10 ? {
Turn on the LSB Turn on the upper
of the LEDs four LEDs

} }
PORTC = ~0x01; PORTC = ~0xF0;
ECET 209 Purdue University 41
Updated C Code

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

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

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

You might also like