cpelecia final lecture 1 - programming mikrobasic using control structure part 2(1)

20
Programming Programming mikroBasic mikroBasic using Control Structure using Control Structure Part 2 – Iterative and Jump Part 2 – Iterative and Jump Statements Statements CpELECIA: Embedded System Final Lecture 1 Engr. Ricrey E. Marquez, CpE, MSCS

Upload: louie

Post on 13-Apr-2015

25 views

Category:

Documents


0 download

DESCRIPTION

CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

TRANSCRIPT

Page 1: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

Programming Programming mikroBasicmikroBasic using Control Structure using Control Structure

Part 2 – Iterative and Jump Part 2 – Iterative and Jump StatementsStatements

CpELECIA: Embedded SystemFinal Lecture 1

Engr. Ricrey E. Marquez, CpE, MSCS

Page 2: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

Introduction to Iterative Introduction to Iterative StatementStatement

Iterative or Looping StatementsIterative or Looping Statements

These control structure allows a repeating one or more instructions for a number of times

Conducting expression determines the number of iterations loop will go through.

Three are three types of looping statements in mikroBasic:

for loop while loop do-while loop

Page 3: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

It be can use the statements break and continue to control the flow of a loop statement.

breakbreak terminates the statement in which it occurs continuecontinue begins executing the next iteration of

the sequence.

Introduction to Iterative Introduction to Iterative StatementStatement

Page 4: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

Also known as repeating of a program segment An iterative statement that requires to specify the

number of iterations of the loop to go through.

Every statement between for and next will be executed once per iteration.

General syntax for FOR loop:

FOR counter = initialValue TO finalValue [STEP step_value] statement_1

statement_2 ... statement_N

NEXT counter

Iterative Statement – Iterative Statement – FOR loop StatementFOR loop Statement

Page 5: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

counter is a variable initialValue and finalValue are expressions compatible with

counter

statement/s is any statement that does not change the value of counter

step_value is value that is added to the counter in each

iteration.

step_value is optional, and defaults to 1 if not stated otherwise. Be careful when using large values for step_value, as overflow may

occur The parameter step_value may be negative, allowing to create a

countdown In FOR statement, the results in an endless loop if the finalValue

equals or exceeds the range of counter’s type.

Iterative Statement – Iterative Statement – FOR loop StatementFOR loop Statement

Page 6: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

Code Sample 1: This sample code turn on and off the LED’s connected to port B of PIC16F84A ten times.

PROGRAM forLoop1DIM i AS BYTE

MAIN:TRISB=0PORTB=0FOR i = 1 TO 10

PORTB=0DELAY_MS(500)PORTB=255DELAY_MS(500)

NEXT iEND.

Iterative Statement – Iterative Statement – FOR loop StatementFOR loop Statement

Page 7: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

Code Sample 2: This sample code turn on and off the LED’s connected to port B of PIC16F84A ten times.

PROGRAM forLoop2DIM i AS BYTE

MAIN:TRISB=0PORTB=0FOR i = 10 TO 1 STEP -1

PORTB=0DELAY_MS(500)PORTB=255DELAY_MS(500)

NEXT iEND.

Iterative Statement – Iterative Statement – FOR loop StatementFOR loop Statement

Page 8: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

An iterative statement that will loop while condition is fulfilled It is similar to do..loop, except the check is performed at the

beginning of the loop.

If expression returns FALSE upon first test, statements will not be executed.

expression is tested first.

If it returns TRUE, all the following statements enclosed by while and wend will be executed (or only one statement, alternatively).

It will keep on executing statements until the expression returns FALSE.

As expression returns FALSE, while will be terminated without executing statements.

Iterative Statement – Iterative Statement – WHILE loop StatementWHILE loop Statement

Page 9: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

General Syntax of WHILE loop:

WHILE expression statement_0 statement_1 ... statement_N

WEND

Iterative Statement – Iterative Statement – WHILE loop StatementWHILE loop Statement

Page 10: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

Iterative Statement – Iterative Statement – WHILE loop StatementWHILE loop Statement

Code Sample 3: This sample code turn on and off the LED’s connected to port B of PIC16F84A ten times.

PROGRAM whileLoop2DIM i AS BYTE

MAIN:TRISB=0PORTB=0i=1WHILE (i<=10)

PORTB=0DELAY_MS(500)PORTB=255DELAY_MS(500)

i=i+1WEND

END.

Page 11: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

Code Sample 4: This sample code turn on and off the LED’s connected to port B of PIC16F84A ten times.

