Professional Documents
Culture Documents
L7 Algorithms and Flowcharts
L7 Algorithms and Flowcharts
Overview
Example Algorithms Flowcharts and C Code
ECET 209
Purdue University
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
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
ECET 209
Purdue University
12
Decision
ECET 209
Purdue University
13
ECET 209
Flowchart Rules
Rule 1 Rule 3
Rule 3
Rule 2
ECET 209
Purdue University
16
Flowchart Rules
ECET 209
Purdue University
17
Averaging Example
Averaging a set of numbers sum = 0
i=0
i=i+1
sum = sum + ni
Does i = 6?
No
End
End
Yes
Average = sum / 6
ECET 209
Purdue University
End
18
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
ECET 209
Purdue University
21
ECET 209
Purdue University
22
ECET 209
Purdue University
23
Yes
No
ECET 209
Purdue University
24
No
ECET 209
Purdue University
25
No
ECET 209
Purdue University
26
if
Is the counter variable greater than 10 ? Yes Turn on the upper four LEDs
No
ECET 209
Purdue University
27
No
ECET 209
Purdue University
28
{
Turn on the upper four LEDs
No
}
ECET 209 Purdue University 29
{
Turn on the upper four LEDs
No
}
ECET 209 Purdue University 30
{
Turn on the upper four LEDs
PORTC = 0xF0;
No
}
ECET 209 Purdue University 31
ECET 209
Purdue University
32
{
Turn on the upper four LEDs
PORTC = 0xF0;
No
}
ECET 209 Purdue University 33
{
Turn on the upper four LEDs
PORTC = ~0xF0;
No
}
ECET 209 Purdue University 34
Translated to C
// turn on LEDs
ECET 209
Purdue University
35
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
Yes
ECET 209
Purdue University
38
Updated Flowchart
else
No
{
Turn on the LSB of the LEDs
Yes
}
ECET 209 Purdue University 39
Updated Flowchart
else
No
{
Turn on the LSB of the LEDs
Yes
}
PORTC = ~0x01;
ECET 209 Purdue University 40
Transformation to C Code
if (counter > 10) else
No
{
Turn on the LSB of the LEDs
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