44
Algorithms and Flowcharts ECET 209 – Lecture 7 Introduction to Microcontrollers

L7 Algorithms and Flowcharts

Embed Size (px)

DESCRIPTION

a

Citation preview

Page 1: L7 Algorithms and Flowcharts

Algorithms and Flowcharts

ECET 209 – Lecture 7Introduction to Microcontrollers

Page 2: L7 Algorithms and Flowcharts

ECET 209 Purdue University 2

Overview

• Example Algorithms• Flowcharts and C Code

Page 3: L7 Algorithms and Flowcharts

ECET 209 Purdue University 3

Formal Problem Solving Steps

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

Page 4: L7 Algorithms and Flowcharts

ECET 209 Purdue University 4

Algorithms

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

Page 5: L7 Algorithms and Flowcharts

ECET 209 Purdue University 5

Page 6: L7 Algorithms and Flowcharts

ECET 209 Purdue University 6

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

Page 7: L7 Algorithms and Flowcharts

ECET 209 Purdue University 7

Flowcharting

Page 8: L7 Algorithms and Flowcharts

ECET 209 Purdue University 8

Decisions

Page 9: L7 Algorithms and Flowcharts

ECET 209 Purdue University 9

Decisions

Page 10: L7 Algorithms and Flowcharts

ECET 209 Purdue University 10

Decisions

Page 11: L7 Algorithms and Flowcharts

ECET 209 Purdue University 11

Decisions

Page 12: L7 Algorithms and Flowcharts

ECET 209 Purdue University 12

For Loop vs. While Loop

Page 13: L7 Algorithms and Flowcharts

ECET 209 Purdue University 13

Decision

Page 14: L7 Algorithms and Flowcharts

ECET 209 Purdue University 14

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!

Page 15: L7 Algorithms and Flowcharts

ECET 209 Purdue University 15

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

Page 16: L7 Algorithms and Flowcharts

ECET 209 Purdue University 16

Flowchart Rules

Rule 1Rule 3

Rule 2

Rule 3

Page 17: L7 Algorithms and Flowcharts

ECET 209 Purdue University 17

Flowchart Rules

Configure PortC for Output

Configure PortA for Input

Turn on PortA Pull-ups

Configure I/O PORTS

Page 18: L7 Algorithms and Flowcharts

ECET 209 Purdue University 18

Averaging Example

Averaging a set ofnumbers

Calculate theAverage of the 6

Numbers

End

Averaging a set ofnumbers

End

sum = 0

i = 0

i = i + 1

sum = sum + ni

Does i = 6?

No

Yes

Average = sum / 6

Calculate the Sum ofthe Numbers

Divide the Sum by theNumber of Entries

Average a set ofNumbers

End

Page 19: L7 Algorithms and Flowcharts

ECET 209 Purdue University 19

Averaging a set ofnumbers

End

sum = 0

i = 0

i = i + 1

sum = sum + ni

Does i = 6?

No

Yes

Average = sum / 6

Page 20: L7 Algorithms and Flowcharts

ECET 209 Purdue University 20

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

Page 21: L7 Algorithms and Flowcharts

ECET 209 Purdue University 21

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!! )

Page 22: L7 Algorithms and Flowcharts

ECET 209 Purdue University 22

More Likely Examples

( number_of_dogs > 3 )

( value != 0 )

( counter > 10 )

Page 23: L7 Algorithms and Flowcharts

ECET 209 Purdue University 23

Relational Operators are Typically used with Decisions

• For example…

Problem:Light the upper nibble of the LEDswhen the counter is above 10.

Page 24: L7 Algorithms and Flowcharts

ECET 209 Purdue University 24

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

Is the countervariable greater

than 10 ?

Turn on the upperfour LEDs

Yes

No

Page 25: L7 Algorithms and Flowcharts

ECET 209 Purdue University 25

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

Is the countervariable greater

than 10 ?

Turn on the upperfour LEDs

Yes

No

What is this??

Page 26: L7 Algorithms and Flowcharts

ECET 209 Purdue University 26

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

Is the countervariable greater

than 10 ?

