Like this presentation? Why not share!

# Algorithms && Flowcharts

## on Oct 06, 2011

• 2,204 views

For Learnig Flow Charrts and Algorithm!!!!

For Learnig Flow Charrts and Algorithm!!!!

### Views

Total Views
2,204
Views on SlideShare
2,204
Embed Views
0

Likes
0
43
0

No embeds

### Report content

• Comment goes here.
Are you sure you want to

## Algorithms && FlowchartsPresentation Transcript

• Algorithms and Flowcharts ECET 209 – Lecture 7Introduction to Microcontrollers
• Overview• Example Algorithms• Flowcharts and C CodeECET 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 SolutionECET 209 Purdue University 3
• Algorithms• Step by step method to solve a problem• Must include ALL required informationECET 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 FlowchartingECET 209 Purdue University 6
• FlowchartingECET 209 Purdue University 7
• DecisionsECET 209 Purdue University 8
• DecisionsECET 209 Purdue University 9
• DecisionsECET 209 Purdue University 10
• DecisionsECET 209 Purdue University 11
• For Loop vs. While LoopECET 209 Purdue University 12
• DecisionECET 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 structureECET 209 Purdue University 15
• Flowchart RulesRule 1 Rule 3 Rule 3 Rule 2ECET 209 Purdue University 16
• Flowchart Rules Configure PortC for Output Configure I/O PORTS Configure PortA for Input Turn on PortA Pull-upsECET 209 Purdue University 17
• Averaging Example Averaging a set of numbers sum = 0 Average a set of i=0Averaging a set of Numbers numbers i=i+1 Calculate the Sum of the Numbers Calculate theAverage 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 EndECET 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 LEDsECET 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 LEDsECET 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 LEDsECET 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 LEDsECET 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 LEDsECET 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 ElseECET 209 Purdue University 36
• Problem RevisitedProblem: 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 LEDsECET 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 }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