L7 Algorithms and Flowcharts

Preview:

DESCRIPTION

a

Citation preview

Algorithms and Flowcharts

ECET 209 – Lecture 7Introduction to Microcontrollers

ECET 209 Purdue University 2

Overview

• Example Algorithms• Flowcharts and C Code

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

ECET 209 Purdue University 4

Algorithms

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

ECET 209 Purdue University 5

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

ECET 209 Purdue University 7

Flowcharting

ECET 209 Purdue University 8

Decisions

ECET 209 Purdue University 9

Decisions

ECET 209 Purdue University 10

Decisions

ECET 209 Purdue University 11

Decisions

ECET 209 Purdue University 12

For Loop vs. While Loop

ECET 209 Purdue University 13

Decision

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!

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

ECET 209 Purdue University 16

Flowchart Rules

Rule 1Rule 3

Rule 2

Rule 3

ECET 209 Purdue University 17

Flowchart Rules

Configure PortC for Output

Configure PortA for Input

Turn on PortA Pull-ups

Configure I/O PORTS

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

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

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

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

ECET 209 Purdue University 22

More Likely Examples

( number_of_dogs > 3 )

( value != 0 )

( counter > 10 )

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.

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

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

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

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

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 )

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 )

{

}

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 )

{

}

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;

ECET 209 Purdue University 32

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;

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;

ECET 209 Purdue University 35

Translated to C

if ( counter > 10 ){

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

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

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.

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

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

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;

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;

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

Updated C Code

if ( counter > 10 ){

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

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

Recommended