PROGRAM whileLoop2DIM i AS BYTE

MAIN:TRISB=0PORTB=0i=10WHILE (i>=10)

PORTB=0DELAY_MS(500)PORTB=255DELAY_MS(500)

i=i-1WEND

END.

Iterative Statement – Iterative Statement – WHILE loop StatementWHILE loop Statement

Page 12: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

An iterative that will loop until condition is fulfilled It is an statement executes until the condition

becomes TRUE. General syntax for DO..LOOP UNTIL loop:

DO

statement_1

...

statement_N

LOOP UNTIL expression

Iterative Statement – Iterative Statement – DO..LOOP UNTIL StatementDO..LOOP UNTIL Statement

Page 13: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

expression returns a TRUE or FALSE value.

Executes statement_1; ...; statement_N continually, checking the expression after each iteration.

When expression returns TRUE, the do..loop statement terminates.

Sequence is executed at least once

Iterative Statement – Iterative Statement – DO.. LOOP UNTIL StatementDO.. LOOP UNTIL Statement

Page 14: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

Iterative Statement – Iterative Statement – DO..LOOP UNTIL StatementDO..LOOP UNTIL Statement

Code Sample 5: This sample code turn on and off the LED’s connected to port B of PIC16F84A ten times.

PROGRAM dowhileLoop1DIM i AS BYTE

MAIN:TRISB=0PORTB=0i=0DO

i=i+1PORTB=0DELAY_MS(500)PORTB=255DELAY_MS(500)

LOOP UNTIL (i>=10)END.

Page 15: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

Code Sample 6: This sample code turn on and off the LED’s connected to port B of PIC16F84A ten times.

PROGRAM dowhileLoop2DIM i AS BYTE

MAIN:TRISB=0PORTB=0i=10DO

i=i-1PORTB=0DELAY_MS(500)PORTB=255DELAY_MS(500)

LOOP UNTIL (i<=0)END.

Iterative Statement – Iterative Statement – DO..LOOP UNTIL StatementDO..LOOP UNTIL Statement

Page 16: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

Jump StatementsJump Statements

These are statement that when executed, it transfers control unconditionally.

There are four such statements in mikroBasic:

break statement continue statement goto statement gosub statement

Introduction to Jump Introduction to Jump StatementStatement

Page 17: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

It is use within loops to pass control to the first statement following the innermost loop (for, while, and do).

If loop need to be stop from within its body.

Sample Code 6: This program will detect card then exit from the loop if card is inserted.

PROGRAM breakinLCDMAIN:

' Wait for CF card to be plugged; refresh every secondWHILE TRUE

Lcd_Out(1,1,"No card inserted")IF Cf_Detect() = 1 THEN

BREAKEND IFDELAY_MS(1000)

WEND

' Now we can work with CF card ...Lcd_Out(1,1,"Card detected ")END.

Jump Statement –Jump Statement –BREAK StatementBREAK Statement

Page 18: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

It is a statement use within loops to “skip the cycle”

In FOR loop moves program counter to the line with keyword FOR

It does not change the loop counter

In WHILE loop moves program counter to the line with loop condition (top of the loop)

In DO..LOOP UNTIL loop moves program counter to the line with loop condition (bottom of the loop).

Jump Statement –Jump Statement –CONTINUE StatementCONTINUE Statement

Page 19: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

Jump Statement –Jump Statement –GOTO StatementGOTO Statement

This transfer control to the location of a local label specified by label_name.

Syntax of GOTO statement:

GOTO label_name

GOTO line can come before or after the label.

It is not possible to jump into or out of routine.

It is use to break out from any level of nested control structures.

Never jump into a loop or other structured statement, since this can have unpredictable effects.

Use of GOTO statement is generally discouraged as practically every algorithm can be realized without it, resulting in legible structured programs.

One possible application of GOTO statement is breaking out from deeply

nested control structures.

Page 20: CpELECIA Final Lecture 1 - Programming MikroBasic Using Control Structure Part 2(1)

This will transfer control to the location of a local label specified by label_name and calling point is remembered.

Upon encountering a return statement, program execution will continue with

the next statement (line) after the GOSUB. GOSUB line can come before or after the label.

Syntax of GOSUB statement:

GOSUB label_name...label_name:...

It is not possible to jump into or out of routine by means of GOSUB.

Never jump into a loop or other structured statement, since this can have unpredictable effects.

Jump Statement –Jump Statement –GOSUB StatementGOSUB Statement