Turn on the upperfour LEDs

Yes

No

if

Page 27: L7 Algorithms and Flowcharts

ECET 209 Purdue University 27

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

Is the countervariable greater

than 10 ?

Turn on the upperfour LEDs

Yes

No

ifWhat??

Page 28: L7 Algorithms and Flowcharts

ECET 209 Purdue University 28

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

Is the countervariable greater

than 10 ?

Turn on the upperfour LEDs

Yes

No

if ( counter > 10 )

Page 29: L7 Algorithms and Flowcharts

ECET 209 Purdue University 29

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

Is the countervariable greater

than 10 ?

Turn on the upperfour LEDs

Yes

No

if ( counter > 10 )

{

}

Page 30: L7 Algorithms and Flowcharts

ECET 209 Purdue University 30

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

Is the countervariable greater

than 10 ?

Turn on the upperfour LEDs

Yes

No

if ( counter > 10 )

{

}

Page 31: L7 Algorithms and Flowcharts

ECET 209 Purdue University 31

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

Is the countervariable greater

than 10 ?

Turn on the upperfour LEDs

Yes

No

if ( counter > 10 )

{

}

PORTC = 0xF0;

Page 32: L7 Algorithms and Flowcharts

ECET 209 Purdue University 32

Page 33: L7 Algorithms and Flowcharts

ECET 209 Purdue University 33

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

Is the countervariable greater

than 10 ?

Turn on the upperfour LEDs

Yes

No

if ( counter > 10 )

{

}

PORTC = 0xF0;

Page 34: L7 Algorithms and Flowcharts

ECET 209 Purdue University 34

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

Is the countervariable greater

than 10 ?

Turn on the upperfour LEDs

Yes

No

if ( counter > 10 )

{

}

PORTC = ~0xF0;

Page 35: L7 Algorithms and Flowcharts

ECET 209 Purdue University 35

Translated to C

if ( counter > 10 ){

PORTC = ~ 0xF0; // turn on LEDs}

Page 36: L7 Algorithms and Flowcharts

ECET 209 Purdue University 36

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

Page 37: L7 Algorithms and Flowcharts

ECET 209 Purdue University 37

Problem Revisited

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

Page 38: L7 Algorithms and Flowcharts

ECET 209 Purdue University 38

Updated Flowchart

Is the countervariable greater

than 10 ?

Turn on the upperfour LEDs

YesNo

Turn on the LSBof the LEDs

Page 39: L7 Algorithms and Flowcharts

ECET 209 Purdue University 39

Updated Flowchart

Is the countervariable greater

than 10 ?

Turn on the upperfour LEDs

YesNo

Turn on the LSBof the LEDs

{

}

else

Page 40: L7 Algorithms and Flowcharts

ECET 209 Purdue University 40

Updated Flowchart

Is the countervariable greater

than 10 ?

Turn on the upperfour LEDs

YesNo

Turn on the LSBof the LEDs

{

}

else

PORTC = ~0x01;

Page 41: L7 Algorithms and Flowcharts

ECET 209 Purdue University 41

Transformation to C Code

Is the countervariable greater

than 10 ?

Turn on the upperfour LEDs

YesNo

Turn on the LSBof the LEDs

else

if (counter > 10)

{{

} }PORTC = ~0x01; PORTC = ~0xF0;

Page 42: L7 Algorithms and Flowcharts

ECET 209 Purdue University 42

Updated C Code

if ( counter > 10 ){

PORTC = ~ 0xF0; // turn on LEDs}else{

PORTC = ~ 0x01; // turn on LSB}

Page 43: L7 Algorithms and Flowcharts

ECET 209 Purdue University 43

Updated C Code

if ( counter > 10 ){

PORTC = ~ 0xF0; // turn on LEDs}else{

PORTC = ~ 0x01; // turn on LSB}

Page 44: L7 Algorithms and Flowcharts

ECET 209 Purdue University 44

Updated C Code

if ( counter > 10 ){

PORTC = ~ 0xF0; // turn on LEDs}else{

PORTC = ~ 0x01; // turn on LSB}